2014-04-22 80 views
1

我被迫使用Savon進入肥皂世界並遇到一些問題。將原始XML查詢轉換爲Savon 2格式

的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> 

我試圖與薩翁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 

我在什麼肥皂有點失落,但特別是在這裏丟失,因爲它似乎是我,包括我的薩翁語法的一切,但必須失去了一些東西......

編輯:這裏的在調試的全部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:[email protected]/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"}}}}>> 

回答

1

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

你應該設置參數

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

,看一看。

+0

感謝您的提示。我已經用完整的回覆更新了我的問題。我得到一個200但神祕的不是關於某些值不能爲空。 – SethS

+1

現在是大槍的時候了:-)。第1步:使用SoapUI創建一個有效的請求。第2步:在Ruby/Savon中重建請求。這將是解決問題的標準方法。顯然,系統不會返回有意義的錯誤消息。 (不幸的是並不罕見)。 –

1

我最近處理了這個相同的問題。 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)