Class: NyplPlatformApiClient

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ NyplPlatformApiClient

Returns a new instance of NyplPlatformApiClient.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nypl_platform_api_client.rb', line 8

def initialize(config = {})
  @config = {
    base_url: ENV['PLATFORM_API_BASE_URL'],
    client_id: ENV['NYPL_OAUTH_ID'],
    client_secret: ENV['NYPL_OAUTH_SECRET'],
    oauth_url: ENV['NYPL_OAUTH_URL'],
    log_level: 'info'
  }.merge config

  raise NyplPlatformApiClientError.new 'Missing config: neither config.base_url nor ENV.PLATFORM_API_BASE_URL are set' unless @config[:base_url]
  raise NyplPlatformApiClientError.new 'Missing config: neither config.client_id nor ENV.NYPL_OAUTH_ID are set' unless @config[:client_id]
  raise NyplPlatformApiClientError.new 'Missing config: neither config.client_secret nor ENV.NYPL_OAUTH_SECRET are set ' unless @config[:client_secret]
  raise NyplPlatformApiClientError.new 'Missing config: neither config.oauth_url nor ENV.NYPL_OAUTH_URL are set ' unless @config[:oauth_url]
end

Instance Method Details

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



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

def get (path, options = {})
  options = parse_http_options options

  authenticate! if options[:authenticated]

  uri = URI.parse("#{@config[:base_url]}#{path}")

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

  begin
    request = Net::HTTP::Get.new(uri)

    # Add bearer token header
    request["Authorization"] = "Bearer #{@access_token}" if options[:authenticated]

    # Execute request:
    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme === 'https') do |http|
      http.request(request)
    end
  rescue Exception => e
    raise NyplPlatformApiClientError.new(e), "Failed to GET #{path}: #{e.message}"
  end

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

  parse_json_response response
end

#post(path, body, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nypl_platform_api_client.rb', line 51

def post (path, body, options = {})
  options = parse_http_options options

  # Default to POSTing JSON unless explicitly stated otherwise
  options[:headers]['Content-Type'] = 'application/json' unless options[:headers]['Content-Type']

  authenticate! if options[:authenticated]

  uri = URI.parse("#{@config[:base_url]}#{path}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme === 'https'

  begin
    request = Net::HTTP::Post.new(uri.path, 'Content-Type' => options[:headers]['Content-Type'])
    request.body = body.to_json

    logger.debug "NyplPlatformApiClient: Posting to platform api", { uri: uri, body: body }

    # Add bearer token header
    request['Authorization'] = "Bearer #{@access_token}" if options[:authenticated]

    # Execute request:
    response = http.request(request)
  rescue Exception => e
    raise NyplPlatformApiClientError.new(e), "Failed to POST to #{path}: #{e.message}"
  end

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

  parse_json_response response
end