github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/ruby-client/api_client_typhoeus_partial.mustache (about)

     1      # Call an API with given options.
     2      #
     3      # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
     4      #   the data deserialized from response body (could be nil), response status code and response headers.
     5      def call_api(http_method, path, opts = {})
     6        request = build_request(http_method, path, opts)
     7        response = request.run
     8  
     9        if @config.debugging
    10          @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
    11        end
    12  
    13        unless response.success?
    14          if response.timed_out?
    15            fail ApiError.new('Connection timed out')
    16          elsif response.code == 0
    17            # Errors from libcurl will be made visible here
    18            fail ApiError.new(:code => 0,
    19                              :message => response.return_message)
    20          else
    21            fail ApiError.new(:code => response.code,
    22                              :response_headers => response.headers,
    23                              :response_body => response.body),
    24                 response.status_message
    25          end
    26        end
    27  
    28        if opts[:return_type]
    29          data = deserialize(response, opts[:return_type])
    30        else
    31          data = nil
    32        end
    33        return data, response.code, response.headers
    34      end
    35  
    36      # Builds the HTTP request
    37      #
    38      # @param [String] http_method HTTP method/verb (e.g. POST)
    39      # @param [String] path URL path (e.g. /account/new)
    40      # @option opts [Hash] :header_params Header parameters
    41      # @option opts [Hash] :query_params Query parameters
    42      # @option opts [Hash] :form_params Query parameters
    43      # @option opts [Object] :body HTTP body (JSON/XML)
    44      # @return [Typhoeus::Request] A Typhoeus Request
    45      def build_request(http_method, path, opts = {})
    46        url = build_request_url(path)
    47        http_method = http_method.to_sym.downcase
    48  
    49        header_params = @default_headers.merge(opts[:header_params] || {})
    50        query_params = opts[:query_params] || {}
    51        form_params = opts[:form_params] || {}
    52  
    53        {{#hasAuthMethods}}
    54        update_params_for_auth! header_params, query_params, opts[:auth_names]
    55        {{/hasAuthMethods}}
    56  
    57        # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
    58        _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
    59  
    60        req_opts = {
    61          :method => http_method,
    62          :headers => header_params,
    63          :params => query_params,
    64          :params_encoding => @config.params_encoding,
    65          :timeout => @config.timeout,
    66          :ssl_verifypeer => @config.verify_ssl,
    67          :ssl_verifyhost => _verify_ssl_host,
    68          :sslcert => @config.cert_file,
    69          :sslkey => @config.key_file,
    70          :verbose => @config.debugging
    71        }
    72  
    73        # set custom cert, if provided
    74        req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
    75  
    76        if [:post, :patch, :put, :delete].include?(http_method)
    77          req_body = build_request_body(header_params, form_params, opts[:body])
    78          req_opts.update :body => req_body
    79          if @config.debugging
    80            @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
    81          end
    82        end
    83  
    84        request = Typhoeus::Request.new(url, req_opts)
    85        download_file(request) if opts[:return_type] == 'File'
    86        request
    87      end
    88  
    89      # Builds the HTTP request body
    90      #
    91      # @param [Hash] header_params Header parameters
    92      # @param [Hash] form_params Query parameters
    93      # @param [Object] body HTTP body (JSON/XML)
    94      # @return [String] HTTP body data in the form of string
    95      def build_request_body(header_params, form_params, body)
    96        # http form
    97        if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
    98            header_params['Content-Type'] == 'multipart/form-data'
    99          data = {}
   100          form_params.each do |key, value|
   101            case value
   102            when ::File, ::Tempfile, ::Array, nil
   103              # let typhoeus handle File, Tempfile, Array and nil parameters
   104              data[key] = value
   105            when ::Hash
   106              data[key] = build_request_body(header_params, value, body)
   107            else
   108              data[key] = value.to_s
   109            end
   110          end
   111        elsif body
   112          data = body.is_a?(String) ? body : body.to_json
   113        else
   114          data = nil
   115        end
   116        data
   117      end