[英]Per request namespaces in savon ruby gem
I need to send a request like: 我需要发送如下请求:
<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>
To accomplish that I can hack around Savon. 为此,我可以在Savon周围乱搞。
Now my understand is Savon is looking at the WSDL to figure out what namespace to add. 现在我的理解是Savon正在研究WSDL以找出要添加的名称空间。 There is a thing called element_form_default which slightly does what I want in combination with
namespace_identifier
and manual patch of soap_header
in call
Eg 有一个叫做element_form_default的东西,它与我在
call
Eg中与namespace_identifier
和soap_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'
}
)
I'll leave it like this if I didn't have to make another request with 3 different namespaces and pass the context of login... 如果我不必使用3个不同的名称空间发出另一个请求并传递登录上下文,我将像这样保留它。
The questions I have are: 我的问题是:
Eg of what I need co accomplish in next request: 例如,我需要在下一个请求中共同完成:
<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>
To answer my own question, I don't think savon can followup namespaces. 要回答我自己的问题,我认为savon不能跟进名称空间。 However after digging in savon's code I realized I can hack around the problem since SOAP requests are not that vital for my application.
但是,在研究了savon的代码之后,我意识到我可以解决这个问题,因为SOAP请求对于我的应用程序不是那么重要。
Here is a nice solution to deal with multiple namespaces: 这是处理多个名称空间的好方法:
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
Where login_xml_string
is a method that returns a stringify SOAP envelope based on a xml.erb
file. 其中
login_xml_string
是一种基于xml.erb
文件返回字符串化SOAP信封的方法。 Eg 例如
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
And @view_paths
is set to ActionView::Base.new(ActionController::Base.view_paths, {})
并且
@view_paths
设置为ActionView::Base.new(ActionController::Base.view_paths, {})
The same goes for the rest SOAP actions I need to call. 我需要调用的其余SOAP操作也是如此。
I'm not saying this is the best approach but it made my life easier dealing with namespaces by passing them in locals. 我并不是说这是最好的方法,但是通过将名称空间传递给本地名称,这使我的生活变得更加轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.