Skip to content

Commit

Permalink
Cleanup. Make API code more consistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
krazkidd committed Jul 22, 2024
1 parent 725fb45 commit c8c9dd7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 50 deletions.
6 changes: 3 additions & 3 deletions include/api/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace kdeck
std::shared_ptr<CancelOrderResponse> CancelOrder(std::string_view orderId);
std::shared_ptr<AmendOrderResponse> AmendOrder(std::string_view orderId);
std::shared_ptr<OrderResponse> DecreaseOrder(std::string_view orderId);
std::shared_ptr<PortfolioPositionsResponse> GetPositions();
std::shared_ptr<PositionsResponse> GetPositions();
std::shared_ptr<PortfolioSettlementsResponse> GetPortfolioSettlements();

// helpers
Expand All @@ -77,8 +77,8 @@ namespace kdeck
std::shared_ptr<MarketsResponse> markets;

// portfolio
std::shared_ptr<PortfolioBalanceResponse> balance;
std::shared_ptr<PortfolioPositionsResponse> positions;
std::shared_ptr<BalanceResponse> balance;
std::shared_ptr<PositionsResponse> positions;

std::map<std::string, std::shared_ptr<EventPosition>> eventsMap;
std::map<std::string, std::map<std::string, std::shared_ptr<MarketPosition>>> marketsMap;
Expand Down
16 changes: 8 additions & 8 deletions include/api/_Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace kdeck
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("POST", "{basePath}/login", Login, PATH(String, basePath), BODY_DTO(Object<LoginRequest>, loginRequest))
API_CALL("POST", "{basePath}/login", Login, PATH(String, basePath), BODY_DTO(Object<LoginRequest>, req))

API_CALL_HEADERS(Logout)
{
Expand Down Expand Up @@ -64,28 +64,28 @@ namespace kdeck
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("GET", "{basePath}/events", GetEvents, PATH(String, basePath), BODY_DTO(Object<EventsRequest>, eventsRequest))
API_CALL("GET", "{basePath}/events", GetEvents, PATH(String, basePath), BODY_DTO(Object<EventsRequest>, req))

API_CALL_HEADERS(GetEvent)
{
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("GET", "{basePath}/events/{event_ticker}", GetEvent, PATH(String, basePath), PATH(String, event_ticker), BODY_DTO(Object<EventRequest>, eventRequest))
API_CALL("GET", "{basePath}/events/{event_ticker}", GetEvent, PATH(String, basePath), PATH(String, event_ticker), BODY_DTO(Object<EventRequest>, req))

API_CALL_HEADERS(GetMarkets)
{
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("GET", "{basePath}/markets", GetMarkets, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), BODY_DTO(Object<MarketsRequest>, marketsRequest))
API_CALL("GET", "{basePath}/markets", GetMarkets, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), BODY_DTO(Object<MarketsRequest>, req))

API_CALL_HEADERS(GetTrades)
{
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("GET", "{basePath}/markets/trades", GetTrades, PATH(String, basePath), BODY_DTO(Object<TradesRequest>, tradesRequest))
API_CALL("GET", "{basePath}/markets/trades", GetTrades, PATH(String, basePath), BODY_DTO(Object<TradesRequest>, req))

API_CALL_HEADERS(GetMarket)
{
Expand All @@ -98,7 +98,7 @@ namespace kdeck
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("GET", "{basePath}/markets/{ticker}/orderbook", GetMarketOrderbook, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), QUERY(String, ticker), BODY_DTO(Object<MarketOrderbookRequest>, marketOrderbookRequest))
API_CALL("GET", "{basePath}/markets/{ticker}/orderbook", GetMarketOrderbook, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), QUERY(String, ticker), BODY_DTO(Object<MarketOrderbookRequest>, req))

API_CALL_HEADERS(GetSeries)
{
Expand All @@ -111,7 +111,7 @@ namespace kdeck
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("GET", "{basePath}/series/{series_ticker}/markets/{ticker}/candlesticks", GetMarketCandlesticks, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), PATH(String, series_ticker), PATH(String, ticker), BODY_DTO(Object<MarketCandlesticksRequest>, marketCandlesticksRequest))
API_CALL("GET", "{basePath}/series/{series_ticker}/markets/{ticker}/candlesticks", GetMarketCandlesticks, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), PATH(String, series_ticker), PATH(String, ticker), BODY_DTO(Object<MarketCandlesticksRequest>, req))

