Extract common JSON API fetch logic
This commit is contained in:
parent
374b61584b
commit
bab85d84e9
@ -21,6 +21,7 @@ module Homebrew
|
||||
|
||||
API_DOMAIN = "https://formulae.brew.sh/api"
|
||||
HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze
|
||||
MAX_RETRIES = 3
|
||||
|
||||
sig { params(endpoint: String, json: T::Boolean).returns(T.any(String, Hash)) }
|
||||
def fetch(endpoint, json: true)
|
||||
@ -38,5 +39,24 @@ module Homebrew
|
||||
rescue JSON::ParserError
|
||||
raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}"
|
||||
end
|
||||
|
||||
def fetch_json_api_file(endpoint, target:)
|
||||
retry_count = 0
|
||||
|
||||
url = "#{API_DOMAIN}/#{endpoint}"
|
||||
begin
|
||||
curl_args = %W[--compressed --silent #{url}]
|
||||
curl_args.prepend("--time-cond", target) if target.exist? && !target.empty?
|
||||
Utils::Curl.curl_download(*curl_args, to: target, max_time: 5)
|
||||
|
||||
JSON.parse(target.read)
|
||||
rescue JSON::ParserError
|
||||
target.unlink
|
||||
retry_count += 1
|
||||
odie "Cannot download non-corrupt #{url}!" if retry_count > MAX_RETRIES
|
||||
|
||||
retry
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -10,13 +10,6 @@ module Homebrew
|
||||
class << self
|
||||
extend T::Sig
|
||||
|
||||
MAX_RETRIES = 3
|
||||
|
||||
sig { returns(String) }
|
||||
def cached_cask_json_file
|
||||
HOMEBREW_CACHE_API/"cask.json"
|
||||
end
|
||||
|
||||
sig { params(name: String).returns(Hash) }
|
||||
def fetch(name)
|
||||
Homebrew::API.fetch "cask/#{name}.json"
|
||||
@ -25,24 +18,7 @@ module Homebrew
|
||||
sig { returns(Hash) }
|
||||
def all_casks
|
||||
@all_casks ||= begin
|
||||
retry_count = 0
|
||||
|
||||
url = "https://formulae.brew.sh/api/cask.json"
|
||||
json_casks = begin
|
||||
curl_args = %W[--compressed --silent #{url}]
|
||||
if cached_cask_json_file.exist? && !cached_cask_json_file.empty?
|
||||
curl_args.prepend("--time-cond", cached_cask_json_file)
|
||||
end
|
||||
curl_download(*curl_args, to: cached_cask_json_file, max_time: 5)
|
||||
|
||||
JSON.parse(cached_cask_json_file.read)
|
||||
rescue JSON::ParserError
|
||||
cached_cask_json_file.unlink
|
||||
retry_count += 1
|
||||
odie "Cannot download non-corrupt #{url}!" if retry_count > MAX_RETRIES
|
||||
|
||||
retry
|
||||
end
|
||||
json_casks = Homebrew::API.fetch_json_api_file "cask.json", target: HOMEBREW_CACHE_API/"cask.json"
|
||||
|
||||
json_casks.to_h do |json_cask|
|
||||
[json_cask["token"], json_cask.except("token")]
|
||||
|
||||
@ -10,19 +10,6 @@ module Homebrew
|
||||
class << self
|
||||
extend T::Sig
|
||||
|
||||
MAX_RETRIES = 3
|
||||
|
||||
sig { returns(String) }
|
||||
def formula_api_path
|
||||
"formula"
|
||||
end
|
||||
alias generic_formula_api_path formula_api_path
|
||||
|
||||
sig { returns(String) }
|
||||
def cached_formula_json_file
|
||||
HOMEBREW_CACHE_API/"#{formula_api_path}.json"
|
||||
end
|
||||
|
||||
sig { params(name: String).returns(Hash) }
|
||||
def fetch(name)
|
||||
Homebrew::API.fetch "#{formula_api_path}/#{name}.json"
|
||||
@ -31,24 +18,8 @@ module Homebrew
|
||||
sig { returns(Hash) }
|
||||
def all_formulae
|
||||
@all_formulae ||= begin
|
||||
retry_count = 0
|
||||
|
||||
url = "https://formulae.brew.sh/api/formula.json"
|
||||
json_formulae = begin
|
||||
curl_args = %W[--compressed --silent #{url}]
|
||||
if cached_formula_json_file.exist? && !cached_formula_json_file.empty?
|
||||
curl_args.prepend("--time-cond", cached_formula_json_file)
|
||||
end
|
||||
curl_download(*curl_args, to: cached_formula_json_file, max_time: 5)
|
||||
|
||||
JSON.parse(cached_formula_json_file.read)
|
||||
rescue JSON::ParserError
|
||||
cached_formula_json_file.unlink
|
||||
retry_count += 1
|
||||
odie "Cannot download non-corrupt #{url}!" if retry_count > MAX_RETRIES
|
||||
|
||||
retry
|
||||
end
|
||||
json_formulae = Homebrew::API.fetch_json_api_file "formula.json",
|
||||
target: HOMEBREW_CACHE_API/"formula.json"
|
||||
|
||||
@all_aliases = {}
|
||||
json_formulae.to_h do |json_formula|
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user