Class: FrederickAPI::V2::Helpers::Requestor

Inherits:
JsonApiClient::Query::Requestor
  • Object
show all
Defined in:
lib/frederick_api/v2/helpers/requestor.rb

Overview

Requestor for v2 client to use built on top of JsonApiClient::Query::Requestor

Constant Summary collapse

JSON_API_CLIENT_PASSTHROUGH_ERRORS =

For backward compatibility, preserve these JSON API client errors instead of raising FrederickAPI::Errors::Error

[
  JsonApiClient::Errors::NotAuthorized,
  JsonApiClient::Errors::AccessDenied,
  JsonApiClient::Errors::NotFound,
  JsonApiClient::Errors::Conflict,
  JsonApiClient::Errors::ServerError,
  JsonApiClient::Errors::UnexpectedStatus
].freeze
GET_VIA_POST_PATHS =

Paths that may have an unbounded query param length so we should always use a POST instead of a GET to get around AWS Cloudfront limitations

[
  %r{^.*locations\/[^\/]+\/contacts$},
  %r{^.*locations\/[^\/]+\/interactions$}
].map(&:freeze).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, path = nil) ⇒ Requestor

Returns a new instance of Requestor.



28
29
30
31
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 28

def initialize(klass, path = nil)
  @klass = klass
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 8

def path
  @path
end

Instance Method Details

#get(params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 42

def get(params = {})
  path = resource_path(params)

  params.delete(klass.primary_key)
  if get_via_post_path?(path)
    return request(:post, path, body: params.to_json, additional_headers: { 'X-Request-Method' => 'GET' })
  end

  request(:get, path, params: params)
end

#linked(path) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 53

def linked(path)
  uri = URI.parse(path)
  return super unless get_via_post_path?(uri.path)

  path_without_params = "#{uri.scheme}://#{uri.host}#{uri.path}"
  params = uri.query ? CGI.parse(uri.query).each_with_object({}) { |(k, v), h| h[k] = v[0] } : {}
  request(:post, path_without_params, body: params.to_json, additional_headers: { 'X-Request-Method' => 'GET' })
end

#request(type, path, params: nil, body: nil, additional_headers: {}) ⇒ Object

Retry once on unhandled server errors



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 63

def request(type, path, params: nil, body: nil, additional_headers: {})
  headers = klass.custom_headers.merge(additional_headers)
  make_request = proc do
    handle_background(handle_errors(make_request(type, path, params: params, body: body, headers: headers)))
  end

  begin
    make_request.call
  rescue JsonApiClient::Errors::ConnectionError, JsonApiClient::Errors::ServerError => ex
    raise ex if ex.is_a?(JsonApiClient::Errors::NotFound) || ex.is_a?(JsonApiClient::Errors::Conflict)
    make_request.call
  end
end

#resource_path(parameters) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 33

def resource_path(parameters)
  base_path = path || klass.path(parameters)
  if (resource_id = parameters[klass.primary_key])
    File.join(base_path, encode_part(resource_id))
  else
    base_path
  end
end