class Esa::Client

Attributes

current_team[RW]
default_headers[RW]
retry_on_rate_limit_exceeded[RW]

Public Class Methods

new(access_token: nil, api_endpoint: nil, current_team: nil, default_headers: {}, retry_on_rate_limit_exceeded: true) click to toggle source
# File lib/esa/client.rb, line 11
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

Public Instance Methods

current_team!() click to toggle source
# File lib/esa/client.rb, line 20
def current_team!
  raise TeamNotSpecifiedError, "current_team is not specified" unless @current_team
  current_team
end
esa_connection() click to toggle source
# File lib/esa/client.rb, line 64
def esa_connection
  @esa_connection ||= Faraday.new(faraday_options) do |c|
    c.request :json
    c.response :json
    c.adapter Faraday.default_adapter
  end
end
s3_connection() click to toggle source
# File lib/esa/client.rb, line 72
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
send_delete(path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 41
def send_delete(path, params = nil, headers = nil)
  send_request(:delete, path, params, headers)
end
send_get(path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 25
def send_get(path, params = nil, headers = nil)
  send_request(:get, path, params, headers)
end
send_patch(path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 37
def send_patch(path, params = nil, headers = nil)
  send_request(:patch, path, params, headers)
end
send_post(path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 29
def send_post(path, params = nil, headers = nil)
  send_request(:post, path, params, headers)
end
send_put(path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 33
def send_put(path, params = nil, headers = nil)
  send_request(:put, path, params, headers)
end
send_request(method, path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 45
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
send_s3_request(method, path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 56
def send_s3_request(method, path, params = nil, headers = nil)
  Esa::Response.new(s3_connection.send(method, path, params, headers))
end
send_simple_request(method, path, params = nil, headers = nil) click to toggle source
# File lib/esa/client.rb, line 60
def send_simple_request(method, path, params = nil, headers = nil)
  Esa::Response.new(simple_connection.send(method, path, params, headers))
end
simple_connection() click to toggle source
# File lib/esa/client.rb, line 81
def simple_connection
  @simple_connection ||= Faraday.new do |c|
    c.adapter Faraday.default_adapter
  end
end

Private Instance Methods

access_token() click to toggle source
# File lib/esa/client.rb, line 106
def access_token
  @access_token || ENV['ESA_ACCESS_TOKEN']
end
api_headers() click to toggle source
# File lib/esa/client.rb, line 93
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
api_url() click to toggle source
# File lib/esa/client.rb, line 110
def api_url
  @api_endpoint || ENV['ESA_API_ENDPOINT'] || 'https://api.esa.io'
end
faraday_options() click to toggle source
# File lib/esa/client.rb, line 89
def faraday_options
  { url: api_url, headers: api_headers }
end
wait_for(wait_sec) click to toggle source
# File lib/esa/client.rb, line 114
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