folio_api_client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 954b7e6b1e48039f364a809e5106625b64d86befca6cfb581b044395b793cc4b
4
+ data.tar.gz: afa3897ee6b420e72e38cced7c3206d9bdefed076214a3bea7216b6abf5bb2e1
5
+ SHA512:
6
+ metadata.gz: a3ed8ef0912573e43adc2263ab65e5096f06bdd6118946fa5e2b9276bdd17e2ea9e88fe98027c1d0d2040a30036afff7ff009d70ee8bf87c11e2be87912e7ce4
7
+ data.tar.gz: 8ff84094f0a2be25a608e313bd29369d3f72a295be31903aa97b261c041fcd4361c4690c2fb609766670b9ed3dd74473aa19dc3c27142eccaa188c2f9bcddcbb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ inherit_gem:
2
+ rubocul: rubocul_default.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 3.2 # Update to your version of ruby
6
+
7
+ Rails/Output:
8
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # FolioApiClient
2
+
3
+ A Ruby interface for making requests to the FOLIO ILS API (https://folio.org), including some convenience methods that have been useful for the Columbia University Libraries.
4
+
5
+ ## Installation
6
+
7
+ At this time, this gem is only available on GitHub and has not been published to RubyGems yet. You can include it in your Gemfile using this syntax:
8
+
9
+ `gem 'folio_api_client', github: 'cul/folio_api_client', branch: 'main'`
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ # Create a client
15
+ client = FolioApiClient.new(FolioApiClient::Configuration.new(
16
+ url: 'https://development.example.com',
17
+ username: 'username',
18
+ password: 'password',
19
+ tenant: 'abc123',
20
+ timeout: 10
21
+ ))
22
+
23
+ # Make some requests
24
+
25
+ instance_record_id = '65e07045-4d68-4a07-835e-33f0c80482ab'
26
+ instance_record_response = client.get("/instance-storage/instances/#{instance_record_id}")
27
+ puts JSON.parse(instance_record_response.body)
28
+
29
+ holdings_record_id = 'b2e9946b-82fe-4cfb-ad4c-b3c452379dae'
30
+ holdings_record_response = client.get("/holdings-storage/holdings/#{holdings_record_id}")
31
+ puts JSON.parse(holdings_record_response.body)
32
+
33
+ # Generic request syntax for any FOLIO REST endpoint:
34
+
35
+ client.get(path, params)
36
+ client.post(path, body, content_type: 'application/json'))
37
+ client.put(path, body, content_type: 'application/json'))
38
+ client.delete(path, body, content_type: 'application/json'))
39
+ ```
40
+
41
+ See [https://dev.folio.org/reference/api/](https://dev.folio.org/reference/api/) for the full list of available FOLIO API endpoints.
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To release a new version of this gem, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at: https://github.com/cul/folio_api_client
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class FolioApiClient
4
+ # Data structure that stores FolioApiClient configuration options.
5
+ class Configuration
6
+ DEFAULT_TIMEOUT = 60
7
+ DEFAULT_USER_AGENT = 'FolioApiClient'
8
+
9
+ attr_reader :url, :username, :password, :tenant, :timeout, :user_agent
10
+ attr_accessor :token
11
+
12
+ def initialize(url:, username:, password:, tenant:, timeout: DEFAULT_TIMEOUT, user_agent: DEFAULT_USER_AGENT)
13
+ @url = url
14
+ @username = username
15
+ @password = password
16
+ @tenant = tenant
17
+ @timeout = timeout
18
+ @user_agent = user_agent
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class FolioApiClient
4
+ module Exceptions
5
+ class Error < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class FolioApiClient
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zeitwerk'
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.setup # ready!
6
+
7
+ require 'faraday'
8
+
9
+ # A client used for making http requests (get/post/etc.) to the FOLIO ILS REST API.
10
+ class FolioApiClient
11
+ attr_reader :config
12
+
13
+ def initialize(config)
14
+ @config = config
15
+ end
16
+
17
+ def headers_for_connection
18
+ {
19
+ 'Accept': 'application/json, text/plain',
20
+ 'Content-Type': 'application/json',
21
+ 'X-Okapi-Tenant': config.tenant,
22
+ 'User-Agent': config.user_agent
23
+ }
24
+ end
25
+
26
+ def connection
27
+ @connection ||= Faraday.new(
28
+ headers: headers_for_connection,
29
+ url: config.url,
30
+ request: { timeout: config.timeout }
31
+ ) do |faraday|
32
+ faraday.adapter Faraday.default_adapter
33
+ faraday.use Faraday::Response::RaiseError
34
+ end
35
+ end
36
+
37
+ def retrieve_new_auth_token
38
+ response = connection.post('/authn/login', { username: config.username, password: config.password }.to_json)
39
+ response_data = JSON.parse(response.body)
40
+ response_data['okapiToken']
41
+ end
42
+
43
+ def refresh_auth_token!
44
+ config.token = retrieve_new_auth_token
45
+ end
46
+
47
+ def with_token_refresh_attempt_when_unauthorized
48
+ refresh_auth_token! if config.token.nil?
49
+ yield
50
+ rescue Faraday::UnauthorizedError, Faraday::ForbiddenError
51
+ # Tokens are sometimes invalidated by FOLIO data refreshes, so we'll attempt to refresh our token
52
+ # one time in responde to a 401 or 403.
53
+ refresh_auth_token!
54
+
55
+ # Re-run block
56
+ yield
57
+ end
58
+
59
+ def get(path, params = {})
60
+ response = with_token_refresh_attempt_when_unauthorized do
61
+ connection.get(path, params, { 'x-okapi-token': config.token })
62
+ end
63
+
64
+ JSON.parse(response.body)
65
+ end
66
+
67
+ def exec_request_with_body(method, path, body = nil, content_type: 'application/json')
68
+ body = body.to_json if content_type == 'application/json' && !body.is_a?(String)
69
+ response = with_token_refresh_attempt_when_unauthorized do
70
+ connection.send(method, path, body, { 'x-okapi-token': config.token, 'content-type': content_type })
71
+ end
72
+
73
+ response.body.nil? ? nil : JSON.parse(response.body)
74
+ end
75
+
76
+ def post(path, body = nil, content_type: 'application/json')
77
+ exec_request_with_body(:post, path, body, content_type: content_type)
78
+ end
79
+
80
+ def put(path, body = nil, content_type: 'application/json')
81
+ exec_request_with_body(:put, path, body, content_type: content_type)
82
+ end
83
+
84
+ def delete(path, body = nil, content_type: 'application/json')
85
+ exec_request_with_body(:delete, path, body, content_type: content_type)
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: folio_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric O'Hanlon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: marc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ description: This gem provides an interface for making requests to the FOLIO ILS API,
56
+ and makes session management easier.
57
+ email:
58
+ - elo2112@columbia.edu
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".ruby-version"
66
+ - README.md
67
+ - Rakefile
68
+ - lib/folio_api_client.rb
69
+ - lib/folio_api_client/configuration.rb
70
+ - lib/folio_api_client/exceptions.rb
71
+ - lib/folio_api_client/version.rb
72
+ homepage: https://www.github.com/cul/folio_api_client
73
+ licenses: []
74
+ metadata:
75
+ homepage_uri: https://www.github.com/cul/folio_api_client
76
+ source_code_uri: https://www.github.com/cul/folio_api_client
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 3.2.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.4.10
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: A Ruby interface for making requests to the FOLIO ILS API.
96
+ test_files: []