-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.rb
125 lines (102 loc) · 3.31 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
require 'esa/errors'
require 'esa/api_methods'
require "esa/response"
module Esa
class Client
class TooManyRequestError < StandardError; end
include ApiMethods
def initialize(access_token: nil, api_endpoint: nil, current_team: nil, default_headers: {}, retry_on_rate_limit_exceeded: true)
@access_token = access_token
@api_endpoint = api_endpoint
@current_team = current_team
@default_headers = default_headers
@retry_on_rate_limit_exceeded = retry_on_rate_limit_exceeded
end
attr_accessor :current_team, :default_headers, :retry_on_rate_limit_exceeded
def current_team!
raise TeamNotSpecifiedError, "current_team is not specified" unless @current_team
current_team
end
def send_get(path, params = nil, headers = nil)
send_request(:get, path, params, headers)
end
def send_post(path, params = nil, headers = nil)
send_request(:post, path, params, headers)
end
def send_put(path, params = nil, headers = nil)
send_request(:put, path, params, headers)
end
def send_patch(path, params = nil, headers = nil)
send_request(:patch, path, params, headers)
end
def send_delete(path, params = nil, headers = nil)
send_request(:delete, path, params, headers)
end
def send_request(method, path, params = nil, headers = nil)
response = esa_connection.send(method, path, params, headers)
raise TooManyRequestError if retry_on_rate_limit_exceeded && response.status == 429 # too_many_requests
Esa::Response.new(response)
rescue TooManyRequestError
wait_sec = response.headers['retry-after'].to_i + 5
puts "Rate limit exceeded: will retry after #{wait_sec} seconds."
wait_for(wait_sec)
retry
end
def send_s3_request(method, path, params = nil, headers = nil)
Esa::Response.new(s3_connection.send(method, path, params, headers))
end
def send_simple_request(method, path, params = nil, headers = nil)
Esa::Response.new(simple_connection.send(method, path, params, headers))
end
def esa_connection
@esa_connection ||= Faraday.new(faraday_options) do |c|
c.request :json
c.response :json
c.adapter Faraday.default_adapter
end
end
def s3_connection
@s3_connection ||= Faraday.new do |c|
c.request :multipart
c.request :url_encoded
c.response :xml
c.adapter Faraday.default_adapter
end
end
def simple_connection
@simple_connection ||= Faraday.new do |c|
c.adapter Faraday.default_adapter
end
end
private
def faraday_options
{ url: api_url, headers: api_headers }
end
def api_headers
headers = {
'Accept' => 'application/json',
'User-Agent' => "Esa Ruby Gem #{Esa::VERSION}"
}.merge(default_headers)
if access_token
headers.merge(Authorization: "Bearer #{access_token}")
else
headers
end
end
def access_token
@access_token || ENV['ESA_ACCESS_TOKEN']
end
def api_url
@api_endpoint || ENV['ESA_API_ENDPOINT'] || 'https://api.esa.io'
end
def wait_for(wait_sec)
return if wait_sec <= 0
(wait_sec / 10).times do
print '.'
sleep 10
end
sleep wait_sec % 10
puts
end
end
end