Class: FolioClient

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/folio_client.rb,
lib/folio_client/users.rb,
lib/folio_client/version.rb,
lib/folio_client/inventory.rb,
lib/folio_client/job_status.rb,
lib/folio_client/data_import.rb,
lib/folio_client/authenticator.rb,
lib/folio_client/organizations.rb,
lib/folio_client/records_editor.rb,
lib/folio_client/source_storage.rb,
lib/folio_client/unexpected_response.rb

Overview

Client for interacting with the Folio API

Defined Under Namespace

Classes: Authenticator, ConflictError, DataImport, Error, ForbiddenError, Inventory, JobStatus, MultipleResourcesFound, Organizations, RecordsEditor, ResourceNotFound, ServiceUnavailable, SourceStorage, UnauthorizedError, UnexpectedResponse, Users, ValidationError

Constant Summary collapse

DEFAULT_HEADERS =
{
  accept: "application/json, text/plain",
  content_type: "application/json"
}.freeze
VERSION =
"0.15.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



82
83
84
# File 'lib/folio_client.rb', line 82

def config
  @config
end

Class Method Details

.configure(url:, login_params:, okapi_headers:, timeout: default_timeout) ⇒ FolioClient

Returns the configured Singleton class.

Parameters:

  • url (String)

    the folio API URL

  • login_params (Hash)

    the folio client login params (username:, password:)

  • okapi_headers (Hash)

    the okapi specific headers to add (X-Okapi-Tenant:, User-Agent:)

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/folio_client.rb', line 52

def configure(url:, login_params:, okapi_headers:, timeout: default_timeout)
  instance.config = OpenStruct.new(
    # For the initial token, use a dummy value to avoid hitting any APIs
    # during configuration, allowing `with_token_refresh_when_unauthorized` to handle
    # auto-magic token refreshing. Why not immediately get a valid token? Our apps
    # commonly invoke client `.configure` methods in the initializer in all
    # application environments, even those that are never expected to
    # connect to production APIs, such as local development machines.
    #
    # NOTE: `nil` and blank string cannot be used as dummy values here as
    # they lead to a malformed request to be sent, which triggers an
    # exception not rescued by `with_token_refresh_when_unauthorized`
    token: "a temporary dummy token to avoid hitting the API before it is needed",
    url: url,
    login_params: ,
    okapi_headers: okapi_headers,
    timeout: timeout
  )

  self
end

Instance Method Details

#connectionObject

the base connection to the Folio API



142
143
144
145
146
147
148
# File 'lib/folio_client.rb', line 142

def connection
  @connection ||= Faraday.new(
    url: config.url,
    headers: DEFAULT_HEADERS.merge(config.okapi_headers || {}),
    request: {timeout: config.timeout}
  )
end

#data_importObject

@ see DataImport#import



195
196
197
198
199
# File 'lib/folio_client.rb', line 195

def data_import(...)
  DataImport
    .new(self)
    .import(...)
end

#default_timeoutObject



250
251
252
# File 'lib/folio_client.rb', line 250

def default_timeout
  120
end

#edit_marc_jsonObject



209
210
211
212
213
# File 'lib/folio_client.rb', line 209

def edit_marc_json(...)
  RecordsEditor
    .new(self)
    .edit_marc_json(...)
end

#fetch_external_idObject



160
161
162
163
164
# File 'lib/folio_client.rb', line 160

def fetch_external_id(...)
  Inventory
    .new(self)
    .fetch_external_id(...)
end

#fetch_hridObject



153
154
155
156
157
# File 'lib/folio_client.rb', line 153

def fetch_hrid(...)
  Inventory
    .new(self)
    .fetch_hrid(...)
end

#fetch_instance_infoObject



167
168
169
170
171
# File 'lib/folio_client.rb', line 167

def fetch_instance_info(...)
  Inventory
    .new(self)
    .fetch_instance_info(...)
end

#fetch_marc_hashObject



174
175
176
177
178
# File 'lib/folio_client.rb', line 174

def fetch_marc_hash(...)
  SourceStorage
    .new(self)
    .fetch_marc_hash(...)
end

#fetch_marc_xmlObject



181
182
183
184
185
# File 'lib/folio_client.rb', line 181

def fetch_marc_xml(...)
  SourceStorage
    .new(self)
    .fetch_marc_xml(...)
end

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

Send an authenticated get request

Parameters:

  • path (String)

    the path to the Folio API request

  • params (Hash) (defaults to: {})

    params to get to the API



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/folio_client.rb', line 87

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

  UnexpectedResponse.call(response) unless response.success?

  return nil if response.body.blank?

  JSON.parse(response.body)
end

#has_instance_status?Boolean

Returns:

  • (Boolean)

See Also:



188
189
190
191
192
# File 'lib/folio_client.rb', line 188

def has_instance_status?(...)
  Inventory
    .new(self)
    .has_instance_status?(...)
end

#interface_detailsObject



230
231
232
233
234
# File 'lib/folio_client.rb', line 230

def interface_details(...)
  Organizations
    .new(self)
    .fetch_interface_details(...)
end

#job_profilesObject

@ see DataImport#job_profiles



202
203
204
205
206
# File 'lib/folio_client.rb', line 202

def job_profiles(...)
  DataImport
    .new(self)
    .job_profiles(...)
end

#organization_interfacesObject



223
224
225
226
227
# File 'lib/folio_client.rb', line 223

def organization_interfaces(...)
  Organizations
    .new(self)
    .fetch_interface_list(...)
end

#organizationsObject



216
217
218
219
220
# File 'lib/folio_client.rb', line 216

def organizations(...)
  Organizations
    .new(self)
    .fetch_list(...)
end

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

Send an authenticated post request If the body is JSON, it will be automatically serialized

Parameters:

  • path (String)

    the path to the Folio API request

  • body (Object) (defaults to: nil)

    body to post to the API as JSON



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/folio_client.rb', line 103

def post(path, body = nil, content_type: "application/json")
  req_body = (content_type == "application/json") ? body&.to_json : body
  response = with_token_refresh_when_unauthorized do
    req_headers = {
      "x-okapi-token": config.token,
      "content-type": content_type
    }
    connection.post(path, req_body, req_headers)
  end

  UnexpectedResponse.call(response) unless response.success?

  return nil if response.body.blank?

  JSON.parse(response.body)
end

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

Send an authenticated put request If the body is JSON, it will be automatically serialized

Parameters:

  • path (String)

    the path to the Folio API request

  • body (Object) (defaults to: nil)

    body to put to the API as JSON



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/folio_client.rb', line 124

def put(path, body = nil, content_type: "application/json")
  req_body = (content_type == "application/json") ? body&.to_json : body
  response = with_token_refresh_when_unauthorized do
    req_headers = {
      "x-okapi-token": config.token,
      "content-type": content_type
    }
    connection.put(path, req_body, req_headers)
  end

  UnexpectedResponse.call(response) unless response.success?

  return nil if response.body.blank?

  JSON.parse(response.body)
end

#user_detailsObject



244
245
246
247
248
# File 'lib/folio_client.rb', line 244

def user_details(...)
  Users
    .new(self)
    .fetch_user_details(...)
end

#usersObject



237
238
239
240
241
# File 'lib/folio_client.rb', line 237

def users(...)
  Users
    .new(self)
    .fetch_list(...)
end