Skip to content

Commit

Permalink
Add timeouts + user agents to faraday requests too
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Aug 20, 2024
1 parent 56c408b commit aea0286
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 5 additions & 1 deletion app/models/cocina.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class Cocina
def self.find(druid, version = :head)
data = Rails.cache.fetch(metadata_cache_key(druid, version), expires_in: 10.minutes) do
benchmark "Fetching public json for #{druid} version #{version}" do
response = Faraday.get(public_json_url(druid, version))
connection = Faraday.new({ url: public_json_url(druid, version),
headers: { user_agent: Settings.user_agent },
request: { open_timeout: 5 } })

response = connection.get
raise Purl::Exception, response.status unless response.success?

JSON.parse(response.body)
Expand Down
7 changes: 5 additions & 2 deletions app/services/metrics_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def event_data(name, properties)
end

def default_headers
{ 'Content-Type': 'application/json' }
{
'Content-Type': 'application/json',
'User-Agent': Settings.user_agent
}
end

def post_json(url, data, headers)
Expand All @@ -50,6 +53,6 @@ def post_json(url, data, headers)
end

def connection
@connection ||= Faraday.new(base_url)
@connection ||= Faraday.new({ url: base_url, request: { open_timeout: 5 } })
end
end
17 changes: 10 additions & 7 deletions spec/controllers/object_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
allow_any_instance_of(StacksFile).to receive(:path).and_return(Rails.root + 'Gemfile')
end

let(:connection) { instance_double(Faraday::Connection) }

describe '#show' do
context 'when not logged in' do
context "with an invalid druid" do
let(:druid) { 'foo' }

it 'returns a 404 Not Found' do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/foo.json').and_return(
allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/foo.json')).and_return(connection)
allow(connection).to receive(:get).and_return(
instance_double(Faraday::Response, status: 404, success?: false)
)
get :show, params: { id: 'foo' }
Expand Down Expand Up @@ -89,8 +92,8 @@
end

before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/fd063dh3727.json')
.and_return(instance_double(Faraday::Response, success?: true, body: json))
allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/fd063dh3727.json')).and_return(connection)
allow(connection).to receive(:get).and_return(instance_double(Faraday::Response, success?: true, body: json))
end

it 'creates a zip' do
Expand Down Expand Up @@ -145,8 +148,8 @@
end

before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.json')
.and_return(instance_double(Faraday::Response, success?: true, body: json))
allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/bb142ws0723.json')).and_return(connection)
allow(connection).to receive(:get).and_return(instance_double(Faraday::Response, success?: true, body: json))
end

it 'redirects to login' do
Expand Down Expand Up @@ -209,8 +212,8 @@
end

before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.json')
.and_return(instance_double(Faraday::Response, success?: true, body: json))
allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/bb142ws0723.json')).and_return(connection)
allow(connection).to receive(:get).and_return(instance_double(Faraday::Response, success?: true, body: json))
end

it 'creates a zip' do
Expand Down

0 comments on commit aea0286

Please sign in to comment.