From bab85d84e944001dbfaa4e672f6c6b937338e513 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Fri, 30 Dec 2022 01:01:52 -0500 Subject: [PATCH] Extract common JSON API fetch logic --- Library/Homebrew/api.rb | 20 ++++++++++++++++++++ Library/Homebrew/api/cask.rb | 26 +------------------------- Library/Homebrew/api/formula.rb | 33 ++------------------------------- 3 files changed, 23 insertions(+), 56 deletions(-) diff --git a/Library/Homebrew/api.rb b/Library/Homebrew/api.rb index e4028b2678..a6dadf6fa6 100644 --- a/Library/Homebrew/api.rb +++ b/Library/Homebrew/api.rb @@ -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 diff --git a/Library/Homebrew/api/cask.rb b/Library/Homebrew/api/cask.rb index 949c739489..19d1166164 100644 --- a/Library/Homebrew/api/cask.rb +++ b/Library/Homebrew/api/cask.rb @@ -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")] diff --git a/Library/Homebrew/api/formula.rb b/Library/Homebrew/api/formula.rb index 0e62566220..8d6d1bc605 100644 --- a/Library/Homebrew/api/formula.rb +++ b/Library/Homebrew/api/formula.rb @@ -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|