Skip to content

Commit 24f185b

Browse files
MapleMaple
authored andcommitted
code condense
cleanup optimization achitecture refactor
1 parent 13ad9bf commit 24f185b

File tree

5 files changed

+411
-439
lines changed

5 files changed

+411
-439
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ _C++ Bitfinex REST API client_
66

77
### Synopsis
88

9-
This header-only library contains class for interfacing Bitfinex REST API v1
9+
This header-only library contains class for interfacing Bitfinex REST API v1.
1010

1111
### Installation
1212

@@ -15,7 +15,27 @@ add `#include "BitfinexAPI.hpp"` to your `.cpp` file.
1515

1616
### Usage
1717

18-
See self-explanatory `example.cpp` for general usage.
18+
// Create API client for both authenticated and unauthenticated requests
19+
BfxAPI::BitfinexAPI bfxAPI("accessKey", "secretKey");
20+
21+
// Create API client for just unauthenticated requests
22+
BfxAPI::BitfinexAPI bfxAPI();
23+
24+
// Fetch data
25+
bfxAPI.getTicker("btcusd");
26+
27+
// Check for errors
28+
if (!bfxAPI.hasApiError())
29+
// Get response in string
30+
cout << bfxAPI.strResponse() << endl;
31+
else
32+
{
33+
// Inspect errors
34+
cout << bfxAPI.getBfxApiStatusCode() << endl;
35+
cout << bfxAPI.getCurlStatusCode() << endl;
36+
}
37+
38+
See self-explanatory `example.cpp` for general usage and more requests.
1939

2040
### Dependencies
2141

example.cpp

Lines changed: 107 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
//////////////////////////////////////////////////////////////////////////////
1+
////////////////////////////////////////////////////////////////////////////////
22
//
33
// example.cpp
44
//
55
//
66
// Bitfinex REST API C++ client - examples
77
//
8-
//////////////////////////////////////////////////////////////////////////////
8+
////////////////////////////////////////////////////////////////////////////////
99

1010
// std
1111
#include <fstream>
@@ -21,126 +21,122 @@ using std::endl;
2121
using std::ifstream;
2222
using std::string;
2323

24-
using BfxAPI::BitfinexAPI;
25-
2624

