簡體   English   中英

savon ruby​​ gem中的按請求命名空間

[英]Per request namespaces in savon ruby gem

我需要發送如下請求:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://www.w3.org/2005/08/addressing">
   <env:Header>
      <ns2:Action>http://tempuri.org/IDataImportService/Login</ns2:Action>
      <ns2:To>https://URLHERE.svc</ns2:To>
   </env:Header>
   <env:Body>
      <ns1:Login>
         <ns1:userName>USERNAME</ns1:userName>
         <ns1:password>PASSWORD</ns1:password>
      </ns1:Login>
   </env:Body>
</env:Envelope>

為此,我可以在Savon周圍亂搞。

現在我的理解是Savon正在研究WSDL以找出要添加的名稱空間。 有一個叫做element_form_default的東西,它與我在call Eg中與namespace_identifiersoap_header手動補丁結合使用時可以做些什么

client = Savon.client(
  wsdl: 'https://urlhere.svc?wsdl',
  log: true,
  soap_version: 2,
  pretty_print_xml: true,
  log_level: :debug,
  namespaces: {
    'xmlns:ns1' => 'http://tempuri.org/',
    'xmlns:ns2' => 'http://www.w3.org/2005/08/addressing'
  },
  namespace_identifier: 'ns1',
  element_form_default: :qualified
)

client.call(
  :login,
  message: {
    'userName' => USERNAME,
    'password' => PASSWORD,
  },
  soap_header: {
    'ns2:Action' => 'http://tempuri.org/IDataImportService/Login',
    'ns2:To' => 'https://urlhere.svc'
  }
)

如果我不必使用3個不同的名稱空間發出另一個請求並傳遞登錄上下文,我將像這樣保留它。

我的問題是:

  1. 有沒有辦法避免手動修補這些名稱空間?
  2. 為什么它可以在帶有SOAP服務的php中正常工作?

例如,我需要在下一個請求中共同完成:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://schemas.datacontract.org/2004/07/Common.DataContracts" xmlns:ns2="http://tempuri.org/" xmlns:ns3="http://www.w3.org/2005/08/addressing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <env:Header>
      <ns3:Action>http://tempuri.org/IDataImportService/CreateLead</ns3:Action>
      <ns3:To>https://URLHERE.svc</ns3:To>
   </env:Header>
   <env:Body>
      <ns2:CreateLead>
         <ns2:autoItContext>
            <ns1:AddressNamePrimary xsi:nil="true" />
            <ns1:Addresshousenumber xsi:nil="true" />
            <ns1:Addressstreet xsi:nil="true" />
         </ns2:autoItContext>
         <ns2:companyId>111</ns2:companyId>
         <ns2:description></ns2:description>
         <ns2:leadData>
            <ns1:C_Address xsi:nil="true" />
            <ns1:C_Address2 xsi:nil="true" />
            <ns1:C_CellPhoneNumber xsi:nil="true" />
            <ns1:C_City xsi:nil="true" />
            <ns1:C_CprNumber1 xsi:nil="true" />
            <ns1:C_CvrNumber xsi:nil="true" />
            <ns1:C_Email>email@email.com</ns1:C_Email>
         </ns2:leadData>
         <ns2:checkForDuplicates>false</ns2:checkForDuplicates>
         <ns2:listId xsi:nil="true" />
      </ns2:CreateLead>
   </env:Body>
</env:Envelope>

要回答我自己的問題,我認為savon不能跟進名稱空間。 但是,在研究了savon的代碼之后,我意識到我可以解決這個問題,因為SOAP請求對於我的應用程序不是那么重要。

這是處理多個名稱空間的好方法:

  def login_request
    request = Savon::SOAPRequest.new(globals).build(
      soap_action: :login,
      cookies: locals[:cookies],
      headers: locals[:headers]
    )
    request.headers['Content-Type'] = 'application/soap+xml'
    request.url = @url
    request.body = login_xml_string
    HTTPI.post(request)
  end


  def globals
    @_globals ||= Savon::GlobalOptions.new
  end

  def locals
    @_locals ||= Savon::LocalOptions.new
  end

其中login_xml_string是一種基於xml.erb文件返回字符串化SOAP信封的方法。 例如

def login_xml_string
  @view_paths.render(file: 'app/views/path/to/file/login.xml.erb',
    locals: { user_name: @username, password: @password, url: @url}
  ).gsub(/\n[\s+]{0,}/, '')
end

並且@view_paths設置為ActionView::Base.new(ActionController::Base.view_paths, {})

我需要調用的其余SOAP操作也是如此。

我並不是說這是最好的方法,但是通過將名稱空間傳遞給本地名稱,這使我的生活變得更加輕松。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM