Skip to content

Commit

Permalink
Merge pull request #46 from krazkidd:36-refactor-static-api-class-as-…
Browse files Browse the repository at this point in the history
…namespace

Add `kdeck` namespace and refactor `Api` class
  • Loading branch information
mross-ua authored Jun 25, 2024
2 parents bd38ffd + 0951b52 commit 68efe11
Show file tree
Hide file tree
Showing 40 changed files with 1,413 additions and 1,329 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
build/**
CMakeUserPresets.json
CMakeUserPresets.json
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
}
],
"version": 4
}
}
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"files.associations": {
"*.hpp.in": "cpp"
},
}
}
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@
]
}
]
}
}
99 changes: 51 additions & 48 deletions include/api/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,73 @@
#include "config.hpp"
#include "api/types.hpp"

template <typename TResponse>
using ApiResult = std::variant<TResponse, ErrorResponse>;

class Api
namespace kdeck
{
public:
// auth
static void Login(std::string_view email, std::string_view password);
static void Logout();
template <typename TResponse>
using ApiResult = std::variant<TResponse, ErrorResponse>;

// exchange
static ExchangeAnnouncementsResponse GetExchangeAnnouncements();
static ExchangeScheduleResponse GetExchangeSchedule();
static ExchangeStatusResponse GetExchangeStatus();
class Api
{
public:
// auth
void Login(std::string_view email, std::string_view password);
void Logout();

// portfolio
static double GetBalance();
static PortfolioPositionsResponse GetPositions();
// exchange
ExchangeAnnouncementsResponse GetExchangeAnnouncements();
ExchangeScheduleResponse GetExchangeSchedule();
ExchangeStatusResponse GetExchangeStatus();

// helpers
static bool IsLoggedIn();
// portfolio
double GetBalance();
PortfolioPositionsResponse GetPositions();

static std::vector<PortfolioPositionsResponse::EventPosition> GetEventPositions();
static std::vector<PortfolioPositionsResponse::MarketPosition> GetMarketPositions(std::string_view eventTicker = "");
// helpers
bool IsLoggedIn();

private:
// auth
static inline LoginResponse login;
std::vector<PortfolioPositionsResponse::EventPosition> GetEventPositions();
std::vector<PortfolioPositionsResponse::MarketPosition> GetMarketPositions(std::string_view eventTicker = "");

// exchange
static inline ExchangeAnnouncementsResponse announcements;
static inline ExchangeScheduleResponse schedule;
static inline ExchangeStatusResponse status;
private:
// auth
LoginResponse login;

// portfolio
static inline PortfolioBalanceResponse balance;
static inline PortfolioPositionsResponse positions;
// exchange
ExchangeAnnouncementsResponse announcements;
ExchangeScheduleResponse schedule;
ExchangeStatusResponse status;

static inline std::map<std::string, PortfolioPositionsResponse::EventPosition> eventsMap;
static inline std::map<std::string, std::map<std::string, PortfolioPositionsResponse::MarketPosition>> marketsMap;
// portfolio
PortfolioBalanceResponse balance;
PortfolioPositionsResponse positions;

// core
template <typename TResponse>
static ApiResult<TResponse> HandleResponse(int responseCode, const std::stringstream &responseBody);
std::map<std::string, PortfolioPositionsResponse::EventPosition> eventsMap;
std::map<std::string, std::map<std::string, PortfolioPositionsResponse::MarketPosition>> marketsMap;

template <typename TResponse>
static ApiResult<TResponse> MakeRequest(std::string_view endpoint, bool doPostMethod = false);
// core
template <typename TResponse>
ApiResult<TResponse> HandleResponse(int responseCode, const std::stringstream &responseBody);

template <typename TResponse>
static ApiResult<TResponse> MakeRequest(std::string_view endpoint, const boost::json::value &json, bool doPostMethod = false);
template <typename TResponse>
ApiResult<TResponse> MakeRequest(std::string_view endpoint, bool doPostMethod = false);

template <typename TResponse>
static ApiResult<TResponse> GetRequest(std::string_view endpoint);
template <typename TResponse>
ApiResult<TResponse> MakeRequest(std::string_view endpoint, const boost::json::value &json, bool doPostMethod = false);

template <typename TResponse, typename TRequest>
static ApiResult<TResponse> GetRequest(std::string_view endpoint, TRequest&& req);
template <typename TResponse>
ApiResult<TResponse> GetRequest(std::string_view endpoint);

template <typename TResponse>
static ApiResult<TResponse> PostRequest(std::string_view endpoint);
template <typename TResponse, typename TRequest>
ApiResult<TResponse> GetRequest(std::string_view endpoint, TRequest&& req);

template <typename TResponse>
ApiResult<TResponse> PostRequest(std::string_view endpoint);

template <typename TResponse, typename TRequest>
static ApiResult<TResponse> PostRequest(std::string_view endpoint, TRequest&& req);
};
template <typename TResponse, typename TRequest>
ApiResult<TResponse> PostRequest(std::string_view endpoint, TRequest&& req);
};
}

#include "api/Api.hxx"

#endif
#endif
Loading

0 comments on commit 68efe11

Please sign in to comment.