Module: XIVAPI::HTTP

Included in:
Paginator, Request
Defined in:
lib/xivapi/http.rb

Overview

Makes HTTP request to XIVAPI

Constant Summary collapse

API_BASE =

Base URL for XIVAPI

'https://xivapi.com'.freeze
STAGING_API_BASE =

Base URL for the staging environment of XIVAPI

'https://staging.xivapi.com'.freeze

Instance Method Summary collapse

Instance Method Details

#request(client, endpoint, params = {}, payload = nil) ⇒ Object

Makes a request to XIVAPI

Parameters:

  • client (XIVAPI::Client)

    The client making the request

  • endpoint (String, Symbol)

    The endpoint to request

  • params (Hash) (defaults to: {})

    Request parameters

  • payload (Hash) (defaults to: nil)

    Request body

Returns:

  • the results of the request



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
# File 'lib/xivapi/http.rb', line 16

def request(client, endpoint, params = {}, payload = nil)
  url = request_url(client, endpoint)
  query_params = params.merge(client.default_params)
    .reject { |_, v| v.nil? || v.size == 0 }

  begin
    if payload
      response = RestClient::Request.execute(method: :get, url: url, headers: { params: query_params },
                                             payload: payload.to_json)
    else
      response = RestClient.get(url, params: query_params)
    end

    body = JSON.parse(response.body)
    objectify(body)
  rescue RestClient::ExceptionWithResponse => e
    if e.http_code == 429
      raise XIVAPI::RateLimitError.new
    else
      raise XIVAPI::RequestError.new(e.response)
    end
  rescue RestClient::Exception => e
    raise e
  end
end