// portfolio ////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -176,7 +176,7 @@ namespace kdeck
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
}
API_CALL("GET", "{basePath}/portfolio/positions", GetPositions, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), BODY_DTO(Object<PortfolioPositionsRequest>, portfolioPositionsRequest))
API_CALL("GET", "{basePath}/portfolio/positions", GetPositions, PATH(String, basePath), AUTHORIZATION(String, authString, "Bearer"), BODY_DTO(Object<PositionsRequest>, req))

API_CALL_HEADERS(GetPortfolioSettlements)
{
Expand Down
19 changes: 10 additions & 9 deletions include/api/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,11 @@ namespace kdeck
// portfolio ////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

class PortfolioBalanceResponse
class BalanceResponse
: public oatpp::DTO
{
DTO_INIT(PortfolioBalanceResponse, DTO /* extends */)

DTO_INIT(BalanceResponse, DTO /* extends */)

DTO_FIELD(Int64, balance);
//TODO how to make optional?
Expand Down Expand Up @@ -714,16 +715,16 @@ namespace kdeck

};

struct PortfolioPositionsRequest
struct PositionsRequest
: public oatpp::DTO
{

DTO_INIT(PortfolioPositionsRequest, DTO /* extends */)
DTO_INIT(PositionsRequest, DTO /* extends */)

DTO_FIELD(String, cursor);
DTO_FIELD(Int32, limit) = 100;
DTO_FIELD(String, count_filter) = "position,total_traded,resting_order_count";
DTO_FIELD(String, settlement_status) = "unsettled";
DTO_FIELD(Int32, limit);
DTO_FIELD(String, count_filter);
DTO_FIELD(String, settlement_status);
DTO_FIELD(String, ticker);
DTO_FIELD(String, event_ticker);

Expand Down Expand Up @@ -760,11 +761,11 @@ namespace kdeck

};

struct PortfolioPositionsResponse
struct PositionsResponse
: public oatpp::DTO
{

DTO_INIT(PortfolioPositionsResponse, DTO /* extends */)
DTO_INIT(PositionsResponse, DTO /* extends */)

DTO_FIELD(String, cursor);
DTO_FIELD(List<Object<EventPosition>>, event_positions);
Expand Down
5 changes: 1 addition & 4 deletions src/api/Api.auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
#include <stdexcept>

#include "api/Api.hpp"

Check failure on line 5 in src/api/Api.auth.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

src/api/Api.auth.cpp:5:10 [clang-diagnostic-error]

'api/Api.hpp' file not found
#include "api/types.hpp"

namespace kdeck
{
class VoidResponse;
class ErrorResponse;

class LoginResponse;

void Api::Login(std::string_view email, std::string_view password)
{
Expand Down
6 changes: 1 addition & 5 deletions src/api/Api.exchange.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#include <stdexcept>

Check notice on line 1 in src/api/Api.exchange.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on src/api/Api.exchange.cpp

File src/api/Api.exchange.cpp does not conform to LLVM style guidelines. (lines 6, 7, 9, 10, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 27, 28, 29, 31, 33, 34, 35, 36, 37, 38, 39, 40, 42, 43, 45, 46, 47, 49, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62)

#include "api/Api.hpp"

Check failure on line 3 in src/api/Api.exchange.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

src/api/Api.exchange.cpp:3:10 [clang-diagnostic-error]

'api/Api.hpp' file not found
#include "api/types.hpp"

namespace kdeck
{
class ErrorResponse;

class ExchangeAnnouncementsResponse;
class ExchangeScheduleResponse;
class ExchangeStatusResponse;

std::shared_ptr<ExchangeAnnouncementsResponse> Api::GetExchangeAnnouncements()
{
Expand Down
10 changes: 1 addition & 9 deletions src/api/Api.market.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#include <stdexcept>

Check notice on line 1 in src/api/Api.market.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on src/api/Api.market.cpp

File src/api/Api.market.cpp does not conform to LLVM style guidelines. (lines 6, 7, 9, 10, 11, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 29, 30, 31, 33, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 47, 48, 50, 51, 52, 54, 56, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 70, 71, 72, 74, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 88, 89, 91, 92, 93, 95, 97, 98, 99, 100, 101, 102, 103, 104, 105, 107, 108, 110, 111, 112, 114, 116, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 131, 132, 133, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 147, 148, 150, 151, 152, 154, 156, 158, 159, 160, 161, 162, 163, 164, 165, 166, 168, 170)

#include "api/Api.hpp"

Check failure on line 3 in src/api/Api.market.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

src/api/Api.market.cpp:3:10 [clang-diagnostic-error]

'api/Api.hpp' file not found
#include "api/types.hpp"

namespace kdeck
{
class ErrorResponse;

class EventsResponse;
class EventResponse;
class MarketsResponse;
class TradesResponse;
class MarketOrderbookResponse;
class SeriesResponse;
class MarketCandlesticksResponse;

std::shared_ptr<EventsResponse> Api::GetEvents()
{
Expand Down
21 changes: 9 additions & 12 deletions src/api/Api.portfolio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
#include <string_view>

#include "api/Api.hpp"

Check failure on line 5 in src/api/Api.portfolio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

src/api/Api.portfolio.cpp:5:10 [clang-diagnostic-error]

'api/Api.hpp' file not found
#include "api/types.hpp"

namespace kdeck
{
class ErrorResponse;

class PortfolioBalanceResponse;
class PortfolioPositionsResponse;

double Api::GetBalance()
{
OATPP_LOGD("Api", "GetBalance");

ApiResult<PortfolioBalanceResponse> res = HandleResponse<PortfolioBalanceResponse>(_api->GetBalance(basePath, login->token));
ApiResult<BalanceResponse> res = HandleResponse<BalanceResponse>(_api->GetBalance(basePath, login->token));

if (std::holds_alternative<std::shared_ptr<PortfolioBalanceResponse>>(res))
if (std::holds_alternative<std::shared_ptr<BalanceResponse>>(res))
{
balance = std::get<std::shared_ptr<PortfolioBalanceResponse>>(res);
balance = std::get<std::shared_ptr<BalanceResponse>>(res);
}
else
{
Expand Down Expand Up @@ -151,17 +148,17 @@ namespace kdeck
//TODO return order;
}

std::shared_ptr<PortfolioPositionsResponse> Api::GetPositions()
std::shared_ptr<PositionsResponse> Api::GetPositions()
{
OATPP_LOGD("Api", "GetPositions");

auto req = PortfolioPositionsRequest::createShared();
auto req = PositionsRequest::createShared();

ApiResult<PortfolioPositionsResponse> res = HandleResponse<PortfolioPositionsResponse>(_api->GetPositions(basePath, login->token, req));
ApiResult<PositionsResponse> res = HandleResponse<PositionsResponse>(_api->GetPositions(basePath, login->token, req));

if (std::holds_alternative<std::shared_ptr<PortfolioPositionsResponse>>(res))
if (std::holds_alternative<std::shared_ptr<PositionsResponse>>(res))
{
positions = std::get<std::shared_ptr<PortfolioPositionsResponse>>(res);
positions = std::get<std::shared_ptr<PositionsResponse>>(res);

eventsMap.clear();
marketsMap.clear();
Expand Down

0 comments on commit c8c9dd7

Please sign in to comment.