|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <nlohmann/json.hpp> |
| 4 | + |
| 5 | +#include <map> // multimap |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "databento/datetime.hpp" // UnixNanos |
| 10 | +#include "databento/enums.hpp" // FromString |
| 11 | +#include "databento/exceptions.hpp" // JsonResponseError |
| 12 | + |
| 13 | +namespace httplib { |
| 14 | +using Params = std::multimap<std::string, std::string>; |
| 15 | +} |
| 16 | + |
| 17 | +namespace databento { |
| 18 | +namespace detail { |
| 19 | +void SetIfNotEmpty(httplib::Params* params, const std::string& key, |
| 20 | + const std::string& value); |
| 21 | +void SetIfNotEmpty(httplib::Params* params, const std::string& key, |
| 22 | + const std::vector<databento::JobState>& states); |
| 23 | + |
| 24 | +template <typename T> |
| 25 | +void SetIfPositive(httplib::Params* params, const std::string& key, |
| 26 | + const T value) { |
| 27 | + if (value > 0) { |
| 28 | + params->emplace(key, std::to_string(value)); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +template <> |
| 33 | +inline void SetIfPositive<databento::UnixNanos>( |
| 34 | + httplib::Params* params, const std::string& key, |
| 35 | + const databento::UnixNanos value) { |
| 36 | + if (value.time_since_epoch().count()) { |
| 37 | + params->emplace(key, databento::ToString(value)); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const nlohmann::json& CheckedAt(const std::string& endpoint, |
| 42 | + const nlohmann::json& json, |
| 43 | + const std::string& key); |
| 44 | + |
| 45 | +template <typename T> |
| 46 | +T FromCheckedAtString(const std::string& endpoint, const nlohmann::json& json, |
| 47 | + const std::string& key) { |
| 48 | + const auto& val_json = CheckedAt(endpoint, json, key); |
| 49 | + if (!val_json.is_string()) { |
| 50 | + throw JsonResponseError::TypeMismatch(endpoint, key + " string", val_json); |
| 51 | + } |
| 52 | + return databento::FromString<T>(val_json); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename T> |
| 56 | +T FromCheckedAtStringOrNull(const std::string& endpoint, |
| 57 | + const nlohmann::json& json, const std::string& key, |
| 58 | + T null_value) { |
| 59 | + const auto& val_json = CheckedAt(endpoint, json, key); |
| 60 | + if (val_json.is_null()) { |
| 61 | + return null_value; |
| 62 | + } |
| 63 | + if (val_json.is_string()) { |
| 64 | + return databento::FromString<T>(val_json); |
| 65 | + } |
| 66 | + throw JsonResponseError::TypeMismatch(endpoint, key + " null or string", |
| 67 | + val_json); |
| 68 | +} |
| 69 | + |
| 70 | +template <typename T> |
| 71 | +T ParseAt(const std::string& endpoint, const nlohmann::json& json, |
| 72 | + const std::string& key); |
| 73 | +template <> |
| 74 | +bool ParseAt(const std::string& endpoint, const nlohmann::json& json, |
| 75 | + const std::string& key); |
| 76 | +template <> |
| 77 | +std::string ParseAt(const std::string& endpoint, const nlohmann::json& json, |
| 78 | + const std::string& key); |
| 79 | +template <> |
| 80 | +std::size_t ParseAt(const std::string& endpoint, const nlohmann::json& json, |
| 81 | + const std::string& key); |
| 82 | +template <> |
| 83 | +double ParseAt(const std::string& endpoint, const nlohmann::json& json, |
| 84 | + const std::string& key); |
| 85 | +template <> |
| 86 | +std::vector<std::string> ParseAt(const std::string& endpoint, |
| 87 | + const nlohmann::json& json, |
| 88 | + const std::string& key); |
| 89 | + |
| 90 | +} // namespace detail |
| 91 | +} // namespace databento |
0 commit comments