Class: PlatformApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/platform_api_client.rb

Direct Known Subclasses

NYPLRubyUtil::PlatformApiClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PlatformApiClient

Returns a new instance of PlatformApiClient.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/platform_api_client.rb', line 11

def initialize(options = {})
  raise 'Missing config: NYPL_OAUTH_ID is unset' if ENV['NYPL_OAUTH_ID'].nil? || ENV['NYPL_OAUTH_ID'].empty?
  raise 'Missing config: NYPL_OAUTH_SECRET is unset' if ENV['NYPL_OAUTH_SECRET'].nil? || ENV['NYPL_OAUTH_SECRET'].empty?

  kms_client = KmsClient.new(options[:kms_options])
  @client_id = kms_client.decrypt(ENV['NYPL_OAUTH_ID'])
  @client_secret = kms_client.decrypt(ENV['NYPL_OAUTH_SECRET'])

  @oauth_site = ENV['NYPL_OAUTH_URL']
  @authenticated = options[:authenticated] || true
  @error_options = default_errors.merge(options[:errors] || {})
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



9
10
11
# File 'lib/platform_api_client.rb', line 9

def access_token
  @access_token
end

#authenticatedObject (readonly)

Returns the value of attribute authenticated.



8
9
10
# File 'lib/platform_api_client.rb', line 8

def authenticated
  @authenticated
end

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/platform_api_client.rb', line 8

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



8
9
10
# File 'lib/platform_api_client.rb', line 8

def client_secret
  @client_secret
end

#error_optionsObject (readonly)

Returns the value of attribute error_options.



8
9
10
# File 'lib/platform_api_client.rb', line 8

def error_options
  @error_options
end

#oauth_siteObject (readonly)

Returns the value of attribute oauth_site.



8
9
10
# File 'lib/platform_api_client.rb', line 8

def oauth_site
  @oauth_site
end

Instance Method Details

#get(path, transaction_data = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/platform_api_client.rb', line 24

def get (path, transaction_data = {})

  authenticate! if authenticated

  uri = URI.parse("#{ENV['PLATFORM_API_BASE_URL']}#{path}")

  $logger.debug "Getting from platform api", { uri: uri }

  begin
    request = Net::HTTP::Get.new(uri)
    request["Authorization"] = "Bearer #{access_token}" if authenticated
    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme === 'https') do |http|
      http.request(request)
    end

    $logger.debug "Got platform api response", { code: response.code, body: response.body }

    parse_json_response response, path, transaction_data

  rescue Exception => e
    raise StandardError.new(e), "Failed to retrieve #{path} #{e.message}"
  end
end