Skip to content

Commit

Permalink
Merge pull request #8736 from dependabot/jamiemage/strong-type-regist…
Browse files Browse the repository at this point in the history
…ry-client

Strong type `Dependabot::RegistryClient`
  • Loading branch information
bdragon committed Jan 11, 2024
2 parents e7698c0 + 6996fac commit c547fb3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
30 changes: 26 additions & 4 deletions common/lib/dependabot/registry_client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# typed: strong
# frozen_string_literal: true

require "sorbet-runtime"
require "dependabot/shared_helpers"

# This class provides a thin wrapper around our normal usage of Excon as a simple HTTP client in order to
Expand All @@ -11,10 +12,20 @@
# read-timeouts as some jobs tend to be sensitive to exceeding our overall 45 minute timeout.
module Dependabot
class RegistryClient
@cached_errors = {}
extend T::Sig

@cached_errors = T.let({}, T::Hash[T.nilable(String), Excon::Error::Timeout])

sig do
params(
url: String,
headers: T::Hash[Symbol, T.untyped],
options: T::Hash[Symbol, T.untyped]
)
.returns(Excon::Response)
end
def self.get(url:, headers: {}, options: {})
raise cached_error_for(url) if cached_error_for(url)
raise T.must(cached_error_for(url)) if cached_error_for(url)

Excon.get(
url,
Expand All @@ -26,8 +37,16 @@ def self.get(url:, headers: {}, options: {})
raise e
end

sig do
params(
url: String,
headers: T::Hash[Symbol, T.untyped],
options: T::Hash[Symbol, T.untyped]
)
.returns(Excon::Response)
end
def self.head(url:, headers: {}, options: {})
raise cached_error_for(url) if cached_error_for(url)
raise T.must(cached_error_for(url)) if cached_error_for(url)

Excon.head(
url,
Expand All @@ -39,15 +58,18 @@ def self.head(url:, headers: {}, options: {})
raise e
end

sig { void }
def self.clear_cache!
@cached_errors = {}
end

sig { params(url: String, error: Excon::Error::Timeout).void }
private_class_method def self.cache_error(url, error)
host = URI(url).host
@cached_errors[host] = error
end

sig { params(url: String).returns(T.nilable(Excon::Error::Timeout)) }
private_class_method def self.cached_error_for(url)
host = URI(url).host
@cached_errors.fetch(host, nil)
Expand Down
4 changes: 2 additions & 2 deletions common/spec/dependabot/registry_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
end

before do
allow(Excon).to receive(:get)
allow(Excon).to receive(:head)
allow(Excon).to receive(:get).and_return(Excon::Response.new)
allow(Excon).to receive(:head).and_return(Excon::Response.new)
end

describe "delegation to Excon" do
Expand Down
10 changes: 10 additions & 0 deletions sorbet/rbi/shims/excon.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ module Excon
.returns(Excon::Response)
end
def get(url, params = T.unsafe(nil), &block); end

sig do
params(
url: String,
params: T.untyped,
block: T.untyped
)
.returns(Excon::Response)
end
def head(url, params = T.unsafe(nil), &block); end
end

class Response
Expand Down

0 comments on commit c547fb3

Please sign in to comment.