php: fetch: prices: coingecko: remove upstream client, add API key support

- Removes `codenixsv/coingecko-api`
- Adds Pro API support
- Related refactoring
This commit is contained in:
2024-06-15 19:07:56 -07:00
parent 52026cace1
commit e632c44bc3
2 changed files with 39 additions and 10 deletions

View File

@@ -24,6 +24,7 @@
*/ */
//! @since docker-finance 1.0.0 //! @since docker-finance 1.0.0
namespace docker_finance\prices\internal namespace docker_finance\prices\internal
{ {
require_once('utils/utils.php'); require_once('utils/utils.php');
@@ -138,15 +139,21 @@ namespace docker_finance\prices\internal
/** /**
* @brief Request's common implementation * @brief Request's common implementation
* @param string $url REST API URL * @param string $url REST API URL
* @param array<string> $header Impl-specific header addendum
* @return mixed Response data * @return mixed Response data
*/ */
protected function request_impl(string $url): mixed protected function request_impl(string $url, array $header): mixed
{ {
$headers = array( $headers = array(
'Accept: application/json', 'Accept: application/json',
'Content-Type: application/json', 'Content-Type: application/json',
); );
if (!empty($header)) {
$headers = array_merge($headers, $header);
}
utils\CLI::print_debug($headers);
$ch = curl_init($url); $ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
@@ -164,7 +171,15 @@ namespace docker_finance\prices\internal
utils\CLI::throw_fatal("cURL null response"); utils\CLI::throw_fatal("cURL null response");
} }
return $response; $decoded = json_decode(
$response,
true,
512,
JSON_PRETTY_PRINT
);
utils\CLI::print_debug($decoded);
return $decoded;
} }
//! @brief Impl-specific REST API request //! @brief Impl-specific REST API request

View File

@@ -24,9 +24,9 @@
*/ */
//! @since docker-finance 1.0.0 //! @since docker-finance 1.0.0
namespace docker_finance\prices\internal\prices\coingecko namespace docker_finance\prices\internal\prices\coingecko
{ {
require_once('php/vendor/autoload.php'); //!< CoinGecko
require_once('prices/internal/base.php'); require_once('prices/internal/base.php');
require_once('utils/utils.php'); require_once('utils/utils.php');
@@ -39,13 +39,9 @@ namespace docker_finance\prices\internal\prices\coingecko
*/ */
final class CoinGecko extends internal\Impl final class CoinGecko extends internal\Impl
{ {
private \Codenixsv\CoinGeckoApi\CoinGeckoClient $api; //!< CoinGecko
public function __construct(utils\Env $env) public function __construct(utils\Env $env)
{ {
parent::__construct($env); parent::__construct($env);
$this->api = new \Codenixsv\CoinGeckoApi\CoinGeckoClient();
} }
/** /**
@@ -56,9 +52,27 @@ namespace docker_finance\prices\internal\prices\coingecko
*/ */
protected function request(string $id, string $timestamp): mixed protected function request(string $id, string $timestamp): mixed
{ {
// TODO(afiore): use request_impl() after removing CoinGeckoClient // If `key` exists, use Pro API (otherwise, use Public API)
$response = $this->api->coins()->getMarketChart($id, 'usd', $timestamp); $key = $this->get_env()->get_env('API_PRICES_KEY');
return $response['prices']; $domain = 'api.coingecko.com';
$header = [];
if ($key != 'None') {
$domain = 'pro-' . $domain;
$header = ["x-cg-pro-api-key: $key"];
}
$vs_currency = 'usd';
$url = "https://{$domain}/api/v3/coins/{$id}/market_chart?vs_currency={$vs_currency}&days={$timestamp}";
$response = $this->request_impl($url, $header);
if (array_key_exists('status', $response)) {
$status = $response['status'];
throw new \Exception($status['error_message'], $status['error_code']);
}
$prices = $response['prices'];
utils\CLI::print_debug($prices);
return $prices;
} }
/** /**