-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathapi_client.mustache
132 lines (113 loc) · 3.7 KB
/
api_client.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
=begin
{{> api_info}}
=end
require 'base64'
require 'json'
require 'excon'
module {{moduleName}}
class ApiClient
def initialize(config = {})
@host = "{{{basePath}}}"
@user_agent = "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/#{VERSION}/ruby{{/httpUserAgent}}"
set_config(config)
end
def self.default
@@default ||= ApiClient.new
end
def get_server_from_api_key(api_key = '')
begin
split = api_key.split('-')
server = 'invalid-server'
if split.length == 2
server = split[1]
end
server
rescue
""
end
end
def set_config(config = {})
@api_key = config[:api_key] || ''
@is_basic_auth = @api_key.to_s.strip.empty? == false
@access_token = config[:access_token] || ''
@is_oauth = @access_token.to_s.strip.empty? == false
# If using Basic auth and no server is provided,
# attempt to extract it from the api_key directy.
@server = config[:server] || 'invalid-server'
if @server == 'invalid-server' && @is_basic_auth
@server = get_server_from_api_key(@api_key)
end
timeout = config[:timeout] || 120
@write_timeout = config[:write_timeout] || timeout
@read_timeout = config[:read_timeout] || timeout
@connect_timeout = config[:connect_timeout] || timeout
end
def call_api(http_method, path, opts = {})
headers = {'Content-Type' => "application/json"}
headers[:Authorization] = "Basic #{encoded_basic_token(@api_key)}" if @is_basic_auth
headers[:Authorization] = "Bearer #@access_token" if @is_oauth
host = @server.length > 0 ? @host.sub('server', @server) : @host
conn = Excon.new(host + path,
:headers => headers,
:read_timeout => @read_timeout,
:write_timeout => @write_timeout,
:connect_timeout => @connect_timeout,
:middlewares => [Excon.defaults[:middlewares], Excon::Middleware::Decompress].flatten)
res = nil
case http_method.to_sym.downcase
when :post, :put, :patch, :delete
res = conn.request(:method => http_method, :query => opts[:query_params], :body => opts[:body])
when :get
res = conn.get(:query => opts[:query_params])
end
data = nil
data = JSON.parse(res.body) if res.status == 200
data = "success" if res.status == 204
if (!data)
fail ApiError.new(:status => res.status, :response_body => res.body)
end
return data
end
# Convert object (array, hash, object, etc) to JSON string.
def object_to_http_body(model)
return model if model.nil? || model.is_a?(String)
local_body = nil
if model.is_a?(Array)
local_body = model.map { |m| object_to_hash(m) }
else
local_body = object_to_hash(model)
end
local_body.to_json
end
# Convert object(non-array) to hash.
def object_to_hash(obj)
if obj.respond_to?(:to_hash)
obj.to_hash
else
obj
end
end
# Build parameter value according to the given collection format.
def build_collection_param(param, collection_format)
case collection_format
when :csv
param.join(',')
when :ssv
param.join(' ')
when :tsv
param.join("\t")
when :pipes
param.join('|')
when :multi
param
else
fail "unknown collection format: #{collection_format.inspect}"
end
end
private
# Build base64 encoded token for basic auth.
def encoded_basic_token(api_key)
Base64.urlsafe_encode64("user:#{api_key}")
end
end
end