Class: PlatformApiClient
- Inherits:
-
Object
- Object
- PlatformApiClient
- Defined in:
- lib/platform_api_client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#authenticated ⇒ Object
readonly
Returns the value of attribute authenticated.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#error_options ⇒ Object
readonly
Returns the value of attribute error_options.
-
#oauth_site ⇒ Object
readonly
Returns the value of attribute oauth_site.
Instance Method Summary collapse
- #get(path, transaction_data = {}) ⇒ Object
-
#initialize(options = {}) ⇒ PlatformApiClient
constructor
A new instance of PlatformApiClient.
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( = {}) 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([: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 = [:authenticated] || true = default_errors.merge([:errors] || {}) end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
9 10 11 |
# File 'lib/platform_api_client.rb', line 9 def access_token @access_token end |
#authenticated ⇒ Object (readonly)
Returns the value of attribute authenticated.
8 9 10 |
# File 'lib/platform_api_client.rb', line 8 def authenticated @authenticated end |
#client_id ⇒ Object (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_secret ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute error_options.
8 9 10 |
# File 'lib/platform_api_client.rb', line 8 def end |
#oauth_site ⇒ Object (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 |