<commit_before>#!/usr/bin/ruby
#
# Author:: <EMAIL> (<NAME>)
#
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
#
# License:: Licensed under the Apache License, Version 2.0 (the "License");
#           you may not use this file except in compliance with the License.
#           You may obtain a copy of the License at
#
#           http://www.apache.org/licenses/LICENSE-2.0
#
#           Unless required by applicable law or agreed to in writing, software
#           distributed under the License is distributed on an "AS IS" BASIS,
#           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
#           implied.
#           See the License for the specific language governing permissions and
#           limitations under the License.
#
# Base class for all generated API services based on Savon backend.

require 'httpi'
require 'savon'

module AdsCommon
  class SavonService
    # Default namespace name.
    DEFAULT_NAMESPACE = 'wsdl'

    # HTTP read timeout in seconds.
    HTTP_READ_TIMEOUT = 15 * 60

    attr_accessor :headerhandler
    attr_reader :api
    attr_reader :version
    attr_reader :namespace

    # Creates a new service.
    def initialize(api, endpoint, namespace, version)
      if self.class() == AdsCommon::SavonService
        raise NoMethodError, 'Tried to instantiate an abstract class'
      end
      @api, @version, @namespace = api, version, namespace
      @headerhandler = []
      @client = create_savon_client(endpoint, namespace)
    end

    private

    # Sets the logger in Savon-specific way. Also sets it for HTTPI used by it.
    def self.logger=(logger)
      Savon.configure do |config|
        config.log_level = :debug
        config.logger = logger
      end
      HTTPI.logger = logger
      HTTPI.log_level = :debug
    end

    # Returns ServiceRegistry for the current service. Has to be overridden.
    def get_service_registry()
      raise NoMethodError, 'This method needs to be overridden'
    end

    # Returns Module for the current service. Has to be overridden.
    def get_module()
      raise NoMethodError, 'This method needs to be overridden'
    end

    # Creates and sets up Savon client.
    def create_savon_client(endpoint, namespace)
      proxy = @api.config.read('connection.proxy')
      client = Savon::Client.new do |wsdl, http|
        wsdl.endpoint = endpoint
        wsdl.namespace = namespace
        http.proxy = proxy if !proxy.nil?
        http.read_timeout = HTTP_READ_TIMEOUT
      end
      return client
    end

    # Executes SOAP action specified as a string with given arguments.
    def execute_action(action_name, args)
      args = validate_args(action_name, args)
      response = execute_soap_request(action_name.to_sym, args)
      handle_errors(response)
      return extract_result(response, action_name)
    end

    # Executes the SOAP request with original SOAP name.
    def execute_soap_request(action, args)
      original_action_name =
          get_service_registry.get_method_signature(action)[:original_name]
      original_action_name = action if original_action_name.nil?
      response = @client.request(original_action_name) do |soap|
        set_headers(soap, args)
      end
      return response
    end

    # Validates input parameters to:
    # - add parameter names;
    # - resolve xsi:type where required;
    # - convert some native types to xml.
    def validate_args(action_name, args)
      validated_args = {}
      in_params = get_service_registry.get_method_signature(action_name)[:input]
      in_params.each_with_index do |in_param, index|
        key = in_param[:name]
        value = deep_copy(args[index])
        validated_args[key] = (value.nil?) ?
             nil : validate_arg(value, validated_args, key, in_param[:type])
      end
      return validated_args
    end

    # Validates method argument. Runs recursively if hash or array encountered.
    # Also handles some types that need special conversions.
    def validate_arg(arg, parent = nil, key = nil, field_type_name = nil)
      field_type = get_full_type_signature(field_type_name)
      result = case arg<commit_msg>Follow SOAP elements order for high level parameters too<commit_after>#!/usr/bin/ruby
#
# Author:: <EMAIL> (<NAME>)
#
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
#
# License:: Licensed under the Apache License, Version 2.0 (the "License");
#           you may not use this file except in compliance with the License.
#           You may obtain a copy of the License at
#
#           http://www.apache.org/licenses/LICENSE-2.0
#
#           Unless required by applicable law or agreed to in writing, software
#           distributed under the License is distributed on an "AS IS" BASIS,
#           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
#           implied.
#           See the License for the specific language governing permissions and
#           limitations under the License.
#
# Base class for all generated API services based on Savon backend.

require 'httpi'
require 'savon'

module AdsCommon
  class SavonService
    # Default namespace name.
    DEFAULT_NAMESPACE = 'wsdl'

    # HTTP read timeout in seconds.
    HTTP_READ_TIMEOUT = 15 * 60

    attr_accessor :headerhandler
    attr_reader :api
    attr_reader :version
    attr_reader :namespace

    # Creates a new service.
    def initialize(api, endpoint, namespace, version)
      if self.class() == AdsCommon::SavonService
        raise NoMethodError, 'Tried to instantiate an abstract class'
      end
      @api, @version, @namespace = api, version, namespace
      @headerhandler = []
      @client = create_savon_client(endpoint, namespace)
    end

    private

    # Sets the logger in Savon-specific way. Also sets it for HTTPI used by it.
    def self.logger=(logger)
      Savon.configure do |config|
        config.log_level = :debug
        config.logger = logger
      end
      HTTPI.logger = logger
      HTTPI.log_level = :debug
    end

    # Returns ServiceRegistry for the current service. Has to be overridden.
    def get_service_registry()
      raise NoMethodError, 'This method needs to be overridden'
    end

    # Returns Module for the current service. Has to be overridden.
    def get_module()
      raise NoMethodError, 'This method needs to be overridden'
    end

    # Creates and sets up Savon client.
    def create_savon_client(endpoint, namespace)
      proxy = @api.config.read('connection.proxy')
      client = Savon::Client.new do |wsdl, http|
        wsdl.endpoint = endpoint
        wsdl.namespace = namespace
        http.proxy = proxy if !proxy.nil?
        http.read_timeout = HTTP_READ_TIMEOUT
      end
      return client
    end

    # Executes SOAP action specified as a string with given arguments.
    def execute_action(action_name, args)
      args = validate_args(action_name, args)
      response = execute_soap_request(action_name.to_sym, args)
      handle_errors(response)
      return extract_result(response, action_name)
    end

    # Executes the SOAP request with original SOAP name.
    def execute_soap_request(action, args)
      original_action_name =
          get_service_registry.get_method_signature(action)[:original_name]
      original_action_name = action if original_action_name.nil?
      response = @client.request(original_action_name) do |soap|
        set_headers(soap, args)
      end
      return response
    end

    # Validates input parameters to:
    # - add parameter names;
    # - resolve xsi:type where required;
    # - convert some native types to XML.
    def validate_args(action_name, args)
      validated_args = {}
      in_params = get_service_registry.get_method_signature(action_name)[:input]
      in_params.each_with_index do |in_param, index|
        key = in_param[:name]
        value = deep_copy(args[index])
        validated_args[key] = (value.nil?) ?
             nil : validate_arg(value, validated_args, key, in_param[:type])
        # Adding :order! key to keep correct order in SOAP elements.
        validated_args[:order!] =
            generate_order_for_args(validated_args, in_params)
      end
      return validated_args
    end

    # Validates method argument. Runs recursively if hash or array encountered.
    # Also handles some types that need special conversions.
    def validate_arg(arg, parent = nil, key = nil, field_type_name = nil)
      field_type = get_full_type_signature(field_type_name)
      result = case arg
{'fraction_non_alphanumeric': 0.07249266862170088, 'fraction_numerical': 0.00281524926686217, 'mean_word_length': 2.84920993227991, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5000229', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2348, 'n_words': 798}