|
| 1 | +=begin |
| 2 | +{{> api_info}} |
| 3 | + |
| 4 | +=end |
| 5 | + |
| 6 | +require 'date' |
| 7 | +require 'json' |
| 8 | +require 'logger' |
| 9 | +require 'tempfile' |
| 10 | +require 'time' |
| 11 | +{{#isTyphoeus}} |
| 12 | +require 'typhoeus' |
| 13 | +{{/isTyphoeus}} |
| 14 | +{{#isFaraday}} |
| 15 | +require 'faraday' |
| 16 | +require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0') |
| 17 | +require 'marcel' |
| 18 | +{{/isFaraday}} |
| 19 | +{{#isHttpx}} |
| 20 | +require 'httpx' |
| 21 | +require 'net/http/status' |
| 22 | +{{/isHttpx}} |
| 23 | + |
| 24 | + |
| 25 | +module {{moduleName}} |
| 26 | + class ApiClient |
| 27 | + # The Configuration object holding settings to be used in the API client. |
| 28 | + attr_accessor :config |
| 29 | + |
| 30 | + # Defines the headers to be used in HTTP requests of all API calls by default. |
| 31 | + # |
| 32 | + # @return [Hash] |
| 33 | + attr_accessor :default_headers |
| 34 | + |
| 35 | + # Initializes the ApiClient |
| 36 | + # @option config [Configuration] Configuration for initializing the object, default to Configuration.default |
| 37 | + def initialize(config = Configuration.default) |
| 38 | + @config = config |
| 39 | + @user_agent = "{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/#{VERSION}/ruby{{/httpUserAgent}}" |
| 40 | + @default_headers = { |
| 41 | + 'Content-Type' => 'application/json', |
| 42 | + 'User-Agent' => @user_agent |
| 43 | + } |
| 44 | + end |
| 45 | + |
| 46 | + def self.default |
| 47 | + @@default ||= ApiClient.new |
| 48 | + end |
| 49 | + |
| 50 | +{{#isTyphoeus}} |
| 51 | +{{> api_client_typhoeus_partial}} |
| 52 | + |
| 53 | +{{/isTyphoeus}} |
| 54 | +{{#isFaraday}} |
| 55 | +{{> api_client_faraday_partial}} |
| 56 | + |
| 57 | +{{/isFaraday}} |
| 58 | +{{#isHttpx}} |
| 59 | +{{> api_client_httpx_partial}} |
| 60 | + |
| 61 | +{{/isHttpx}} |
| 62 | + # Check if the given MIME is a JSON MIME. |
| 63 | + # JSON MIME examples: |
| 64 | + # application/json |
| 65 | + # application/json; charset=UTF8 |
| 66 | + # APPLICATION/JSON |
| 67 | + # */* |
| 68 | + # @param [String] mime MIME |
| 69 | + # @return [Boolean] True if the MIME is application/json |
| 70 | + def json_mime?(mime) |
| 71 | + (mime == '*/*') || !(mime =~ /^Application\/.*json(?!p)(;.*)?/i).nil? |
| 72 | + end |
| 73 | + |
| 74 | + # Deserialize the response to the given return type. |
| 75 | + # |
| 76 | + # @param [Response] response HTTP response |
| 77 | + # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>" |
| 78 | + def deserialize(response, return_type) |
| 79 | + body = response.body |
| 80 | + return nil if body.nil? || body.empty? |
| 81 | + |
| 82 | + # return response body directly for String return type |
| 83 | + return body.to_s if return_type == 'String' |
| 84 | + |
| 85 | + # ensuring a default content type |
| 86 | + content_type = response.headers['Content-Type'] || 'application/json' |
| 87 | + |
| 88 | + fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type) |
| 89 | + |
| 90 | + begin |
| 91 | + data = JSON.parse("[#{body}]", :symbolize_names => true)[0] |
| 92 | + rescue JSON::ParserError => e |
| 93 | + if %w(String Date Time).include?(return_type) |
| 94 | + data = body |
| 95 | + else |
| 96 | + raise e |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + convert_to_type data, return_type |
| 101 | + end |
| 102 | + |
| 103 | + # Convert data to the given return type. |
| 104 | + # @param [Object] data Data to be converted |
| 105 | + # @param [String] return_type Return type |
| 106 | + # @return [Mixed] Data in a particular type |
| 107 | + def convert_to_type(data, return_type) |
| 108 | + return nil if data.nil? |
| 109 | + case return_type |
| 110 | + when 'String' |
| 111 | + data.to_s |
| 112 | + when 'Integer' |
| 113 | + data.to_i |
| 114 | + when 'Float' |
| 115 | + data.to_f |
| 116 | + when 'Boolean' |
| 117 | + data == true |
| 118 | + when 'Time' |
| 119 | + # parse date time (expecting ISO 8601 format) |
| 120 | + Time.parse data |
| 121 | + when 'Date' |
| 122 | + # parse date time (expecting ISO 8601 format) |
| 123 | + Date.parse data |
| 124 | + when 'Object' |
| 125 | + # generic object (usually a Hash), return directly |
| 126 | + data |
| 127 | + when /\AArray<(.+)>\z/ |
| 128 | + # e.g. Array<Pet> |
| 129 | + sub_type = $1 |
| 130 | + data.map { |item| convert_to_type(item, sub_type) } |
| 131 | + when /\AHash\<String, (.+)\>\z/ |
| 132 | + # e.g. Hash<String, Integer> |
| 133 | + sub_type = $1 |
| 134 | + {}.tap do |hash| |
| 135 | + data.each { |k, v| hash[k] = convert_to_type(v, sub_type) } |
| 136 | + end |
| 137 | + else |
| 138 | + # models (e.g. Pet) or oneOf/anyOf |
| 139 | + klass = {{moduleName}}.const_get(return_type) |
| 140 | + if klass.respond_to?(:openapi_one_of) || klass.respond_to?(:openapi_any_of) |
| 141 | + klass.build(data) |
| 142 | + else |
| 143 | + klass.build_from_hash(data) |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + # Sanitize filename by removing path. |
| 149 | + # e.g. ../../sun.gif becomes sun.gif |
| 150 | + # |
| 151 | + # @param [String] filename the filename to be sanitized |
| 152 | + # @return [String] the sanitized filename |
| 153 | + def sanitize_filename(filename) |
| 154 | + filename.split(/[\/\\]/).last |
| 155 | + end |
| 156 | + |
| 157 | + def build_request_url(path, opts = {}) |
| 158 | + # Add leading and trailing slashes to path |
| 159 | + path = "/#{path}".gsub(/\/+/, '/') |
| 160 | + @config.base_url(opts[:operation]) + path |
| 161 | + end |
| 162 | + |
| 163 | + # Update header and query params based on authentication settings. |
| 164 | + # |
| 165 | + # @param [Hash] header_params Header parameters |
| 166 | + # @param [Hash] query_params Query parameters |
| 167 | + # @param [String] auth_names Authentication scheme name |
| 168 | + def update_params_for_auth!(header_params, query_params, auth_names) |
| 169 | + Array(auth_names).each do |auth_name| |
| 170 | + auth_setting = @config.auth_settings[auth_name] |
| 171 | + next unless auth_setting |
| 172 | + case auth_setting[:in] |
| 173 | + when 'header' then header_params[auth_setting[:key]] = auth_setting[:value] unless auth_setting[:value].nil? |
| 174 | + when 'query' then query_params[auth_setting[:key]] = auth_setting[:value] unless auth_setting[:value].nil? |
| 175 | + else fail ArgumentError, 'Authentication token must be in `query` or `header`' |
| 176 | + end |
| 177 | + end |
| 178 | + end |
| 179 | + |
| 180 | + # Sets user agent in HTTP header |
| 181 | + # |
| 182 | + # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0) |
| 183 | + def user_agent=(user_agent) |
| 184 | + @user_agent = user_agent |
| 185 | + @default_headers['User-Agent'] = @user_agent |
| 186 | + end |
| 187 | + |
| 188 | + # Return Accept header based on an array of accepts provided. |
| 189 | + # @param [Array] accepts array for Accept |
| 190 | + # @return [String] the Accept header (e.g. application/json) |
| 191 | + def select_header_accept(accepts) |
| 192 | + return nil if accepts.nil? || accepts.empty? |
| 193 | + # use JSON when present, otherwise use all of the provided |
| 194 | + json_accept = accepts.find { |s| json_mime?(s) } |
| 195 | + json_accept || accepts.join(',') |
| 196 | + end |
| 197 | + |
| 198 | + # Return Content-Type header based on an array of content types provided. |
| 199 | + # @param [Array] content_types array for Content-Type |
| 200 | + # @return [String] the Content-Type header (e.g. application/json) |
| 201 | + def select_header_content_type(content_types) |
| 202 | + # return nil by default |
| 203 | + return if content_types.nil? || content_types.empty? |
| 204 | + # use JSON when present, otherwise use the first one |
| 205 | + json_content_type = content_types.find { |s| json_mime?(s) } |
| 206 | + json_content_type || content_types.first |
| 207 | + end |
| 208 | + |
| 209 | + # Convert object (array, hash, object, etc) to JSON string. |
| 210 | + # @param [Object] model object to be converted into JSON string |
| 211 | + # @return [String] JSON string representation of the object |
| 212 | + def object_to_http_body(model) |
| 213 | + return model if model.nil? || model.is_a?(String) |
| 214 | + local_body = nil |
| 215 | + if model.is_a?(Array) |
| 216 | + local_body = model.map { |m| object_to_hash(m) } |
| 217 | + else |
| 218 | + local_body = object_to_hash(model) |
| 219 | + end |
| 220 | + local_body.to_json |
| 221 | + end |
| 222 | + |
| 223 | + # Convert object(non-array) to hash. |
| 224 | + # @param [Object] obj object to be converted into JSON string |
| 225 | + # @return [String] JSON string representation of the object |
| 226 | + def object_to_hash(obj) |
| 227 | + if obj.respond_to?(:to_hash) |
| 228 | + obj.to_hash |
| 229 | + else |
| 230 | + obj |
| 231 | + end |
| 232 | + end |
| 233 | + |
| 234 | + # Build parameter value according to the given collection format. |
| 235 | + # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi |
| 236 | + def build_collection_param(param, collection_format) |
| 237 | + case collection_format |
| 238 | + when :csv |
| 239 | + param.join(',') |
| 240 | + when :ssv |
| 241 | + param.join(' ') |
| 242 | + when :tsv |
| 243 | + param.join("\t") |
| 244 | + when :pipes |
| 245 | + param.join('|') |
| 246 | + when :multi |
| 247 | + # return the array directly as typhoeus will handle it as expected |
| 248 | + param |
| 249 | + else |
| 250 | + fail "unknown collection format: #{collection_format.inspect}" |
| 251 | + end |
| 252 | + end |
| 253 | + end |
| 254 | +end |
0 commit comments