Skip to content

coinprices/PriceHttpClient.hpp

Namespaces

Name
sgns

Classes

Name
class sgns::PriceHttpClient
Price-fetch facade over the status-aware sgns::HTTPClient transport (02-02): applies the status-before-parse gate (LPM-05), CoinGecko-friendly UA and 5s timeouts (LPM-06, D-08/D-10), the transient-only retry schedule with 429 hold-off (LPM-07, D-11..D-13), and produces PriceQuote objects with fetch-time timestamps (D-14).

Source code

#pragma once

#include "PriceFetchError.hpp"
#include "PriceQuote.hpp"
#include "PriceResponseParsers.hpp"
#include "PriceRetryPolicy.hpp"

#include "boost/asio.hpp"

#include <atomic>
#include <chrono>
#include <memory>
#include <string>
#include <vector>

#include "base/logger.hpp"

namespace sgns
{
    class PriceHttpClient
    {
    public:
        PriceHttpClient( std::string                         baseUrl,
                         RetryConfig                          retryConfig          = {},
                         std::chrono::seconds                holdOffDuration      = std::chrono::seconds( 60 ),
                         RateLimitHoldOff::Clock              clock                = [] { return std::chrono::system_clock::now(); },
                         std::chrono::milliseconds           requestTimeout      = std::chrono::milliseconds( 5000 ),
                         ResponseFormat                       responseFormat       = ResponseFormat::CoinGeckoSimplePrice );

        PriceResult<std::vector<PriceQuote>> FetchPrices( std::shared_ptr<boost::asio::io_context> ioc,
                                                          const std::vector<std::string>          &tokenIds,
                                                          const std::string                       &currency = "usd" );

        int AttemptsLastFetch() const
        {
            return attemptsLastFetch_.load();
        }

        static constexpr const char *kUserAgent = "SGNS-PriceClient/1.0 (+https://gnus.ai; SuperGenius node price fetch)";

    private:
        std::string          baseUrl_;
        RetryConfig          retryConfig_;
        std::chrono::seconds holdOffDuration_;
        RateLimitHoldOff     holdOff_;
        std::chrono::milliseconds requestTimeout_;
        ResponseFormat       responseFormat_;
        std::atomic<int>     attemptsLastFetch_{ 0 };
        base::Logger         m_logger = sgns::base::createLogger( "PriceHttpClient" );
    };
} // namespace sgns

Updated on 2026-10-09 at 04:02:50 +0000