如何在 SOAP 操作请求中添加可选参数



我想在soap_action(wash_out)请求中添加可选的参数,这是代码

soap_action "DeActivate_VAS_Request",
:args => {:some_arguments}, :return => {:return_values}

和操作是

<operation name="DeActivate_VAS_Request">
   <soap:operation soapAction="DeActivate_VAS_Request"/>
     <input>
       <soap:body
             use="encoded"
             encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
             namespace="urn:WashOut"/>
     </input>
   <output>
     <soap:body use="encoded"
                encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
                namespace="urn:WashOut"/>
   </output>
 </operation>

操作应该是可选的,请帮助。

你可以猴

子补丁wash_out 0.5.2。把它放在config/initializers/washout_monkey_patch.rb

WashOut::Param.class_eval do
    attr_accessor :optional
    def initialize(name, type, multiplied = false, optional = false)
      type ||= {}
      @name       = name.to_s
      @raw_name   = name.to_s
      @map        = {}
      @multiplied = multiplied
      @optional   = optional
      if WashOut::Engine.camelize_wsdl.to_s == 'lower'
        @name = @name.camelize(:lower)
      elsif WashOut::Engine.camelize_wsdl
        @name = @name.camelize
      end
      if type.is_a?(Symbol)
        @type = type.to_s
      elsif type.is_a?(Class)
        @type         = 'struct'
        @map          = self.class.parse_def(type.wash_out_param_map)
        @source_class = type
      else
        @type = 'struct'
        @map  = self.class.parse_def(type)
      end
    end
    def flat_copy
      copy = self.class.new(@name, @type.to_sym, @multiplied, @optional)
      copy.raw_name = raw_name
      copy
    end
end
WashOutHelper.module_eval do
  def wsdl_occurence(param, inject, extend_with = {})
    if param.multiplied
      data = {
        "#{'xsi:' if inject}minOccurs" => 0,
        "#{'xsi:' if inject}maxOccurs" => 'unbounded'
      }
    elsif param.optional
      data = {
        "#{'xsi:' if inject}minOccurs" => 0,
      }
    else
      data = {}
    end
    extend_with.merge(data)
  end
  def wsdl_data(xml, params)
    Rails.logger.debug params
    params.each do |param|
      tag_name = param.name
      if !param.struct?
        if param.multiplied
          param.value = [] unless param.value.is_a?(Array)
          param.value.each do |v|
            xml.tag! tag_name, v, "xsi:type" => param.namespaced_type
          end
        elsif param.optional
          if !param.value.nil?
            xml.tag! tag_name, param.value, "xsi:type" => param.namespaced_type
          end
        else
          xml.tag! tag_name, param.value, "xsi:type" => param.namespaced_type
        end
      else
        if param.multiplied
          param.map.each do |p|
            xml.tag! tag_name, "xsi:type" => param.namespaced_type do
              wsdl_data(xml, p.map)
            end
          end
        elsif param.optional
          if !param.map.empty?
              xml.tag! tag_name, "xsi:type" => param.namespaced_type do
                wsdl_data(xml, param.map)
              end
          end
        else
          xml.tag! tag_name, "xsi:type" => param.namespaced_type do
            wsdl_data(xml, param.map)
          end
        end
      end
    end
  end
end

若要定义可选参数,请使用 WashOut::P aram 构造函数,传递第三个参数等于 false 和第四个参数等于 true 。在以下示例中,第三个输入参数和第二个输出参数将被定义为可选参数:

  soap_action "YourWebService", 
    :args => {
      :arg1    => :string,
      :arg2   => :string,
      :arg3  => WashOut::Param.new(:arg3, :string, false, true),
    },
    :return => {
      :arg1 => :string,
      :arg2 => WashOut::Param.new(:arg2, :string, false, true),
    },
    :to     => :web_service

最新更新