2021-08-06 02:30:44 -04:00
|
|
|
# typed: false
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "api/analytics"
|
|
|
|
require "api/cask"
|
|
|
|
require "api/formula"
|
|
|
|
require "api/versions"
|
2021-08-09 10:29:55 -04:00
|
|
|
require "extend/cachable"
|
2021-08-06 02:30:44 -04:00
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
# Helper functions for using Homebrew's formulae.brew.sh API.
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
module API
|
|
|
|
extend T::Sig
|
|
|
|
|
2021-08-09 10:29:55 -04:00
|
|
|
extend Cachable
|
|
|
|
|
2021-08-06 02:30:44 -04:00
|
|
|
module_function
|
|
|
|
|
|
|
|
API_DOMAIN = "https://formulae.brew.sh/api"
|
2021-12-07 00:13:56 +00:00
|
|
|
HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze
|
2022-12-30 01:01:52 -05:00
|
|
|
MAX_RETRIES = 3
|
2021-08-06 02:30:44 -04:00
|
|
|
|
2023-01-26 17:36:40 +00:00
|
|
|
sig { params(endpoint: String).returns(Hash) }
|
|
|
|
def fetch(endpoint)
|
2021-08-09 10:29:55 -04:00
|
|
|
return cache[endpoint] if cache.present? && cache.key?(endpoint)
|
2021-08-06 02:30:44 -04:00
|
|
|
|
|
|
|
api_url = "#{API_DOMAIN}/#{endpoint}"
|
2021-09-06 22:56:25 -04:00
|
|
|
output = Utils::Curl.curl_output("--fail", api_url, max_time: 5)
|
2021-08-06 02:30:44 -04:00
|
|
|
raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success?
|
|
|
|
|
2023-01-26 17:36:40 +00:00
|
|
|
cache[endpoint] = JSON.parse(output.stdout)
|
2021-08-06 02:30:44 -04:00
|
|
|
rescue JSON::ParserError
|
|
|
|
raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}"
|
|
|
|
end
|
2022-12-30 01:01:52 -05:00
|
|
|
|
2023-01-26 17:36:40 +00:00
|
|
|
sig { params(endpoint: String, target: Pathname).returns(Hash) }
|
2022-12-30 01:01:52 -05:00
|
|
|
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
|
2023-01-26 17:36:40 +00:00
|
|
|
|
2023-01-28 02:16:09 -06:00
|
|
|
sig { params(filepath: String, repo: String, git_head: T.nilable(String)).returns(String) }
|
2023-01-28 02:15:00 -06:00
|
|
|
def fetch_file_source(filepath, repo:, git_head: nil)
|
2023-01-26 17:36:40 +00:00
|
|
|
git_head ||= "master"
|
2023-01-28 02:15:00 -06:00
|
|
|
endpoint = "#{git_head}/#{filepath}"
|
2023-01-26 17:36:40 +00:00
|
|
|
return cache[endpoint] if cache.present? && cache.key?(endpoint)
|
|
|
|
|
2023-01-28 02:15:00 -06:00
|
|
|
raw_url = "https://raw.githubusercontent.com/#{repo}/#{endpoint}"
|
2023-01-26 17:36:40 +00:00
|
|
|
output = Utils::Curl.curl_output("--fail", raw_url, max_time: 5)
|
|
|
|
raise ArgumentError, "No file found at #{Tty.underline}#{raw_url}#{Tty.reset}" unless output.success?
|
|
|
|
|
|
|
|
cache[endpoint] = output.stdout
|
|
|
|
end
|
2021-08-06 02:30:44 -04:00
|
|
|
end
|
|
|
|
end
|