代码之家  ›  专栏  ›  技术社区  ›  Shawn Wilson

使用Savon gem格式化SOAP API的问题

  •  0
  • Shawn Wilson  · 技术社区  · 4 年前

    我害怕这一天会到来。。处理SOAP API的。。。

    这对我来说是一个全新的领域,我已经对萨冯宝石做了一些挖掘,但我似乎无法构建我的召唤。。

    本质上,我试图做的是以下几点:

    步骤1:调用API以检索LatestCallerVersion(API版本)

    步骤2:获取LatestCallerVersion并向验证API发送第二个请求,以查看我的客户是否在有效的服务区域。

    这就是我想出来的(但它崩溃了,烧得很厉害)

    caller_client = Savon.client(
      wsdl: 'https://alarmadmin.alarm.com/webservices/CustomerManagement.asmx?WSDL '\
    )
    caller_response = caller_client.call(
      :authentication,
      message: {
          user: 'xxxxxx',
          password: 'xxxxxx',
          two_factor_device_id: 'xxxxxx'
      },
      :body,
      message: {
          :get_latest_caller_version
      }
    )
    

    这是SOAP请求&GetLatestCaller调用的响应文档

    POST /webservices/CustomerManagement.asmx HTTP/1.1
    Host: alarmadmin.alarm.com
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: length
    
    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Header>
        <Authentication xmlns="http://www.alarm.com/WebServices">
          <User>string</User>
          <Password>string</Password>
          <TwoFactorDeviceId>string</TwoFactorDeviceId>
        </Authentication>
      </soap12:Header>
      <soap12:Body>
        <GetLatestCallerVersion xmlns="http://www.alarm.com/WebServices" />
      </soap12:Body>
    </soap12:Envelope>
    HTTP/1.1 200 OK
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: length
    
    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Body>
        <GetLatestCallerVersionResponse xmlns="http://www.alarm.com/WebServices">
          <GetLatestCallerVersionResult>int</GetLatestCallerVersionResult>
        </GetLatestCallerVersionResponse>
      </soap12:Body>
    </soap12:Envelope>
    

    这是CheckCaller_V2请求和;答复

    POST /webservices/Validate.asmx HTTP/1.1
    Host: alarmadmin.alarm.com
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: length
    
    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Header>
        <Authentication xmlns="http://www.alarm.com/WebServices">
          <User>string</User>
          <Password>string</Password>
          <TwoFactorDeviceId>string</TwoFactorDeviceId>
        </Authentication>
      </soap12:Header>
      <soap12:Body>
        <CheckCoverage_v2 xmlns="http://www.alarm.com/WebServices">
          <input>
            <Address>
              <Street1>string</Street1>
              <Street2>string</Street2>
              <SubCity>string</SubCity>
              <City>string</City>
              <SubState>string</SubState>
              <State>string</State>
              <Zip>string</Zip>
              <CountryId>Canada</CountryId>
            </Address>
            <Network>Gsm</Network>
            <Generation>FourG</Generation>
            <CallerVersion>returned from GetLatestCaller call</CallerVersion>
          </input>
        </CheckCoverage_v2>
      </soap12:Body>
    </soap12:Envelope>
    HTTP/1.1 200 OK
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: length
    
    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Body>
        <CheckCoverage_v2Response xmlns="http://www.alarm.com/WebServices">
          <CheckCoverage_v2Result>NotChecked or FullCoverage or MostlyCovered or NoCoverage or Error or BadZip or PartialCoverage or NotSupported or NotOffered</CheckCoverage_v2Result>
        </CheckCoverage_v2Response>
      </soap12:Body>
    </soap12:Envelope>
    

    我是SOAP的新手,非常感谢您的帮助。如果还需要什么,请告诉我。

    0 回复  |  直到 4 年前
        1
  •  1
  •   Arpan Lepcha    4 年前

    请使用SOAP UI检查Request定义。在SOAP UI中加载WSDL文件时,我能够得到以下内容。 Loading to SOAP UI

    在那之后,它变得相对容易遵循。似乎所有的操作都有相同的头,所以我将创建一个看起来像这样的客户端

    caller_client = Savon.client(
              wsdl: 'https://alarmadmin.alarm.com/webservices/CustomerManagement.asmx?WSDL',
              env_namespace: :soapenv,
              namespace_identifier: :web,
              soap_header: {
                'web:Authentication': {
                  'web:User': 'xxxx',
                  'web:Password': 'xxxxx',
                  'web:TwoFactorDeviceId': 'xxx'
                }
              })
    

    在那之后,调用单个操作就变成了

    caller_client.call(:get_latest_caller_version)
    

    如果你想列出所有可能的操作,你可以使用

    caller_client.operations
    

    如果要将消息传递给单个操作,请使用以下格式

       caller_client.call(:get_modem_serial_from_iccid, message: { iccid: 'xxxx' })