Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code refactoring] - Extract Permanent curl option builder method fro… #616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/api/common/include/cache-file-updator-interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

namespace cct {

class CacheFileUpdatorInterface {
public:
virtual ~CacheFileUpdatorInterface() = default;

virtual void updateCacheFile() const {}
};

} // namespace cct
4 changes: 2 additions & 2 deletions src/api/common/include/commonapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include <string_view>

#include "binance-common-api.hpp"
#include "cache-file-updator-interface.hpp"
#include "cachedresult.hpp"
#include "curlhandle.hpp"
#include "currencycode.hpp"
#include "currencycodeset.hpp"
#include "currencycodevector.hpp"
#include "exchangebase.hpp"
#include "monetaryamount.hpp"
#include "monetaryamountbycurrencyset.hpp"
#include "timedef.hpp"
Expand All @@ -22,7 +22,7 @@ namespace cct {
class CoincenterInfo;
namespace api {
/// Public API connected to different exchanges, providing fast methods to retrieve huge amount of data.
class CommonAPI : public ExchangeBase {
class CommonAPI : public CacheFileUpdatorInterface {
public:
enum class AtInit : int8_t { kLoadFromFileCache, kNoLoadFromFileCache };

Expand Down
25 changes: 25 additions & 0 deletions src/api/common/include/exchange-permanent-curl-options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <cstdint>

#include "permanentcurloptions.hpp"

namespace cct {

class ExchangeConfig;

namespace api {
class ExchangePermanentCurlOptions {
public:
explicit ExchangePermanentCurlOptions(const ExchangeConfig &exchangeConfig);

enum class Api : int8_t { Public, Private };

PermanentCurlOptions::Builder builderBase(Api api) const;

private:
const ExchangeConfig &_exchangeConfig;
};
} // namespace api

} // namespace cct
56 changes: 0 additions & 56 deletions src/api/common/include/exchangebase.hpp

This file was deleted.

7 changes: 5 additions & 2 deletions src/api/common/include/exchangeprivateapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include "apikey.hpp"
#include "balanceoptions.hpp"
#include "balanceportfolio.hpp"
#include "cache-file-updator-interface.hpp"
#include "cachedresultvault.hpp"
#include "currencycode.hpp"
#include "currencyexchangeflatset.hpp"
#include "depositsconstraints.hpp"
#include "exchangebase.hpp"
#include "exchangeconfig.hpp"
#include "exchangeprivateapitypes.hpp"
#include "exchangepublicapi.hpp"
Expand All @@ -20,6 +20,7 @@
#include "monetaryamountbycurrencyset.hpp"
#include "orderid.hpp"
#include "ordersconstraints.hpp"
#include "permanentcurloptions.hpp"
#include "tradedamounts.hpp"
#include "tradeinfo.hpp"
#include "wallet.hpp"
Expand All @@ -35,7 +36,7 @@ class WithdrawOptions;
namespace api {
class APIKey;

class ExchangePrivate : public ExchangeBase {
class ExchangePrivate : public CacheFileUpdatorInterface {
public:
virtual ~ExchangePrivate() = default;

Expand Down Expand Up @@ -164,6 +165,8 @@ class ExchangePrivate : public ExchangeBase {

TradedAmounts marketTrade(MonetaryAmount from, const TradeOptions &tradeOptions, Market mk);

PermanentCurlOptions::Builder permanentCurlOptionsBuilder() const;

ExchangePublic &_exchangePublic;
CachedResultVault &_cachedResultVault{_exchangePublic._cachedResultVault};
const CoincenterInfo &_coincenterInfo;
Expand Down
13 changes: 8 additions & 5 deletions src/api/common/include/exchangepublicapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
#include <optional>
#include <string_view>

#include "cache-file-updator-interface.hpp"
#include "commonapi.hpp"
#include "currencycode.hpp"
#include "currencycodeset.hpp"
#include "currencyexchangeflatset.hpp"
#include "exchangebase.hpp"
#include "exchangepublicapitypes.hpp"
#include "market-order-book-vector.hpp"
#include "market-timestamp-set.hpp"
#include "market.hpp"
#include "marketorderbook.hpp"
#include "monetaryamount.hpp"
#include "monetaryamountbycurrencyset.hpp"
#include "permanentcurloptions.hpp"
#include "priceoptions.hpp"
#include "public-trade-vector.hpp"
#include "time-window.hpp"
Expand All @@ -31,7 +32,7 @@ class FiatConverter;

namespace api {

class ExchangePublic : public ExchangeBase {
class ExchangePublic : public CacheFileUpdatorInterface {
public:
static constexpr int kDefaultDepth = MarketOrderBook::kDefaultDepth;
static constexpr int kNbLastTradesDefault = 100;
Expand Down Expand Up @@ -191,17 +192,19 @@ class ExchangePublic : public ExchangeBase {
MarketOrderBookVector pullMarketOrderBooksForReplay(Market market, TimeWindow timeWindow);

protected:
ExchangePublic(std::string_view name, FiatConverter &fiatConverter, CommonAPI &commonApi,
const CoincenterInfo &coincenterInfo);

/// Retrieve the order book of given market.
/// It should be more precise that previous version with possibility to go deeper.
virtual MarketOrderBook queryOrderBook(Market mk, int depth = kDefaultDepth) = 0;

/// Retrieve an ordered vector of recent last trades
virtual PublicTradeVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) = 0;

friend class ExchangePrivate;
PermanentCurlOptions::Builder permanentCurlOptionsBuilder() const;

ExchangePublic(std::string_view name, FiatConverter &fiatConverter, CommonAPI &commonApi,
const CoincenterInfo &coincenterInfo);
friend class ExchangePrivate;

std::string_view _name;
CachedResultVault _cachedResultVault;
Expand Down
5 changes: 3 additions & 2 deletions src/api/common/include/fiatconverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string_view>
#include <unordered_map>

#include "cache-file-updator-interface.hpp"
#include "cct_string.hpp"
#include "curlhandle.hpp"
#include "currencycode.hpp"
Expand Down Expand Up @@ -33,7 +34,7 @@ class CoincenterInfo;
/// Fallback mechanism exists if api key does not exist or is expired.
///
/// Conversion methods are thread safe.
class FiatConverter {
class FiatConverter : public CacheFileUpdatorInterface {
public:
static File GetRatesCacheFile(std::string_view dataDir);

Expand All @@ -58,7 +59,7 @@ class FiatConverter {

/// Store rates in a file to make data persistent.
/// This method is not thread-safe and is expected to be called only once before end of normal termination of program.
void updateCacheFile() const;
void updateCacheFile() const override;

private:
struct PriceTimedValue {
Expand Down
5 changes: 3 additions & 2 deletions src/api/common/include/withdrawalfees-crawler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <unordered_map>
#include <utility>

#include "cache-file-updator-interface.hpp"
#include "cachedresult.hpp"
#include "cachedresultvault.hpp"
#include "coincenterinfo.hpp"
Expand All @@ -17,7 +18,7 @@ namespace cct {

/// This class is able to crawl some public withdrawal fees web pages in order to retrieve them from unofficial sources,
/// which is better than nothing. This class is non thread-safe.
class WithdrawalFeesCrawler {
class WithdrawalFeesCrawler : public CacheFileUpdatorInterface {
public:
WithdrawalFeesCrawler(const CoincenterInfo& coincenterInfo, Duration minDurationBetweenQueries,
CachedResultVault& cachedResultVault);
Expand All @@ -27,7 +28,7 @@ class WithdrawalFeesCrawler {

const WithdrawalInfoMaps& get(std::string_view exchangeName) { return _withdrawalFeesCache.get(exchangeName); }

void updateCacheFile() const;
void updateCacheFile() const override;

private:
class WithdrawalFeesFunc {
Expand Down
33 changes: 33 additions & 0 deletions src/api/common/src/exchange-permanent-curl-options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "exchange-permanent-curl-options.hpp"

#include "exchangeconfig.hpp"

namespace cct::api {

ExchangePermanentCurlOptions::ExchangePermanentCurlOptions(const ExchangeConfig &exchangeConfig)
: _exchangeConfig(exchangeConfig) {}

PermanentCurlOptions::Builder ExchangePermanentCurlOptions::builderBase(Api api) const {
PermanentCurlOptions::Builder builder;

builder.setAcceptedEncoding(_exchangeConfig.acceptEncoding())
.setRequestCallLogLevel(_exchangeConfig.requestsCallLogLevel())
.setRequestAnswerLogLevel(_exchangeConfig.requestsAnswerLogLevel())
.setTimeout(_exchangeConfig.httpConfig().timeout());

switch (api) {
case Api::Private:
builder.setMinDurationBetweenQueries(_exchangeConfig.privateAPIRate());
break;
case Api::Public:
builder.setMinDurationBetweenQueries(_exchangeConfig.publicAPIRate())
.setTooManyErrorsPolicy(PermanentCurlOptions::TooManyErrorsPolicy::kReturnEmptyResponse);
break;
default:
break;
}

return builder;
}

} // namespace cct::api
4 changes: 4 additions & 0 deletions src/api/common/src/exchangeprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "deposit.hpp"
#include "depositsconstraints.hpp"
#include "durationstring.hpp"
#include "exchange-permanent-curl-options.hpp"
#include "exchangeconfig.hpp"
#include "exchangename.hpp"
#include "exchangeprivateapitypes.hpp"
Expand Down Expand Up @@ -638,4 +639,7 @@ SentWithdrawInfo ExchangePrivate::isWithdrawSuccessfullySent(const InitiatedWith
return {currencyCode};
}

PermanentCurlOptions::Builder ExchangePrivate::permanentCurlOptionsBuilder() const {
return ExchangePermanentCurlOptions(exchangeConfig()).builderBase(ExchangePermanentCurlOptions::Api::Private);
}
} // namespace cct::api
5 changes: 5 additions & 0 deletions src/api/common/src/exchangepublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "commonapi.hpp"
#include "currencycode.hpp"
#include "currencycodeset.hpp"
#include "exchange-permanent-curl-options.hpp"
#include "exchangeconfig.hpp"
#include "exchangepublicapitypes.hpp"
#include "fiatconverter.hpp"
Expand Down Expand Up @@ -527,4 +528,8 @@ AbstractMarketDataSerializer &ExchangePublic::getMarketDataSerializer() {
return *_marketDataSerializerPtr;
}

PermanentCurlOptions::Builder ExchangePublic::permanentCurlOptionsBuilder() const {
return ExchangePermanentCurlOptions(exchangeConfig()).builderBase(ExchangePermanentCurlOptions::Api::Public);
}

} // namespace cct::api
5 changes: 3 additions & 2 deletions src/api/exchanges/include/binancepublicapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class BinancePublic : public ExchangePublic {
MonetaryAmount computePriceForNotional(Market mk, int avgPriceMins);

struct CommonInfo {
CommonInfo(const CoincenterInfo& coincenterInfo, const ExchangeConfig& exchangeConfig, settings::RunMode runMode);
CommonInfo(CurlHandle& curlHandle, const ExchangeConfig& exchangeConfig);

const ExchangeConfig& _exchangeConfig;
CurlHandle _curlHandle;
CurlHandle& _curlHandle;
};

struct ExchangeInfoFunc {
Expand Down Expand Up @@ -124,6 +124,7 @@ class BinancePublic : public ExchangePublic {
CommonInfo& _commonInfo;
};

CurlHandle _curlHandle;
CommonInfo _commonInfo;
CachedResult<ExchangeInfoFunc> _exchangeConfigCache;
CachedResult<MarketsFunc> _marketsCache;
Expand Down
3 changes: 1 addition & 2 deletions src/api/exchanges/src/binanceprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ json::container PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpR

BinancePrivate::BinancePrivate(const CoincenterInfo& coincenterInfo, BinancePublic& binancePublic, const APIKey& apiKey)
: ExchangePrivate(coincenterInfo, binancePublic, apiKey),
_curlHandle(BinancePublic::kURLBases, coincenterInfo.metricGatewayPtr(),
exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPrivate).build(),
_curlHandle(BinancePublic::kURLBases, coincenterInfo.metricGatewayPtr(), permanentCurlOptionsBuilder().build(),
coincenterInfo.getRunMode()),
_tradableCurrenciesCache(
CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault), _curlHandle,
Expand Down
11 changes: 5 additions & 6 deletions src/api/exchanges/src/binancepublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ VolAndPriNbDecimals QueryVolAndPriNbDecimals(const ExchangeInfoDataByMarket& exc
BinancePublic::BinancePublic(const CoincenterInfo& coincenterInfo, FiatConverter& fiatConverter,
api::CommonAPI& commonAPI)
: ExchangePublic("binance", fiatConverter, commonAPI, coincenterInfo),
_commonInfo(coincenterInfo, exchangeConfig(), coincenterInfo.getRunMode()),
_curlHandle(kURLBases, coincenterInfo.metricGatewayPtr(), permanentCurlOptionsBuilder().build(),
coincenterInfo.getRunMode()),
_commonInfo(_curlHandle, exchangeConfig()),
_exchangeConfigCache(
CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault),
_commonInfo),
Expand Down Expand Up @@ -135,11 +137,8 @@ std::optional<MonetaryAmount> BinancePublic::queryWithdrawalFee(CurrencyCode cur
return withdrawFee;
}

BinancePublic::CommonInfo::CommonInfo(const CoincenterInfo& coincenterInfo, const ExchangeConfig& exchangeConfig,
settings::RunMode runMode)
: _exchangeConfig(exchangeConfig),
_curlHandle(kURLBases, coincenterInfo.metricGatewayPtr(),
_exchangeConfig.curlOptionsBuilderBase(ExchangeConfig::Api::kPublic).build(), runMode) {}
BinancePublic::CommonInfo::CommonInfo(CurlHandle& curlHandle, const ExchangeConfig& exchangeConfig)
: _exchangeConfig(exchangeConfig), _curlHandle(curlHandle) {}

MarketSet BinancePublic::MarketsFunc::operator()() {
BinancePublic::ExchangeInfoFunc::ExchangeInfoDataByMarket exchangeInfoData = _exchangeConfigCache.get();
Expand Down
Loading
Loading