Skip to content

Commit

Permalink
Merge pull request #63 from sul-dlss/bump-connection-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Mar 31, 2023
2 parents 3834717 + f4cb6ed commit d0e7706
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
16 changes: 12 additions & 4 deletions lib/folio_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ServiceUnavailable < Error; end
# Error raised when the Folio API returns a 422 Unprocessable Entity
class ValidationError < Error; end

# Error raised when the Folio API returns a 409 Conflict
class ConflictError < Error; end

DEFAULT_HEADERS = {
accept: "application/json, text/plain",
content_type: "application/json"
Expand All @@ -44,15 +47,15 @@ class << self
# @param url [String] the folio API URL
# @param login_params [Hash] the folio client login params (username:, password:)
# @param okapi_headers [Hash] the okapi specific headers to add (X-Okapi-Tenant:, User-Agent:)
def configure(url:, login_params:, okapi_headers:)
instance.config = OpenStruct.new(url: url, login_params: login_params, okapi_headers: okapi_headers, token: nil)
def configure(url:, login_params:, okapi_headers:, timeout: default_timeout)
instance.config = OpenStruct.new(url: url, login_params: login_params, okapi_headers: okapi_headers, token: nil, timeout: timeout)

instance.config.token = Authenticator.token(login_params, connection)

self
end

delegate :config, :connection, :get, :post, :put, to: :instance
delegate :config, :connection, :get, :post, :put, :default_timeout, to: :instance
delegate :fetch_hrid, :fetch_external_id, :fetch_instance_info, :fetch_marc_hash, :has_instance_status?, :data_import, :edit_marc_json,
:organizations, :organization_interfaces, :interface_details, to: :instance
end
Expand Down Expand Up @@ -120,7 +123,8 @@ def put(path, body = nil, content_type: "application/json")
def connection
@connection ||= Faraday.new(
url: config.url,
headers: DEFAULT_HEADERS.merge(config.okapi_headers || {})
headers: DEFAULT_HEADERS.merge(config.okapi_headers || {}),
request: {timeout: config.timeout}
)
end

Expand Down Expand Up @@ -192,4 +196,8 @@ def interface_details(...)
.new(self)
.fetch_interface_details(...)
end

def default_timeout
120
end
end
6 changes: 4 additions & 2 deletions lib/folio_client/unexpected_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def self.call(response)
raise ForbiddenError, "The operation requires privileges which the client does not have: #{response.body}"
when 404
raise ResourceNotFound, "Endpoint not found or resource does not exist: #{response.body}"
when 409
raise ConflictError, "Resource cannot be updated: #{response.body}"
when 422
raise ValidationError, "There was a validation problem with the request: #{response.body} "
raise ValidationError, "There was a validation problem with the request: #{response.body}"
when 500
raise ServiceUnavailable, "The remote server returned an internal server error."
raise ServiceUnavailable, "The remote server returned an internal server error: #{response.body}"
else
raise StandardError, "Unexpected response: #{response.status} #{response.body}"
end
Expand Down
9 changes: 9 additions & 0 deletions spec/folio_client/authenticator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
end
end

context "when the service returns a 409 conflict error" do
let(:http_status) { 409 }
let(:http_body) { "{\"error\" : \"get bent\"}" }

it "raises a conflict error exception" do
expect { authenticator.token }.to raise_error(FolioClient::ConflictError)
end
end

context "when the truly unexpected happens" do
let(:http_status) { 666 }
let(:http_body) { "{\"error\" : \"huh?\"}" }
Expand Down
4 changes: 4 additions & 0 deletions spec/folio_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
expect(client.config.okapi_headers).to eq(okapi_headers)
end

it "gets the default timeout value" do
expect(client.config.timeout).to eq(120)
end

it "stores the fetched token in the config" do
expect(client.config.token).to eq(token)
end
Expand Down

0 comments on commit d0e7706

Please sign in to comment.