github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/ruby-client/api_client_faraday_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 ssl_options = { 7 :ca_file => @config.ssl_ca_file, 8 :verify => @config.ssl_verify, 9 :verify_mode => @config.ssl_verify_mode, 10 :client_cert => @config.ssl_client_cert, 11 :client_key => @config.ssl_client_key 12 } 13 14 connection = Faraday.new(:url => config.base_url, :ssl => ssl_options) do |conn| 15 conn.basic_auth(config.username, config.password) 16 if opts[:header_params]["Content-Type"] == "multipart/form-data" 17 conn.request :multipart 18 conn.request :url_encoded 19 end 20 conn.adapter(Faraday.default_adapter) 21 end 22 23 begin 24 response = connection.public_send(http_method.to_sym.downcase) do |req| 25 build_request(http_method, path, req, opts) 26 end 27 28 if @config.debugging 29 @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" 30 end 31 32 unless response.success? 33 if response.status == 0 34 # Errors from libcurl will be made visible here 35 fail ApiError.new(:code => 0, 36 :message => response.return_message) 37 else 38 fail ApiError.new(:code => response.status, 39 :response_headers => response.headers, 40 :response_body => response.body), 41 response.reason_phrase 42 end 43 end 44 rescue Faraday::TimeoutError 45 fail ApiError.new('Connection timed out') 46 end 47 48 if opts[:return_type] 49 data = deserialize(response, opts[:return_type]) 50 else 51 data = nil 52 end 53 return data, response.status, response.headers 54 end 55 56 # Builds the HTTP request 57 # 58 # @param [String] http_method HTTP method/verb (e.g. POST) 59 # @param [String] path URL path (e.g. /account/new) 60 # @option opts [Hash] :header_params Header parameters 61 # @option opts [Hash] :query_params Query parameters 62 # @option opts [Hash] :form_params Query parameters 63 # @option opts [Object] :body HTTP body (JSON/XML) 64 # @return [Typhoeus::Request] A Typhoeus Request 65 def build_request(http_method, path, request, opts = {}) 66 url = build_request_url(path) 67 http_method = http_method.to_sym.downcase 68 69 header_params = @default_headers.merge(opts[:header_params] || {}) 70 query_params = opts[:query_params] || {} 71 form_params = opts[:form_params] || {} 72 73 update_params_for_auth! header_params, query_params, opts[:auth_names] 74 75 req_opts = { 76 :method => http_method, 77 :headers => header_params, 78 :params => query_params, 79 :params_encoding => @config.params_encoding, 80 :timeout => @config.timeout, 81 :verbose => @config.debugging 82 } 83 84 if [:post, :patch, :put, :delete].include?(http_method) 85 req_body = build_request_body(header_params, form_params, opts[:body]) 86 req_opts.update :body => req_body 87 if @config.debugging 88 @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n" 89 end 90 end 91 request.headers = header_params 92 request.body = req_body 93 request.url url 94 request.params = query_params 95 download_file(request) if opts[:return_type] == 'File' 96 request 97 end 98 99 # Builds the HTTP request body 100 # 101 # @param [Hash] header_params Header parameters 102 # @param [Hash] form_params Query parameters 103 # @param [Object] body HTTP body (JSON/XML) 104 # @return [String] HTTP body data in the form of string 105 def build_request_body(header_params, form_params, body) 106 # http form 107 if header_params['Content-Type'] == 'application/x-www-form-urlencoded' 108 data = URI.encode_www_form(form_params) 109 elsif header_params['Content-Type'] == 'multipart/form-data' 110 data = {} 111 form_params.each do |key, value| 112 case value 113 when ::File, ::Tempfile 114 # TODO hardcode to application/octet-stream, need better way to detect content type 115 data[key] = Faraday::UploadIO.new(value.path, 'application/octet-stream', value.path) 116 when ::Array, nil 117 # let Faraday handle Array and nil parameters 118 data[key] = value 119 when ::Hash 120 data[key] = build_request_body(header_params, value, body) 121 else 122 data[key] = value.to_s 123 end 124 end 125 elsif body 126 data = body.is_a?(String) ? body : body.to_json 127 else 128 data = nil 129 end 130 data 131 end