Class: FolioClient

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
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, BadRequestError, Config, ConflictError, DataImport, Error, ForbiddenError, Inventory, JobStatus, MultipleResourcesFound, Organizations, RecordsEditor, ResourceNotFound, ServiceUnavailable, SourceStorage, UnauthorizedError, UnexpectedResponse, Users, ValidationError

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configFolioClient::Config?

Returns active runtime configuration.

Returns:



100
101
102
# File 'lib/folio_client.rb', line 100

def config
  @config
end

Class Method Details

.configure(url:, login_params:, tenant_id: nil, user_agent: nil, timeout: nil) ⇒ Class<FolioClient>

Configure the singleton FolioClient instance.

Examples:

FolioClient.configure(
  url: 'https://folio.example.edu',
  login_params: { username: 'svc-user', password: 'secret' },
  tenant_id: 'sul'
)

Parameters:

  • url (String)

    Folio API base URL

  • login_params (Hash)

    authentication payload (e.g., username, password)

  • tenant_id (String, nil) (defaults to: nil)

    Folio tenant identifier

  • user_agent (String, nil) (defaults to: nil)

    user agent string included on outbound requests

  • timeout (Integer) (defaults to: nil)

    request timeout, in seconds

Returns:

  • (Class<FolioClient>)

    the configured singleton class for chaining



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/folio_client.rb', line 78

def configure(url:, login_params:, tenant_id: nil, user_agent: nil, timeout: nil)
  instance.config = Config.new(
    url: url,
    login_params: ,
    tenant_id: tenant_id,
    timeout: timeout || default_timeout,
    user_agent: user_agent || default_user_agent
  )

  self
end

Instance Method Details

#connectionFaraday::Connection

Build (or memoize) the base Faraday connection.

Returns:

  • (Faraday::Connection)

    configured HTTP connection



158
159
160
161
162
163
164
165
166
167
# File 'lib/folio_client.rb', line 158

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

Build (or memoize) the cookie jar used by Faraday to store authentication cookies.

Returns:

  • (HTTP::CookieJar)

    cookie storage for session-aware requests



171
172
173
# File 'lib/folio_client.rb', line 171

def cookie_jar
  @cookie_jar ||= HTTP::CookieJar.new
end

#create_holdingsObject

Create a new holdings record.

Returns:

  • (Object)

    delegated return value from Inventory#create_holdings

See Also:



220
221
222
# File 'lib/folio_client.rb', line 220

def create_holdings(...)
  inventory.create_holdings(...)
end

#data_importObject

Run an inventory data import workflow.

Returns:

  • (Object)

    delegated return value from DataImport#import

See Also:



248
249
250
# File 'lib/folio_client.rb', line 248

def data_import(...)
  data_import_service.import(...)
end

#default_timeoutInteger

Default HTTP timeout in seconds.

Returns:

  • (Integer)


311
312
313
# File 'lib/folio_client.rb', line 311

def default_timeout
  180
end

#default_user_agentString

Default user-agent string used for outbound requests.

Returns:

  • (String)


318
319
320
# File 'lib/folio_client.rb', line 318

def default_user_agent
  "folio_client #{VERSION}"
end

#edit_marc_jsonObject

Edit MARC-in-JSON records.

Returns:

  • (Object)

    delegated return value from RecordsEditor#edit_marc_json

See Also:



262
263
264
# File 'lib/folio_client.rb', line 262

def edit_marc_json(...)
  records_editor.edit_marc_json(...)
end

#fetch_external_idObject

Fetch the Folio external id for a matching record.

Returns:

  • (Object)

    delegated return value from Inventory#fetch_external_id

See Also:



185
186
187
# File 'lib/folio_client.rb', line 185

def fetch_external_id(...)
  inventory.fetch_external_id(...)
end

#fetch_holdingsObject

Fetch holdings associated with a record.

Returns:

  • (Object)

    delegated return value from Inventory#fetch_holdings

See Also:



206
207
208
# File 'lib/folio_client.rb', line 206

def fetch_holdings(...)
  inventory.fetch_holdings(...)
end

#fetch_hridObject

Fetch a Folio HRID by instance identifier or query context.

Returns:

  • (Object)

    delegated return value from Inventory#fetch_hrid

See Also:



178
179
180
# File 'lib/folio_client.rb', line 178

def fetch_hrid(...)
  inventory.fetch_hrid(...)
end

#fetch_instance_infoObject

Fetch inventory instance details.

Returns:

  • (Object)

    delegated return value from Inventory#fetch_instance_info

See Also:



192
193
194
# File 'lib/folio_client.rb', line 192

def fetch_instance_info(...)
  inventory.fetch_instance_info(...)
end

#fetch_locationObject

Fetch location details from inventory.

Returns:

  • (Object)

    delegated return value from Inventory#fetch_location

See Also:



199
200
201
# File 'lib/folio_client.rb', line 199

def fetch_location(...)
  inventory.fetch_location(...)
end

#fetch_marc_hashObject

Fetch MARC data as a Ruby hash.

Returns:

  • (Object)

    delegated return value from SourceStorage#fetch_marc_hash

See Also:



227
228
229
# File 'lib/folio_client.rb', line 227

