Skip to content

Commit 984e199

Browse files
committed
FIX: Fix C++ client examples
1 parent 7d403ca commit 984e199

File tree

15 files changed

+69
-35
lines changed

15 files changed

+69
-35
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The client supports both streaming live and historical data through similar inte
1010

1111
## Usage
1212

13-
A simple program that fetches 10 minutes of historical trades for all ES symbols and prints it look like this:
13+
A simple program that fetches a days worth of historical trades for all ES symbols and prints it looks like this:
1414

1515
```cpp
1616
#include <databento/historical.hpp>
@@ -22,9 +22,9 @@ static constexpr auto kApiKey = "YOUR_API_KEY";
2222

2323
int main() {
2424
auto client = HistoricalBuilder{}.SetKey(kApiKey).Build();
25-
client.TimeseriesStream("GLBX.MDP3", "2022-06-10T14:30", "2022-05-10T14:40",
26-
{"ES"}, Schema::Trades, SType::Smart,
27-
SType::ProductId, {}, {}, [](const Record& record) {
25+
client.TimeseriesStream("GLBX.MDP3", "2022-06-10", "2022-06-11", {"ES"},
26+
Schema::Trades, SType::Smart, SType::ProductId, {},
27+
{}, [](const Record& record) {
2828
const auto& trade_msg = record.get<TradeMsg>();
2929
std::cout << trade_msg << '\n';
3030
return KeepGoing::Continue;

example/metadata.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ int main() {
6868
}
6969
std::cout << '\n';
7070

71+
const auto record_count = client.MetadataGetRecordCount(
72+
kGlbxMdp3, "2020-12-28", "2020-12-29", {"ESH1"}, databento::Schema::Mbo);
73+
std::cout << "Record count: " << record_count << "\n\n";
74+
7175
const auto live_unit_prices =
7276
client.MetadataListUnitPrices(kGlbxMdp3, databento::FeedMode::Live);
7377
std::cout << "Unit prices (live):\n";
@@ -92,10 +96,14 @@ int main() {
9296
<< "\n\n";
9397

9498
const std::size_t billable_size = client.MetadataGetBillableSize(
95-
kGlbxMdp3, "2020-12-28", "2020-12-29", {"ESH0"}, databento::Schema::Mbo,
99+
kGlbxMdp3, "2020-12-28", "2020-12-29", {"ESH1"}, databento::Schema::Mbo,
96100
databento::SType::Native, {});
97101
std::cout << "Billable size (uncompressed binary bytes): " << billable_size
98102
<< "\n\n";
99103

104+
const auto cost = client.MetadataGetCost(
105+
kGlbxMdp3, "2020-12-28", "2020-12-29", {"ESH1"}, databento::Schema::Mbo);
106+
std::cout << "Cost (in cents): " << cost << '\n';
107+
100108
return 0;
101109
}

example/readme.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ static constexpr auto kApiKey = "YOUR_API_KEY";
1010

1111
int main() {
1212
auto client = HistoricalBuilder{}.SetKey(kApiKey).Build();
13-
client.TimeseriesStream("GLBX.MDP3", "2022-06-10T14:30", "2022-05-10T14:40",
14-
{"ES"}, Schema::Trades, SType::Smart,
15-
SType::ProductId, {}, {}, [](const Record& record) {
13+
client.TimeseriesStream("GLBX.MDP3", "2022-06-10", "2022-06-11", {"ES"},
14+
Schema::Trades, SType::Smart, SType::ProductId, {},
15+
{}, [](const Record& record) {
1616
const auto& trade_msg = record.get<TradeMsg>();
1717
std::cout << trade_msg << '\n';
1818
return KeepGoing::Continue;

example/symbology_resolve.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
int main(int argc, char* argv[]) {
1111
if (argc < 6) {
1212
std::cerr << "USAGE: symbology-resolve <DATASET> <STYPE_IN> <STYPE_OUT> "
13-
"<DATE> <SYMBOLS...>\n";
13+
"<START_DATE> <END_DATE> <SYMBOLS...>\n";
1414
return EX_USAGE;
1515
}
1616
const auto stype_in = databento::FromString<databento::SType>(argv[2]);
1717
const auto stype_out = databento::FromString<databento::SType>(argv[3]);
1818

1919
std::vector<std::string> symbols;
20-
for (int i = 5; i < argc; ++i) {
20+
for (int i = 6; i < argc; ++i) {
2121
symbols.emplace_back(argv[i]);
2222
}
2323

2424
auto client = databento::HistoricalBuilder{}.SetKeyFromEnv().Build();
2525
const databento::SymbologyResolution resolution = client.SymbologyResolve(
26-
argv[1], argv[4], argv[4], symbols, stype_in, stype_out);
26+
argv[1], argv[4], argv[5], symbols, stype_in, stype_out);
2727
std::cout << resolution << '\n';
2828

2929
return 0;

include/databento/batch.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ struct BatchJob {
1313
std::string id;
1414
std::string user_id;
1515
std::string bill_id;
16-
std::string dataset;
16+
// Price in cents
1717
double cost;
18+
std::string dataset;
1819
std::vector<std::string> symbols;
1920
SType stype_in;
2021
SType stype_out;
@@ -29,17 +30,27 @@ struct BatchJob {
2930
bool split_symbols;
3031
Packaging packaging;
3132
Delivery delivery;
33+
// If the entire book is selected.
3234
bool is_full_book;
35+
// If the batch job is a tutorial example.
3336
bool is_example;
3437
std::size_t record_count;
38+
// Size in bytes.
3539
std::size_t billed_size;
40+
// Size in bytes.
3641
std::size_t actual_size;
42+
// Size in bytes.
3743
std::size_t package_size;
3844
JobState state;
3945
std::string ts_received;
46+
// Empty if it hasn't been queued.
4047
std::string ts_queued;
48+
// Empty if processing hasn't started.
4149
std::string ts_process_start;
50+
// Empty if it hasn't finished processing.
4251
std::string ts_process_done;
52+
// Empty if it hasn't finished processing. The expiration is set based on when
53+
// the job finishes processing, not when it was requested.
4354
std::string ts_expiration;
4455
};
4556

include/databento/detail/http_client.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class HttpClient {
1919
nlohmann::json PostJson(const std::string& path,
2020
const httplib::Params& params);
2121
void GetRawStream(const std::string& path, const httplib::Params& params,
22-
httplib::ContentReceiver callback);
22+
const httplib::ContentReceiver& callback);
2323

2424
private:
2525
static nlohmann::json CheckAndParseResponse(const std::string& path,

src/batch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ std::ostream& operator<<(std::ostream& stream, const BatchJob& batch_job) {
2222
.AddField("id", batch_job.id)
2323
.AddField("user_id", batch_job.user_id)
2424
.AddField("bill_id", batch_job.bill_id)
25-
.AddField("dataset", batch_job.dataset)
2625
.AddField("cost", batch_job.cost)
26+
.AddField("dataset", batch_job.dataset)
2727
.AddField("symbols",
2828
static_cast<std::ostringstream&>(symbol_helper.Finish()))
2929
.AddField("stype_in", batch_job.stype_in)

src/dbz_parser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ databento::Metadata DbzParser<Input>::ParseMetadata() {
9595
// Decompress variable-length metadata
9696
const auto compressed_size =
9797
static_cast<std::size_t>(metadata_buffer.cend() - metadata_it);
98-
const std::size_t buffer_size = compressed_size * 3; // 3x is arbitrary
98+
const std::size_t buffer_size = compressed_size * 10; // 10x is arbitrary
9999
std::vector<std::uint8_t> var_buffer(buffer_size);
100100
const std::size_t actual_size = ::ZSTD_decompress(
101101
var_buffer.data(), buffer_size, &*metadata_it, compressed_size);
102102
if (::ZSTD_isError(actual_size) == 1) {
103103
throw DbzResponseError{
104-
"Error decompressing zstd-compressed variable-length metadata"};
104+
std::string{
105+
"Error decompressing zstd-compressed variable-length metadata: "} +
106+
::ZSTD_getErrorName(actual_size)};
105107
}
106108
auto var_buffer_it = var_buffer.cbegin();
107109
const auto schema_definition_length = Consume<std::uint32_t>(var_buffer_it);

src/detail/http_client.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,29 @@ nlohmann::json HttpClient::PostJson(const std::string& path,
4646

4747
void HttpClient::GetRawStream(const std::string& path,
4848
const httplib::Params& params,
49-
httplib::ContentReceiver callback) {
49+
const httplib::ContentReceiver& callback) {
5050
const std::string full_path = httplib::append_query_params(path, params);
51+
std::string err_body{};
5152
int err_status{};
5253
httplib::Result res = client_.Get(
5354
full_path,
5455
[&err_status](const httplib::Response& resp) {
55-
// only continue if good response status
5656
if (HttpClient::IsErrorStatus(resp.status)) {
57-
// instead of throwing here, store the HTTP status and return false to
58-
// have the client close the connection
5957
err_status = resp.status;
60-
return false;
6158
}
6259
return true;
6360
},
64-
std::move(callback));
61+
[&callback, &err_body, &err_status](const char* data,
62+
std::size_t length) {
63+
// if an error response was received, read all content into err_status
64+
if (err_status > 0) {
65+
err_body.append(data, length);
66+
return true;
67+
}
68+
return callback(data, length);
69+
});
6570
if (err_status > 0) {
66-
throw HttpResponseError{path, err_status, ""};
71+
throw HttpResponseError{path, err_status, std::move(err_body)};
6772
}
6873
if (res.error() != httplib::Error::Success) {
6974
throw HttpRequestError{path, res.error()};

src/exceptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ std::string HttpResponseError::BuildMessage(const std::string& request_path,
1818
const std::string& response_body) {
1919
std::ostringstream err_msg;
2020
err_msg << "Received an error response from request to " << request_path
21-
<< " with status " << status_code << " and body \"" << response_body
22-
<< '"';
21+
<< " with status " << status_code << " and body '" << response_body
22+
<< '\'';
2323
return err_msg.str();
2424
}
2525

0 commit comments

Comments
 (0)