Class: ShopifyApiBruv::Clients::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_api_bruv/clients/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, path:) ⇒ HttpClient

Returns a new instance of HttpClient.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shopify_api_bruv/clients/http_client.rb', line 8

def initialize(config:, path:)
  raise Errors::HttpClientError, 'Missing or invalid config.' unless config.is_a?(Auth::Config)

  @config = config
  @endpoint = "#{config.api_domain}/#{path}"
  @headers = {
    'User-Agent': "Bruv #{VERSION}",
    'Accept': 'application/json',
    'X-Shopify-Access-Token': config.api_access_token
  }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/shopify_api_bruv/clients/http_client.rb', line 6

def config
  @config
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



6
7
8
# File 'lib/shopify_api_bruv/clients/http_client.rb', line 6

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/shopify_api_bruv/clients/http_client.rb', line 6

def headers
  @headers
end

Instance Method Details

#request(http_request:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shopify_api_bruv/clients/http_client.rb', line 20

def request(http_request:)
  raise Errors::HttpClientError, 'Missing or invalid http request.' unless http_request.is_a?(HttpRequest)

  headers.merge(http_request.headers) unless http_request.headers.nil?
  headers['Content-Type'] = http_request.content_type

  response = HTTParty.public_send(
    http_request.method,
    "https://#{endpoint}/#{http_request.path}",
    headers:,
    query: http_request.query,
    body: http_request.body
  )

  if http_request.api == :rest
    Rest::HttpResponse.new(code: response.code, headers: response.headers, body: response.parsed_response)
  else
    HttpResponse.new(code: response.code, headers: response.headers, body: response.parsed_response)
  end
end