2017-06-16 140 views
2

我有一個SOAP請求採取了下文XML主體不能創建在泡沫

<x:Body> 
    <ser:CreateExportJobRequest> 
     <ser:ExportJobTypeName>Products</ser:ExportJobTypeName> 
     <ser:ExportColumns> 
      <ser:ExportColumn>Id</ser:ExportColumn> 
      <ser:ExportColumn>itemName</ser:ExportColumn> 
     </ser:ExportColumns> 
     <ser:ExportFilters> 
      <ser:ExportFilter id="updatedSince"> 
       <ser:Text>2.0</ser:Text> 
      </ser:ExportFilter> 
     </ser:ExportFilters> 
     <ser:Frequency>ONETIME</ser:Frequency> 
    </ser:CreateExportJobRequest> 
</x:Body> 

我可以使用Boomerang一個成功的請求SOAP過濾器。

現在我真的想在我的Python代碼中使用它。所以,我想,

inputElement = client.factory.create('CreateExportJobRequest') 

inputElement.ExportJobTypeName = "Products" 
inputElement.ExportColumns.ExportColumn = ["Id", "itemName"] 

inputElement.Frequency = 'ONETIME' 

if updatedSince: 
    inputElement.ExportFilters.ExportFilter = ['updatedSince'] 

t = client.service.CreateExportJob(inputElement.ExportJobTypeName, inputElement.ExportColumns, inputElement.ExportFilters, None, None, inputElement.Frequency) 

我得到一個錯誤,

'list' object has no attribute 'id' 

因爲有些錯誤的XML請求被創建

<ns1:ExportFilters> 
    <ns1:ExportFilter>updatedSince</ns1:ExportFilter> 
</ns1:ExportFilters> 

所以我嘗試一些其他的事情ExportFilter

inputElement.ExportFilters.ExportFilter = [{'id': 'updatedSince', 'text': updatedSince}] 

inputElement.ExportFilters.ExportFilter = [('updatedSince', updatedSince)] 

inputElement.ExportFilters.ExportFilter = [{'updatedSince': updatedSince}] 
# says, Type not found: 'updatedSince' 

inputElement.ExportFilters.ExportFilter = [ 
    {'key': 'updatedSince', 'value': {'key': 'eq', 'value': updatedSince}} 
] 
# says, Type not found: 'value' 

,但沒有什麼工作。

之前設置ExportFilter,它的價值是在

ExportFilters: (ExportFilters){ 
    ExportFilter[] = <empty> 
} 

請幫助的形式。

回答

1

經過調試和通過一些泡沫代碼後,我找到了修復程序。

此修復程序的完整代碼片段:

inputElement = client.factory.create('CreateExportJobRequest') 

inputElement.ExportJobTypeName = "Products" 
inputElement.ExportColumns.ExportColumn = ["Id", "itemName"] 

inputElement.Frequency = 'ONETIME' 

if updatedSince: 
    efilter = client.factory.create("ExportFilter") 
    efilter._id = 'updatedSince' 
    efilter.Text = updatedSince 
    inputElement.ExportFilters.ExportFilter.append(efilter) 

t = client.service.CreateExportJob(inputElement.ExportJobTypeName, inputElement.ExportColumns, inputElement.ExportFilters, None, None, inputElement.Frequency) 

調試: 因爲泡沫是提高TypeNotFound例外,我找了所有raise TypeNotFound內泡沫的地方。我把調試點放在我的PyCharm中。

我發現方法從Typed類裏面的suds/mx/literal.py提高了我得到的錯誤。

def start(self, content): 
    # 
    # Start marshalling the 'content' by ensuring that both the 
    # 'content' _and_ the resolver are primed with the XSD type 
    # information. The 'content' value is both translated and 
    # sorted based on the XSD type. Only values that are objects 
    # have their attributes sorted. 
    # 
    log.debug('starting content:\n%s', content) 
    if content.type is None: 
     name = content.tag 
     if name.startswith('_'): 
      name = '@'+name[1:] 
     content.type = self.resolver.find(name, content.value) 
     if content.type is None: 
      raise TypeNotFound(content.tag) 
    else: 
     known = None 
     if isinstance(content.value, Object): 
      known = self.resolver.known(content.value) 
      if known is None: 
       log.debug('object has no type information', content.value) 
       known = content.type 
     frame = Frame(content.type, resolved=known) 
     self.resolver.push(frame) 
    frame = self.resolver.top() 
    content.real = frame.resolved 
    content.ancestry = frame.ancestry 
    self.translate(content) 
    self.sort(content) 
    if self.skip(content): 
     log.debug('skipping (optional) content:\n%s', content) 
     self.resolver.pop() 
     return False 
    else: 
     return True 

所以從這個邏輯,我來到了修復。

但是,如果有人爲此提出了一個標準程序,那將非常棒。