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.5.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Finders

#find_holdings_record, #find_instance_record, #find_item_record, #find_loan_type_record, #find_location_record, #find_material_type_record, #find_source_marc_records, #find_source_record, #instance_record_query, #location_record_query, #marc_records_query, #process_marc_for_instance, #source_record_query, #with_uuid_prefixes

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



93
94
95
# File 'lib/folio_api_client.rb', line 93

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



76
77
78
79
80
81
82
83
# File 'lib/folio_api_client.rb', line 76

def exec_request_with_body(method, path, body = nil, content_type: 'application/json')
  body = JSON.generate(body) 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? || response.body == '' ? nil : JSON.parse(response.body)
end

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



68
69
70
71
72
73
74
# File 'lib/folio_api_client.rb', line 68

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



85
86
87
# File 'lib/folio_api_client.rb', line 85

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



89
90
91
# File 'lib/folio_api_client.rb', line 89

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', JSON.generate({ username: config.username, password: config.password }))
  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
61
62
63
64
65
66
# 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
rescue Faraday::ConnectionFailed
  # If we make too many rapid requests in a row, FOLIO sometimes disconnects.
  # If this happens, we'll sleep for a little while and retry the request.
  sleep 5
  # Re-run block
  yield
end