将原始XML查询转换为Savon 2格式

时间:2014-04-22 02:23:40

标签: ruby-on-rails ruby soap savon rets

我被迫使用Savon进入肥皂世界并遇到一些问题。

wsdl是:http://evernet.nwmls.com/evernetqueryservice/evernetquery.asmx?wsdl 如果我使用此数据提供程序的测试表单,我可以使用以下格式无误地接收数据:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<EverNetQuerySpecification xmlns="urn:www.nwmls.com/Schemas/General/EverNetQueryXML.xsd">
  <Message>
<Head>
  <UserId>my_username_here</UserId>
  <Password>my_password_here</Password>
  <SchemaName>StandardXML1_2</SchemaName>
</Head>
<Body>
  <Query>
    <MLS>nwmls</MLS>
    <PropertyType>RESI</PropertyType>
    <BeginDate>2014-04-20T00:25:00</BeginDate>
    <EndDate>2014-04-22T00:25:00</EndDate>
  </Query>
  <Filter />
</Body>

我尝试使用Savon 2.2在我的rails应用程序中复制它是:

client = Savon.client(:wsdl => "http://evernet.nwmls.com/evernetqueryservice/evernetquery.asmx?wsdl")

response = client.call(:retrieve_listing_data) do |locals|
    locals.message "Query" => {"Head" => {"UserId" => "my_username_here", "Password" => "my_password_here", "SchemaName" => "StandardXML1_2"}, "Body" => {"MLS" => "nwmls", "PropertyType" => "RESI", "BeginDate" => "2014-04-17T00:25:00", "EndDate" => "2014-04-22T00:25:00"}}
end

但我收到的错误是:

Value cannot be null. Parameter name: s

我有点迷失在任何肥皂中,但特别迷失在这里,因为我似乎包含了我的Savon语法中的所有东西,但必须遗漏一些东西......

编辑:这里是调试时的完整xml响应:

