-
Notifications
You must be signed in to change notification settings - Fork 51
/
client.rb
233 lines (196 loc) · 7 KB
/
client.rb
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require 'httparty'
require 'dnsimple/extra'
require 'dnsimple/struct'
require 'dnsimple/response'
require 'dnsimple/client/clients'
module Dnsimple
# Client for the DNSimple API
#
# @see https://developer.dnsimple.com/
class Client
HEADER_AUTHORIZATION = "Authorization".freeze
WILDCARD_ACCOUNT = "_".freeze
# @return [String] The current API version.
API_VERSION = "v2".freeze
# Prepends the correct API version to +path+.
#
# @return [String] The versioned path.
def self.versioned(path)
File.join(API_VERSION, path)
end
# @!attribute username
# @see https://developer.dnsimple.com/v2/#authentication
# @return [String] DNSimple username for Basic Authentication
# @!attribute password
# @see https://developer.dnsimple.com/v2/#authentication
# @return [String] DNSimple password for Basic Authentication
# @!attribute domain_api_token
# @see https://developer.dnsimple.com/v2/#authentication
# @return [String] Domain API access token for authentication
# @!attribute access_token
# @see https://developer.dnsimple.com/v2/#authentication
# @return [String] Domain API access token for authentication
# @!attribute base_url
# @return [String] Base URL for API requests. (default: https://api.dnsimple.com/)
# @!attribute user_agent
# @return [String] Configure User-Agent header for requests.
# @!attribute proxy
# @return [String,nil] Configure address:port values for proxy server
attr_accessor :username, :password, :domain_api_token, :access_token,
:base_url, :user_agent, :proxy
def initialize(options = {})
defaults = Dnsimple::Default.options
Dnsimple::Default.keys.each do |key|
instance_variable_set(:"@#{key}", options[key] || defaults[key])
end
@services = {}
end
# @return [String] Base URL for API requests.
def base_url
Extra.join_uri(@base_url, "")
end
# Make a HTTP GET request.
#
# @param [String] path The path, relative to {#base_url}
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
def get(path, options = {})
execute :get, path, nil, options
end
# Make a HTTP POST request.
#
# @param [String] path The path, relative to {#base_url}
# @param [Hash] data The body for the request
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
def post(path, data = nil, options = {})
execute :post, path, data, options
end
# Make a HTTP PUT request.
#
# @param [String] path The path, relative to {#base_url}
# @param [Hash] data The body for the request
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
def put(path, data = nil, options = {})
execute :put, path, data, options
end
# Make a HTTP PATCH request.
#
# @param [String] path The path, relative to {#base_url}
# @param [Hash] data The body for the request
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
def patch(path, data = nil, options = {})
execute :patch, path, data, options
end
# Make a HTTP DELETE request.
#
# @param [String] path The path, relative to {#base_url}
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
def delete(path, data = nil, options = {})
execute :delete, path, data, options
end
# Executes a request, validates and returns the response.
#
# @param [String] method The HTTP method
# @param [String] path The path, relative to {#base_url}
# @param [Hash] data The body for the request
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
# @raise [RequestError]
# @raise [NotFoundError]
# @raise [AuthenticationFailed]
# @raise [TwoFactorAuthenticationRequired]
def execute(method, path, data = nil, options = {})
response = request(method, path, data, options)
case response.code
when 200..299
response
when 401
raise AuthenticationFailed, response["message"]
when 404
raise NotFoundError, response
else
raise RequestError, response
end
end
# Make a HTTP request.
#
# This method doesn't validate the response and never raise errors
# even in case of HTTP error codes, except for connection errors raised by
# the underlying HTTP client.
#
# Therefore, it's up to the caller to properly handle and validate the response.
#
# @param [String] method The HTTP method
# @param [String] path The path, relative to {#base_url}
# @param [Hash] data The body for the request
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
def request(method, path, data = nil, options = {})
request_options = request_options(options)
if data
request_options[:headers]["Content-Type"] = content_type(request_options[:headers])
request_options[:body] = content_data(request_options[:headers], data)
end
HTTParty.send(method, base_url + path, request_options)
end
private
def request_options(custom_options = {})
base_options.tap do |options|
Extra.deep_merge!(options, custom_options)
Extra.deep_merge!(options, headers: { "User-Agent" => format_user_agent })
add_auth_options!(options)
add_proxy_options!(options)
end
end
def base_options
{
format: :json,
headers: {
"Accept" => "application/json",
},
}
end
def add_proxy_options!(options)
if proxy
address, port = proxy.split(":")
options[:http_proxyaddr] = address
options[:http_proxyport] = port
end
end
def add_auth_options!(options)
if password
options[:basic_auth] = { username: username, password: password }
elsif access_token
options[:headers][HEADER_AUTHORIZATION] = "Bearer #{access_token}"
end
end
# Builds the final user agent to use for HTTP requests.
#
# If no custom user agent is provided, the default user agent is used.
#
# dnsimple-ruby/1.0
#
# If a custom user agent is provided, the final user agent is the combination
# of the custom user agent prepended by the default user agent.
#
# dnsimple-ruby/1.0 customAgentFlag
#
def format_user_agent
if user_agent.to_s.empty?
Dnsimple::Default::USER_AGENT
else
"#{Dnsimple::Default::USER_AGENT} #{user_agent}"
end
end
def content_type(headers)
headers["Content-Type"] || "application/json"
end
def content_data(headers, data)
headers["Content-Type"] == "application/json" ? JSON.dump(data) : data
end
end
end