Class: FolioApiClient

Inherits:
Object
  • Object
show all
Includes:
Finders
Defined in:
lib/folio_api_client.rb,
lib/folio_api_client/finders.rb,
lib/folio_api_client/version.rb,
lib/folio_api_client/exceptions.rb,
lib/folio_api_client/configuration.rb

Overview

A client used for making http requests (get/post/etc.) to the FOLIO ILS REST API.

Defined Under Namespace

Modules: Exceptions, Finders Classes: Configuration

Constant Summary collapse

VERSION =
'0.3.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Finders

#find_holdings_record, #find_instance_record, #find_item_record, #find_location_record, #find_marc_record, #marc_record_query

Constructor Details

#initialize(config) ⇒ FolioApiClient

Returns a new instance of FolioApiClient.



16
17
18
# File 'lib/folio_api_client.rb', line 16

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/folio_api_client.rb', line 14

def config
  @config
end

Instance Method Details

#connectionObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/folio_api_client.rb', line 29

def connection
  @connection ||= Faraday.new(
    headers: headers_for_connection,
    url: config.url,
    request: { timeout: config.timeout }
  ) do |faraday|
    faraday.adapter Faraday.default_adapter
    faraday.use Faraday::Response::RaiseError
  end
end

#delete(path, body = nil, content_type: 'application/json') ⇒ Object



87
88
89
# File 'lib/folio_api_client.rb', line 87

def delete(path, body = nil, content_type: 'application/json')
  exec_request_with_body(:delete, path, body, content_type: content_type)
end

#exec_request_with_body(method, path, body = nil, content_type: 'application/json') ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/folio_api_client.rb', line 70

def exec_request_with_body(method, path, body = nil, content_type: 'application/json')
  body = body.to_json if content_type == 'application/json' && !body.is_a?(String)
  response = with_token_refresh_attempt_when_unauthorized do
    connection.send(method, path, body, { 'x-okapi-token': config.token, 'content-type': content_type })
  end

  response.body.nil? ? nil : JSON.parse(response.body)
end

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



62
63
64
65
66
67
68
# File 'lib/folio_api_client.rb', line 62

def get(path, params = {})
  response = with_token_refresh_attempt_when_unauthorized do
    connection.get(path, params, { 'x-okapi-token': config.token })
  end

  JSON.parse(response.body)
end

#headers_for_connectionObject



20
21
22
23
24
25
26
27
# File 'lib/folio_api_client.rb', line 20

def headers_for_connection
  {
    'Accept': 'application/json, text/plain',
    'Content-Type': 'application/json',
    'X-Okapi-Tenant': config.tenant,
    'User-Agent': config.user_agent
  }
end

#post(path, body = nil, content_type: 'application/json') ⇒ Object



79
80
81
# File 'lib/folio_api_client.rb', line 79

def post(path, body = nil, content_type: 'application/json')
  exec_request_with_body(:post, path, body, content_type: content_type)
end

#put(path, body = nil, content_type: 'application/json') ⇒ Object



83
84
85
# File 'lib/folio_api_client.rb', line 83

def put(path, body = nil, content_type: 'application/json')
  exec_request_with_body(:put, path, body, content_type: content_type)
end

#refresh_auth_token!Object



46
47
48
# File 'lib/folio_api_client.rb', line 46

def refresh_auth_token!
  config.token = retrieve_new_auth_token
end

#retrieve_new_auth_tokenObject



40
41
42
43
44
# File 'lib/folio_api_client.rb', line 40

def retrieve_new_auth_token
  response = connection.post('/authn/login', { username: config.username, password: config.password }.to_json)
  response_data = JSON.parse(response.body)
  response_data['okapiToken']
end

#with_token_refresh_attempt_when_unauthorizedObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/folio_api_client.rb', line 50

def with_token_refresh_attempt_when_unauthorized
  refresh_auth_token! if config.token.nil?
  yield
rescue Faraday::UnauthorizedError, Faraday::ForbiddenError
  # Tokens are sometimes invalidated by FOLIO data refreshes, so we'll attempt to refresh our token
  # one time in responde to a 401 or 403.
  refresh_auth_token!

  # Re-run block
  yield
end