D, [2014-04-22T16:55:52.806716 #77213] DEBUG -- : HTTPI GET request to evernet.nwmls.com (net_http)
I, [2014-04-22T16:55:54.340990 #77213]  INFO -- : SOAP request: http://evernet.nwmls.com/evernetqueryservice/evernetquery.asmx
I, [2014-04-22T16:55:54.341077 #77213]  INFO -- : SOAPAction: "http://www.nwmls.com/EverNetServices/RetrieveListingData", Content-Type: text/xml;charset=UTF-8, Content-Length: 682
D, [2014-04-22T16:55:54.341120 #77213] DEBUG -- : <?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://www.nwmls.com/EverNetServices" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><tns:RetrieveListingData><tns:Query><tns:Head><tns:Head>hoverboard</tns:Head><tns:Password>cP6cHias</tns:Password><tns:SchemaName>StandardXML1_2</tns:SchemaName></tns:Head><tns:Body><tns:MLS>nwmls</tns:MLS><tns:PropertyType>RESI</tns:PropertyType><tns:BeginDate>2014-04-17T00:25:00</tns:BeginDate><tns:EndDate>2014-04-22T00:25:00</tns:EndDate></tns:Body></tns:Query></tns:RetrieveListingData></env:Body></env:Envelope>
D, [2014-04-22T16:55:54.341255 #77213] DEBUG -- : HTTPI POST request to evernet.nwmls.com (net_http)
I, [2014-04-22T16:55:54.678125 #77213]  INFO -- : SOAP response (status 200)
D, [2014-04-22T16:55:54.678292 #77213] DEBUG -- : <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><RetrieveListingDataResponse xmlns="http://www.nwmls.com/EverNetServices"><RetrieveListingDataResult>&lt;Listings&gt;
  &lt;ResponseMessages&gt;
    &lt;Message&gt;Value cannot be null.
Parameter name: s&lt;/Message&gt;
  &lt;/ResponseMessages&gt;
&lt;/Listings&gt;</RetrieveListingDataResult></RetrieveListingDataResponse></soap:Body></soap:Envelope>
 => #<Savon::Response:0x007f844230e4e0 @http=#<HTTPI::Response:0x007f844230fc00 @code=200, @headers={"cache-control"=>"private, max-age=0", "content-type"=>"text/xml; charset=utf-8", "server"=>"Microsoft-IIS/7.5", "x-aspnet-version"=>"4.0.30319", "x-powered-by"=>"ASP.NET", "date"=>"Tue, 22 Apr 2014 23:55:54 GMT", "content-length"=>"574"}, @raw_body="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><RetrieveListingDataResponse xmlns=\"http://www.nwmls.com/EverNetServices\"><RetrieveListingDataResult>&lt;Listings&gt;\r\n  &lt;ResponseMessages&gt;\r\n    &lt;Message&gt;Value cannot be null.\r\nParameter name: s&lt;/Message&gt;\r\n  &lt;/ResponseMessages&gt;\r\n&lt;/Listings&gt;</RetrieveListingDataResult></RetrieveListingDataResponse></soap:Body></soap:Envelope>", @body="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><RetrieveListingDataResponse xmlns=\"http://www.nwmls.com/EverNetServices\"><RetrieveListingDataResult>&lt;Listings&gt;\r\n  &lt;ResponseMessages&gt;\r\n    &lt;Message&gt;Value cannot be null.\r\nParameter name: s&lt;/Message&gt;\r\n  &lt;/ResponseMessages&gt;\r\n&lt;/Listings&gt;</RetrieveListingDataResult></RetrieveListingDataResponse></soap:Body></soap:Envelope>">, @globals=#<Savon::GlobalOptions:0x007f8447489a40 @option_type=:global, @options={:encoding=>"UTF-8", :soap_version=>1, :namespaces=>{}, :logger=>#<Logger:0x007f84474899c8 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f84474899a0 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007f8447489950 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007f8447489928 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f84474898d8>>>>, :log=>true, :filters=>[], :pretty_print_xml=>false, :raise_errors=>true, :strip_namespaces=>true, :convert_response_tags_to=>#<Proc:0x007f8447489860@/Users/seth/.rvm/gems/ruby-2.0.0-p195/gems/savon-2.4.0/lib/savon/options.rb:56 (lambda)>, :multipart=>false, :wsdl=>"http://evernet.nwmls.com/evernetqueryservice/evernetquery.asmx?wsdl"}>, @locals=#<Savon::LocalOptions:0x007f84443732a8 @option_type=:local, @options={:advanced_typecasting=>true, :response_parser=>:nokogiri, :multipart=>false, :message=>{"Query"=>{"Head"=>{"Head"=>"my_username", "Password"=>"my_password", "SchemaName"=>"StandardXML1_2"}, "Body"=>{"MLS"=>"nwmls", "PropertyType"=>"RESI", "BeginDate"=>"2014-04-17T00:25:00", "EndDate"=>"2014-04-22T00:25:00"}}}}>> 

2 个答案:

答案 0 :(得分:2)

我最近处理过同样的问题。 NWMLS使用名为v_strXmlQuery的特殊根密钥,该密钥包含嵌套的XML字符串。这对我来说很好用:

[1] pry(main)> require 'savon'
=> true
[2] pry(main)> client = Savon.client(
[2] pry(main)*   wsdl: "http://evernet.nwmls.com/evernetqueryservice/evernetquery.asmx?wsdl",
[2] pry(main)* convert_request_keys_to: :none); # this is important
=> #<Savon::Client:0x007fbf5a324288
  ...
[4] pry(main)> username = 'myusername'
[5] pry(main)> password = 'mypassword'
[7] pry(main)> message =  {v_strXmlQuery:"<?xml version=\"1.0\" encoding=\"UTF-8\"?><EverNetQuerySpecification xmlns=\"urn:www.nwmls.com/Schemas/General/EverNetQueryXML.xsd\"><Message><Head><UserId>#{username}</UserId><Password>#{password}</Password><SchemaName>#{schema_name}</SchemaName></Head><Body><Query><MLS>NWMLS</MLS><PropertyType>RESI</PropertyType><Status>A</Status><County>King</County><City>Seattle</City><BeginDate>2005-08-27T16:14:09</BeginDate><EndDate>2015-08-28T09:14:09</EndDate></Query><Filter></Filter></Body></Message></EverNetQuerySpecification>"}y><BeginDate>2005-08-27T16:14:09</BeginDate><EndDate>2015-08-28T09:14:09</EndDate></Query><Filter></Filter></Body></Message></EverNetQuerySpecification>"}
=> {:v_strXmlQuery=>
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?><EverNetQuerySpecification xmlns=\"urn:www.nwmls.com/Schemas/General/EverNetQueryXML.xsd\"><Message><Head><UserId>myusername</UserId><Password>mypassword</Password><SchemaName>StandardXML1_3</SchemaName></Head><Body><Query><MLS>NWMLS</MLS><PropertyType>RESI</PropertyType><Status>A</Status><County>King</County><City>Seattle</City><BeginDate>2005-08-27T16:14:09</BeginDate><EndDate>2015-08-28T09:14:09</EndDate></Query><Filter></Filter></Body></Message></EverNetQuerySpecification>"}

[8] pry(main)> client.call(:retrieve_listing_data, message: message).body
...
=> {:retrieve_listing_data_response=>
  {:retrieve_listing_data_result=>
    "<Listings xmlns=\"http://www.nwmls.com/Schemas/Standard/StandardXML1_3.xsd\"><Residential>...<MR>Timeless, top of the line finish work in this studs-out craftsman remodel.  This remodel has everything desired in a home: open floor plan, rich hardwood flooring,...

我建议使用像Savon's Gyoku gem这样的XML构建器来构造子哈希并附加xml页眉/页脚,然后将其注入父文档查询中:

# takes a query hash and generates correctly-formatted SOAP-friendly XML
class QueryGenerator

  def self.call(hash)
    new.call(hash)
  end

  def call(hash)
    _xml = Gyoku.xml(hash, key_converter: :camelcase)
    repaired_xml = fix_weird_keys(_xml)
    wrapped_xml = xml_header + repaired_xml + xml_footer
    { v_strXmlQuery: wrapped_xml }
  end

  def xml_header
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?><EverNetQuerySpecification xmlns=\"urn:www.nwmls.com/Schemas/General/EverNetQueryXML.xsd\"><Message>"
  end

  def xml_footer
    "</Message></EverNetQuerySpecification>"
  end

  # because its all Camelcase, except for that one that aint
  def fix_weird_keys(xml_str)
    xml_str.gsub('<Mls>','<MLS>').
            gsub('</Mls>','</MLS>')
  end
end

query_hash = {
  head: {
    user_id:     'myusername',
    password:    'mypassword',
    schema_name: 'StandardXML1_3'
  },
  body: {
    query: {
      mls: 'NWMLS',
      property_type: 'RESI',
      status: 'A',
      city: 'Seattle',
      begin_date: '2014-04-17T00:25:00',
      end_date: '2014-04-22T00:25:00'
    },
    filter: {}
  }
}
listing_data_query =  QueryGenerator.call(query_hash)
client.call(:retrieve_listing_data, message: listing_data_query )

答案 1 :(得分:1)

您是否查看了生成的XML消息?

你应该设置参数

log => true,
log_level => :debug,
pretty_print_xml => true

看看。