class ESI::Client

Client for the EVE Swagger Interface (ESI) API.

Constants

DEFAULT_BASE_URL
DEFAULT_VERSION
ERROR_MAPPING
ESI_RETRY_EXCEPTIONS

Attributes

base_url[R]
cache[R]
hydra[R]
user_agent[R]
version[R]

Public Class Methods

new(user_agent:, base_url: DEFAULT_BASE_URL, version: DEFAULT_VERSION, cache: {}, max_concurrency: 50) click to toggle source

Returns a new {ESI::Client}.

See the [faraday-http-cache](github.com/sourcelevel/faraday-http-cache) documentation for information on how to set up caching via the `cache` parameter.

@param user_agent [String] Value of the `User-Agent` header for HTTP calls @param base_url [String] The base URL of the ESI API @param version [String] The version of the ESI API @param cache [Hash] The cache configuration to use @param max_concurrency [Integer] Maximum concurrent requests for pagination @option cache [Object] :store The cache store (e.g. `Rails.cache`) @option cache [Object] :logger The logger (e.g. `Rails.logger`) @option cache [Object] :instrumenter The instrumenter (e.g. `ActiveSupport::Notifications`)

# File lib/esi/client.rb, line 111
def initialize(user_agent:, base_url: DEFAULT_BASE_URL, version: DEFAULT_VERSION, cache: {}, max_concurrency: 50)
  @base_url = base_url
  @cache = cache
  @user_agent = user_agent
  @version = version
  @hydra = Typhoeus::Hydra.new(max_concurrency: max_concurrency)
end

Public Instance Methods

authorize(token) click to toggle source

Set the `Authorization` header for subsequent requests.

@param token [String] The [EVE SSO JWT token](docs.esi.evetech.net/docs/sso/) to use

# File lib/esi/client.rb, line 122
def authorize(token)
  url_encoded_connection.authorization :Bearer, token
  json_encoded_connection.authorization :Bearer, token
end

Private Instance Methods

default_headers() click to toggle source
# File lib/esi/client.rb, line 243
def default_headers
  { "User-Agent": user_agent, Accept: "application/json" }
end
delete(path, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 134
def delete(path, params: {}, headers: {})
  response = make_delete_request(path, params: params, headers: headers)
  raise_error(response) unless response.success?
  response.body
end
get(path, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 140
def get(path, params: {}, headers: {})
  response = make_get_request(path, params: params, headers: headers)
  raise_error(response) unless response.success?

  response_headers = normalize_headers(response.headers)
  return paginate(response, path, params, headers) if paginated?(response_headers)

  response.body
end
json_encoded_connection() click to toggle source
# File lib/esi/client.rb, line 233
def json_encoded_connection
  @json_encoded_connection ||= Faraday.new(base_url, headers: default_headers) do |f|
    f.use :http_cache, **cache unless cache.empty?
    f.request :json
    f.request :retry, { exceptions: ESI_RETRY_EXCEPTIONS, max: 10, retry_statuses: [502, 503, 504] }
    f.response :json
    f.adapter :typhoeus
  end
end
make_delete_request(path, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 183
def make_delete_request(path, params: {}, headers: {})
  params.delete_if { |_, v| v.nil? }
  url_encoded_connection.delete("/#{version}#{path}", params, headers)
end
make_get_request(path, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 188
def make_get_request(path, params: {}, headers: {})
  params.delete_if { |_, v| v.nil? }
  url_encoded_connection.get("/#{version}#{path}", params, headers)
end
make_post_request(path, payload: {}, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 193
def make_post_request(path, payload: {}, params: {}, headers: {})
  params.delete_if { |_, v| v.nil? }
  json_encoded_connection.post("/#{version}#{path}") do |req|
    req.params = params
    req.headers = req.headers.merge(headers)
    req.body = payload.to_json
  end
end
make_put_request(path, payload: {}, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 202
def make_put_request(path, payload: {}, params: {}, headers: {})
  params.delete_if { |_, v| v.nil? }
  json_encoded_connection.put("/#{version}#{path}") do |req|
    req.params = params
    req.headers = req.headers.merge(headers)
    req.body = payload.to_json
  end
end
normalize_headers(headers) click to toggle source
# File lib/esi/client.rb, line 215
def normalize_headers(headers)
  headers.transform_keys(&:downcase)
end
paginate(response, path, params, headers) click to toggle source
# File lib/esi/client.rb, line 162
def paginate(response, path, params, headers) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  all_items = response.body
  response_headers = normalize_headers(response.headers)
  page_count = response_headers["x-pages"].to_i - 1

  responses = []
  url_encoded_connection.in_parallel(hydra) do
    page_count.times do |n|
      page_number = n + 2
      params = params.merge(page: page_number)
      responses << make_get_request(path, params: params, headers: headers)
    end
  end
  unless responses.all?(&:success?)
    raise ESI::Errors::PaginationError.new("Error paginating request", response: response,
                                                                       responses: responses)
  end

  all_items + responses.map(&:body).flatten
end
paginated?(headers) click to toggle source
# File lib/esi/client.rb, line 211
def paginated?(headers)
  headers["x-pages"] && headers["x-pages"].to_i > 1
end
post(path, payload: {}, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 150
def post(path, payload: {}, params: {}, headers: {})
  response = make_post_request(path, payload: payload, params: params, headers: headers)
  raise_error(response) unless response.success?
  response.body
end
put(path, payload: {}, params: {}, headers: {}) click to toggle source
# File lib/esi/client.rb, line 156
def put(path, payload: {}, params: {}, headers: {})
  response = make_put_request(path, payload: payload, params: params, headers: headers)
  raise_error(response) unless response.success?
  response.body
end
raise_error(res) click to toggle source
# File lib/esi/client.rb, line 219
def raise_error(res)
  raise (ERROR_MAPPING[res.status] || ESI::Errors::ClientError).new("(#{res.status}) #{res["error"]}",
                                                                    response: res)
end
url_encoded_connection() click to toggle source
# File lib/esi/client.rb, line 224
def url_encoded_connection
  @url_encoded_connection ||= Faraday.new(base_url, headers: default_headers) do |f|
    f.use :http_cache, **cache unless cache.empty?
    f.request :retry, { exceptions: ESI_RETRY_EXCEPTIONS, max: 10, retry_statuses: [502, 503, 504] }
    f.response :json
    f.adapter :typhoeus
  end
end