github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/ruby-client/configuration.mustache (about) 1 module {{moduleName}} 2 class Configuration 3 # Defines url scheme 4 attr_accessor :scheme 5 6 # Defines url host 7 attr_accessor :host 8 9 # Defines url base path 10 attr_accessor :base_path 11 12 # Defines API keys used with API Key authentications. 13 # 14 # @return [Hash] key: parameter name, value: parameter value (API key) 15 # 16 # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string) 17 # config.api_key['api_key'] = 'xxx' 18 attr_accessor :api_key 19 20 # Defines API key prefixes used with API Key authentications. 21 # 22 # @return [Hash] key: parameter name, value: API key prefix 23 # 24 # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers) 25 # config.api_key_prefix['api_key'] = 'Token' 26 attr_accessor :api_key_prefix 27 28 # Defines the username used with HTTP basic authentication. 29 # 30 # @return [String] 31 attr_accessor :username 32 33 # Defines the password used with HTTP basic authentication. 34 # 35 # @return [String] 36 attr_accessor :password 37 38 # Defines the access token (Bearer) used with OAuth2. 39 attr_accessor :access_token 40 41 # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response 42 # details will be logged with `logger.debug` (see the `logger` attribute). 43 # Default to false. 44 # 45 # @return [true, false] 46 attr_accessor :debugging 47 48 # Defines the logger used for debugging. 49 # Default to `Rails.logger` (when in Rails) or logging to STDOUT. 50 # 51 # @return [#debug] 52 attr_accessor :logger 53 54 # Defines the temporary folder to store downloaded files 55 # (for API endpoints that have file response). 56 # Default to use `Tempfile`. 57 # 58 # @return [String] 59 attr_accessor :temp_folder_path 60 61 # The time limit for HTTP request in seconds. 62 # Default to 0 (never times out). 63 attr_accessor :timeout 64 65 # Set this to false to skip client side validation in the operation. 66 # Default to true. 67 # @return [true, false] 68 attr_accessor :client_side_validation 69 70 {{^isFaraday}} 71 {{> configuration_tls_typhoeus_partial}} 72 {{/isFaraday}} 73 {{#isFaraday}} 74 {{> configuration_tls_faraday_partial}} 75 {{/isFaraday}} 76 # Set this to customize parameters encoding of array parameter with multi collectionFormat. 77 # Default to nil. 78 # 79 # @see The params_encoding option of Ethon. Related source code: 80 # https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96 81 attr_accessor :params_encoding 82 83 attr_accessor :inject_format 84 85 attr_accessor :force_ending_format 86 87 def initialize 88 @scheme = '{{scheme}}' 89 @host = '{{host}}' 90 @base_path = '{{contextPath}}' 91 @api_key = {} 92 @api_key_prefix = {} 93 @timeout = 0 94 @client_side_validation = true 95 {{#isFaraday}} 96 @ssl_verify = true 97 @ssl_verify_mode = nil 98 @ssl_ca_file = nil 99 @ssl_client_cert = nil 100 @ssl_client_key = nil 101 {{/isFaraday}} 102 {{^isFaraday}} 103 @verify_ssl = true 104 @verify_ssl_host = true 105 @params_encoding = nil 106 @cert_file = nil 107 @key_file = nil 108 {{/isFaraday}} 109 @debugging = false 110 @inject_format = false 111 @force_ending_format = false 112 @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) 113 114 yield(self) if block_given? 115 end 116 117 # The default Configuration object. 118 def self.default 119 @@default ||= Configuration.new 120 end 121 122 def configure 123 yield(self) if block_given? 124 end 125 126 def scheme=(scheme) 127 # remove :// from scheme 128 @scheme = scheme.sub(/:\/\//, '') 129 end 130 131 def host=(host) 132 # remove http(s):// and anything after a slash 133 @host = host.sub(/https?:\/\//, '').split('/').first 134 end 135 136 def base_path=(base_path) 137 # Add leading and trailing slashes to base_path 138 @base_path = "/#{base_path}".gsub(/\/+/, '/') 139 @base_path = '' if @base_path == '/' 140 end 141 142 def base_url 143 "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') 144 end 145 146 # Gets API key (with prefix if set). 147 # @param [String] param_name the parameter name of API key auth 148 def api_key_with_prefix(param_name) 149 if @api_key_prefix[param_name] 150 "#{@api_key_prefix[param_name]} #{@api_key[param_name]}" 151 else 152 @api_key[param_name] 153 end 154 end 155 156 # Gets Basic Auth token string 157 def basic_auth_token 158 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n") 159 end 160 161 # Returns Auth Settings hash for api client. 162 def auth_settings 163 { 164 {{#authMethods}} 165 {{#isApiKey}} 166 '{{name}}' => 167 { 168 type: 'api_key', 169 in: {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, 170 key: '{{keyParamName}}', 171 value: api_key_with_prefix('{{keyParamName}}') 172 }, 173 {{/isApiKey}} 174 {{#isBasic}} 175 {{#isBasicBasic}} 176 '{{name}}' => 177 { 178 type: 'basic', 179 in: 'header', 180 key: 'Authorization', 181 value: basic_auth_token 182 }, 183 {{/isBasicBasic}} 184 {{#isBasicBearer}} 185 '{{name}}' => 186 { 187 type: 'bearer', 188 in: 'header', 189 {{#bearerFormat}} 190 format: '{{{.}}}', 191 {{/bearerFormat}} 192 key: 'Authorization', 193 value: "Bearer #{access_token}" 194 }, 195 {{/isBasicBearer}} 196 {{/isBasic}} 197 {{#isOAuth}} 198 '{{name}}' => 199 { 200 type: 'oauth2', 201 in: 'header', 202 key: 'Authorization', 203 value: "Bearer #{access_token}" 204 }, 205 {{/isOAuth}} 206 {{/authMethods}} 207 } 208 end 209 210 # Returns an array of Server setting 211 def server_settings 212 [ 213 {{#servers}} 214 { 215 url: "{{{url}}}", 216 description: "{{{description}}}{{^description}}No description provided{{/description}}", 217 {{#variables}} 218 {{#-first}} 219 variables: { 220 {{/-first}} 221 {{{name}}}: { 222 description: "{{{description}}}{{^description}}No description provided{{/description}}", 223 default_value: "{{{defaultValue}}}", 224 {{#enumValues}} 225 {{#-first}} 226 enum_values: [ 227 {{/-first}} 228 "{{{.}}}"{{^-last}},{{/-last}} 229 {{#-last}} 230 ] 231 {{/-last}} 232 {{/enumValues}} 233 }{{^-last}},{{/-last}} 234 {{#-last}} 235 } 236 {{/-last}} 237 {{/variables}} 238 }{{^-last}},{{/-last}} 239 {{/servers}} 240 ] 241 end 242 243 # Returns URL based on server settings 244 # 245 # @param index array index of the server settings 246 # @param variables hash of variable and the corresponding value 247 def server_url(index, variables = {}) 248 servers = server_settings 249 250 # check array index out of bound 251 if (index < 0 || index >= servers.size) 252 fail ArgumentError, "Invalid index #{index} when selecting the server. Must be less than #{servers.size}" 253 end 254 255 server = servers[index] 256 url = server[:url] 257 258 # go through variable and assign a value 259 server[:variables].each do |name, variable| 260 if variables.key?(name) 261 if (server[:variables][name][:enum_values].include? variables[name]) 262 url.gsub! "{" + name.to_s + "}", variables[name] 263 else 264 fail ArgumentError, "The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}." 265 end 266 else 267 # use default value 268 url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] 269 end 270 end 271 272 url 273 end 274 end 275 end