Skip to content

Commit

Permalink
Handle non-successful response from borrowdirect
Browse files Browse the repository at this point in the history
  • Loading branch information
corylown committed Dec 4, 2024
1 parent 6d77952 commit ceea129
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/models/borrow_direct_reshare_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def reshare_requests
request_client.requests(patron_university_id).map do |request|
ReshareRequest.new(request)
end
rescue BorrowDirectReshareClient::BorrowDirectError => e
Honeybadger.notify(e)
[]
end

def request_client
Expand Down
14 changes: 11 additions & 3 deletions app/services/borrow_direct_reshare_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'http'

class BorrowDirectReshareClient
class BorrowDirectError < StandardError; end

DEFAULT_HEADERS = {
accept: 'application/json, text/plain',
content_type: 'application/json'
Expand All @@ -25,7 +27,7 @@ def get(path, **)
end

def get_json(path, **)
parse(get(path, **))
parse(check_response(get(path, **)))
end

def requests(university_id)
Expand All @@ -45,8 +47,14 @@ def ping

private

def check_response(response)
return response if [200, 201].include?(response.status)

raise BorrowDirectError, "Response status: #{response.status}; Response body: #{response.body}"
end

def parse(response)
return nil if !response || response.body.empty?
return nil unless response.respond_to?(:body) && !response.body.empty?

JSON.parse(response.body)
rescue JSON::ParserError => e
Expand All @@ -56,7 +64,7 @@ def parse(response)
def session_token
@session_token ||= begin
response = request('/authn/login', json: { username: @username, password: @password }, method: :post)
response ? response['x-okapi-token'] : nil
check_response(response) ? response['x-okapi-token'] : nil
end
end

Expand Down

0 comments on commit ceea129

Please sign in to comment.