def fetch_marc_hash(...)
  source_storage.fetch_marc_hash(...)
end

#fetch_marc_xmlObject

Fetch MARC data as XML.

Returns:

  • (Object)

    delegated return value from SourceStorage#fetch_marc_xml

See Also:



234
235
236
# File 'lib/folio_client.rb', line 234

def fetch_marc_xml(...)
  source_storage.fetch_marc_xml(...)
end

#force_token_refresh!Object

Force a refresh of the current auth token.

Returns:

  • (Object)

    return value from Authenticator.refresh_token!



304
305
306
# File 'lib/folio_client.rb', line 304

def force_token_refresh!
  Authenticator.refresh_token!
end

#get(path, params = {}) ⇒ Hash, ...

Send an authenticated GET request.

Parameters:

  • path (String)

    API path relative to configured url

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

    query parameters

Returns:

  • (Hash, Array, nil)

    parsed JSON body, or nil for empty body

Raises:



107
108
109
110
111
112
113
114
115
# File 'lib/folio_client.rb', line 107

def get(path, params = {})
  response = with_token_refresh_when_unauthorized do
    connection.get(path, params)
  end

  UnexpectedResponse.call(response) unless response.success?

  JSON.parse(response.body) if response.body.present?
end

#has_instance_status?Boolean

Determine whether an instance has the requested status.

Returns:

  • (Boolean)

    delegated predicate result

See Also:



241
242
243
# File 'lib/folio_client.rb', line 241

def has_instance_status?(...) # rubocop:disable Naming/PredicatePrefix
  inventory.has_instance_status?(...)
end

#interface_detailsObject

Fetch detailed interface information for an organization interface.

Returns:

  • (Object)

    delegated return value from Organizations#fetch_interface_details

See Also:



283
284
285
# File 'lib/folio_client.rb', line 283

def interface_details(...)
  organizations_service.fetch_interface_details(...)
end

#job_profilesObject

List available data-import job profiles.

Returns:

  • (Object)

    delegated return value from DataImport#job_profiles

See Also:



255
256
257
# File 'lib/folio_client.rb', line 255

def job_profiles(...)
  data_import_service.job_profiles(...)
end

#organization_interfacesObject

List interfaces for organizations.

Returns:

  • (Object)

    delegated return value from Organizations#fetch_interface_list

See Also:



276
277
278
# File 'lib/folio_client.rb', line 276

def organization_interfaces(...)
  organizations_service.fetch_interface_list(...)
end

#organizationsObject

List organizations.

Returns:

  • (Object)

    delegated return value from Organizations#fetch_list

See Also:



269
270
271
# File 'lib/folio_client.rb', line 269

def organizations(...)
  organizations_service.fetch_list(...)
end

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

Send an authenticated POST request. If content_type is application/json, body is serialized with to_json. Otherwise body is sent unchanged.

Parameters:

  • path (String)

    API path relative to configured url

  • body (Hash, String, nil) (defaults to: nil)

    request payload

  • content_type (String) (defaults to: 'application/json')

    MIME type of request body

Returns:

  • (Hash, Array, nil)

    parsed JSON body, or nil for empty body

Raises:



125
126
127
128
129
130
131
132
133
134
# File 'lib/folio_client.rb', line 125

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
    connection.post(path, req_body, { content_type: content_type })
  end

  UnexpectedResponse.call(response) unless response.success?

  JSON.parse(response.body) if response.body.present?
end

#put(path, body = nil, content_type: 'application/json', **exception_args) ⇒ Hash, ...

Send an authenticated PUT request. If content_type is application/json, body is serialized with to_json. Otherwise body is sent unchanged.

Parameters:

  • path (String)

    API path relative to configured url

  • body (Hash, String, nil) (defaults to: nil)

    request payload

  • content_type (String) (defaults to: 'application/json')

    MIME type of request body

  • exception_args (Hash)

    supplemental context forwarded to UnexpectedResponse

Returns:

  • (Hash, Array, nil)

    parsed JSON body, or nil for empty body

Raises:



145
146
147
148
149
150
151
152
153
154
# File 'lib/folio_client.rb', line 145

def put(path, body = nil, content_type: 'application/json', **exception_args)
  req_body = content_type == 'application/json' ? body&.to_json : body
  response = with_token_refresh_when_unauthorized do
    connection.put(path, req_body, { content_type: content_type })
  end

  UnexpectedResponse.call(response, **exception_args) unless response.success?

  JSON.parse(response.body) if response.body.present?
end

#update_holdingsObject

Update an existing holdings record.

Returns:

  • (Object)

    delegated return value from Inventory#update_holdings

See Also:



213
214
215
# File 'lib/folio_client.rb', line 213

def update_holdings(...)
  inventory.update_holdings(...)
end

#user_detailsObject

Fetch details for a user.

Returns:

  • (Object)

    delegated return value from Users#fetch_user_details

See Also:



297
298
299
# File 'lib/folio_client.rb', line 297

def user_details(...)
  users_service.fetch_user_details(...)
end

#usersObject

List users.

Returns:

  • (Object)

    delegated return value from Users#fetch_list

See Also:



290
291
292
# File 'lib/folio_client.rb', line 290

def users(...)
  users_service.fetch_list(...)
end