2725
int main(int argc, char *argv[])
2826
{
29-
/////////////////////////////////////////////////////////////////////////
30-
// Examples
31-
// Note that default values are not mandatory. See BitfinexAPI.hpp
32-
// for details.
33-
/////////////////////////////////////////////////////////////////////////
34-
35-
/////////////////////////////////////////////////////////////////////////
36-
// GET REQUESTS - unauthenticated endpoints
37-
/////////////////////////////////////////////////////////////////////////
38-
39-
BitfinexAPI bfxAPI;
40-
string response;
41-
int errCode = 0;
42-
43-
// errCode = bfxAPI.getTicker(response, "btcusd");
44-
// errCode = bfxAPI.getStats(response, "btcusd");
45-
// errCode = bfxAPI.getFundingBook(response, "USD", 50, 50);
46-
// errCode = bfxAPI.getOrderBook(response, "btcusd", 50, 50, 1);
47-
// errCode = bfxAPI.getTrades(response, "btcusd", 0L, 50);
48-
// errCode = bfxAPI.getLends(response, "USD", 0L, 50);
49-
errCode = bfxAPI.getSymbols(response);
50-
// errCode = bfxAPI.getSymbolDetails(response);
51-
52-
cout << "Response: " << response << endl;
53-
cout << "Error code: " << errCode << endl;
54-
55-
/////////////////////////////////////////////////////////////////////////
56-
// POST REQUESTS - authenticated endpoints
57-
/////////////////////////////////////////////////////////////////////////
27+
// Create bfxAPI without API keys
28+
BfxAPI::BitfinexAPI bfxAPI;
29+
// Create bfxAPI with API keys
30+
// BfxAPI::BitfinexAPI bfxAPI("accessKey", "secretKey");
5831

32+
// Load API keys from file
5933
ifstream ifs("key-secret", ifstream::in);
6034
if (!ifs.is_open())
6135
{
6236
cout << "Can't open 'key-secret' file. " << endl;
6337
return 1;
6438
}
39+
getline(ifs, bfxAPI.setAccessKey());
40+
getline(ifs, bfxAPI.setSecretKey());
41+
ifs.close();
42+
43+
// Fetch API
44+
cout << "Request with error checking: " << endl;
45+
bfxAPI.getTicker("btcusd");
46+
if (!bfxAPI.hasApiError())
47+
cout << bfxAPI.strResponse() << endl;
6548
else
6649
{
67-
response.clear();
68-
string accessKey, secretKey;
69-
getline(ifs, accessKey);
70-
getline(ifs, secretKey);
71-
ifs.close();
72-
73-
bfxAPI.setKeys(accessKey, secretKey);
74-
75-
/// Account ///
76-
// errCode = bfxAPI.getAccountInfo(response);
77-
// errCode = bfxAPI.getAccountFees(response);
78-
// errCode = bfxAPI.getSummary(response);
79-
// errCode = bfxAPI.deposit(response, "bitcoin", "deposit", 1);
80-
// errCode = bfxAPI.getKeyPermissions(response);
81-
// errCode = bfxAPI.getMarginInfos(response);
82-
// errCode = bfxAPI.getBalances(response);
83-
// errCode = bfxAPI.transfer(response, 0.1, "BTC", "trading", "deposit");
84-
// errCode = bfxAPI.withdraw(response); // configure withdraw.conf file before use
85-
86-
/// Orders ///
87-
// errCode = bfxAPI.newOrder(response, "btcusd", 0.01, 983, "sell", "exchange limit", 0, 1,
88-
// 0, 0, 0);
89-
//
90-
// How to create vOrders object for newOrders() call
91-
// BitfinexAPI::vOrders orders =
92-
// {
93-
// {"btcusd", 0.1, 950, "sell", "exchange limit"},
94-
// {"btcusd", 0.1, 950, "sell", "exchange limit"},
95-
// {"btcusd", 0.1, 950, "sell", "exchange limit"}
96-
// };
97-
// errCode = bfxAPI.newOrders(response, orders);
98-
//
99-
// errCode = bfxAPI.cancelOrder(response, 13265453586LL);
100-
//
101-
// How to create ids object for cancelOrders() call
102-
// BitfinexAPI::vIds ids =
103-
// {
104-
// 12324589754LL,
105-
// 12356754322LL,
106-
// 12354996754LL
107-
// };
108-
// errCode = bfxAPI.cancelOrders(response, ids);
109-
//
110-
// errCode = bfxAPI.cancelAllOrders(response);
111-
// errCode = bfxAPI.replaceOrder(response, 1321548521LL, "btcusd", 0.05, 1212, "sell",
112-
// "exchange limit", 0, 0);
113-
// errCode = bfxAPI.getOrderStatus(response, 12113548453LL);
114-
// errCode = bfxAPI.getActiveOrders(response);
115-
// errCode = bfxAPI.getOrdersHistory(response, 10);
116-
117-
118-
/// Positions ///
119-
// errCode = bfxAPI.getActivePositions(response);
120-
// errCode = bfxAPI.claimPosition(response, 156321412LL, 150);
121-
122-
/// Historical data ///
123-
// errCode = bfxAPI.getBalanceHistory(response, "USD", 0L, 0L, 500, "all");
124-
// errCode = bfxAPI.getWithdrawalHistory(response, "BTC", "all", 0L , 0L, 500);
125-
// errCode = bfxAPI.getPastTrades(response, "btcusd", 0L, 0L, 500, 0);
126-
127-
/// Margin funding ///
128-
// errCode = bfxAPI.newOffer(response, "USD", 12000, 25.2, 30, "lend");
129-
// errCode = bfxAPI.cancelOffer(response, 12354245628LL);
130-
// errCode = bfxAPI.getOfferStatus(response, 12313541215LL);
131-
// errCode = bfxAPI.getActiveCredits(response);
132-
// errCode = bfxAPI.getOffers(response);
133-
// errCode = bfxAPI.getOffersHistory(response, 50);
134-
// errCode = bfxAPI.getPastFundingTrades(response, "BTC", 0, 50);
135-
// errCode = bfxAPI.getTakenFunds(response);
136-
// errCode = bfxAPI.getUnusedTakenFunds(response);
137-
// errCode = bfxAPI.getTotalTakenFunds(response);
138-
// errCode = bfxAPI.closeLoan(response, 1235845634LL);
139-
// errCode = bfxAPI.closePosition(response, 1235845634LL);
140-
141-
cout << "Response: " << response << endl;
142-
cout << "Error code: " << errCode << endl;
143-
144-
return 0;
50+
// see bfxERR enum in BitfinexAPI.hpp::BitfinexAPI
51+
cout << bfxAPI.getBfxApiStatusCode() << endl;
52+
// see https://curl.haxx.se/libcurl/c/libcurl-errors.html
53+
cout << bfxAPI.getCurlStatusCode() << endl;
14554
}
55+
56+
cout << "Request without error checking: " << endl;
57+
cout << bfxAPI.getSummary().strResponse() << endl;
58+
59+
////////////////////////////////////////////////////////////////////////////
60+
/// Available unauthenticated requests
61+
////////////////////////////////////////////////////////////////////////////
62+
63+
// bfxAPI.getTicker("btcusd");
64+
// bfxAPI.getStats("btcusd");
65+
// bfxAPI.getFundingBook("USD", 50, 50);
66+
// bfxAPI.getOrderBook("btcusd", 50, 50, 1);
67+
// bfxAPI.getTrades("btcusd", 0L, 50);
68+
// bfxAPI.getLends("USD", 0L, 50);
69+
// bfxAPI.getSymbols();
70+
// bfxAPI.getSymbolDetails();
71+
72+
////////////////////////////////////////////////////////////////////////////
73+
/// Available authenticated requests
74+
////////////////////////////////////////////////////////////////////////////
75+
76+
/// Account ///
77+
// bfxAPI.getAccountInfo();
78+
// bfxAPI.getAccountFees();
79+
// bfxAPI.getSummary();
80+
// bfxAPI.deposit("bitcoin", "deposit", 1);
81+
// bfxAPI.getKeyPermissions();
82+
// bfxAPI.getMarginInfos();
83+
// bfxAPI.getBalances();
84+
// bfxAPI.transfer(0.1, "BTC", "trading", "deposit");
85+
// bfxAPI.withdraw(); // configure withdraw.conf file before use
86+
87+
/// Orders ///
88+
// bfxAPI.newOrder("btcusd", 0.01, 983, "sell", "exchange limit", 0, 1,
89+
// 0, 0, 0);
90+
//
91+
// How to create vOrders object for newOrders() call
92+
// BitfinexAPI::vOrders orders =
93+
// {
94+
// {"btcusd", 0.1, 950, "sell", "exchange limit"},
95+
// {"btcusd", 0.1, 950, "sell", "exchange limit"},
96+
// {"btcusd", 0.1, 950, "sell", "exchange limit"}
97+
// };
98+
// bfxAPI.newOrders(orders);
99+
//
100+
// bfxAPI.cancelOrder(13265453586LL);
101+
//
102+
// How to create ids object for cancelOrders() call
103+
// BitfinexAPI::vIds ids =
104+
// {
105+
// 12324589754LL,
106+
// 12356754322LL,
107+
// 12354996754LL
108+
// };
109+
// bfxAPI.cancelOrders(ids);
110+
//
111+
// bfxAPI.cancelAllOrders();
112+
// bfxAPI.replaceOrder(1321548521LL, "btcusd", 0.05, 1212, "sell",
113+
// "exchange limit", 0, 0);
114+
// bfxAPI.getOrderStatus(12113548453LL);
115+
// bfxAPI.getActiveOrders();
116+
// bfxAPI.getOrdersHistory(10);
117+
118+
/// Positions ///
119+
// bfxAPI.getActivePositions();
120+
// bfxAPI.claimPosition(156321412LL, 150);
121+
122+
/// Historical data ///
123+
// bfxAPI.getBalanceHistory("USD", 0L, 0L, 500, "all");
124+
// bfxAPI.getWithdrawalHistory("BTC", "all", 0L , 0L, 500);
125+
// bfxAPI.getPastTrades("btcusd", 0L, 0L, 500, 0);
126+
127+
/// Margin funding ///
128+
// bfxAPI.newOffer("USD", 12000, 25.2, 30, "lend");
129+
// bfxAPI.cancelOffer(12354245628LL);
130+
// bfxAPI.getOfferStatus(12313541215LL);
131+
// bfxAPI.getActiveCredits();
132+
// bfxAPI.getOffers();
133+
// bfxAPI.getOffersHistory(50);
134+
// bfxAPI.getPastFundingTrades("BTC", 0, 50);
135+
// bfxAPI.getTakenFunds();
136+
// bfxAPI.getUnusedTakenFunds();
137+
// bfxAPI.getTotalTakenFunds();
138+
// bfxAPI.closeLoan(1235845634LL);
139+
// bfxAPI.closePosition(1235845634LL);
140+
141+
return 0;
146142
}

example2.cpp

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)