31 #ifndef LEDGER_CORE_LEDGERAPIPARSER_HPP 32 #define LEDGER_CORE_LEDGERAPIPARSER_HPP 35 #include <rapidjson/reader.h> 36 #include <net/HttpClient.hpp> 37 #include <collections/collections.hpp> 41 template <
typename ResultType,
typename Parser>
47 _result = std::make_shared<ResultType>();
48 _parser.init(_result.get());
52 _statusCode(cpy._statusCode),
53 _statusText(cpy._statusText),
56 _lastKey(cpy._lastKey),
58 _parser.init(_result.get());
62 return delegate([&] () {
68 return delegate([&] () {
74 return delegate([&] () {
79 bool Uint(
unsigned i) {
80 return delegate([&] () {
85 bool Int64(int64_t i) {
86 return delegate([&] () {
91 bool Uint64(uint64_t i) {
92 return delegate([&] () {
97 bool Double(
double d) {
98 return delegate([&] () {
103 bool RawNumber(
const rapidjson::Reader::Ch* str, rapidjson::SizeType length,
bool copy) {
104 return delegate([&] () {
105 _parser.RawNumber(str, length, copy);
109 bool String(
const rapidjson::Reader::Ch* str, rapidjson::SizeType length,
bool copy) {
110 if (_depth == 1 && isFailure() && _lastKey ==
"error") {
111 _error = std::string(str, length);
113 return delegate([&] () {
114 _parser.String(str, length, copy);
120 return delegate([&] () {
121 _parser.StartObject();
125 bool Key(
const rapidjson::Reader::Ch* str, rapidjson::SizeType length,
bool copy) {
126 _lastKey = std::string(str, length);
128 _parser.Key(str, length, copy);
130 return continueParsing();
133 bool EndObject(rapidjson::SizeType memberCount) {
136 _parser.EndObject(memberCount);
138 return continueParsing();
144 _parser.StartArray();
146 return continueParsing();
149 bool EndArray(rapidjson::SizeType elementCount) {
152 _parser.EndArray(elementCount);
154 return continueParsing();
166 void attach(
const std::shared_ptr<api::HttpUrlConnection>& connection) {
167 _statusCode = (uint32_t) connection->getStatusCode();
168 _statusText = connection->getStatusText();
171 void attach(
const std::string& statusText, uint32_t statusCode) {
172 _statusCode = statusCode;
173 _statusText = statusText;
176 inline bool isFailure()
const {
177 return _statusCode < 200 || _statusCode >= 400;
180 inline bool continueParsing()
const {
181 return _exception.isEmpty();
185 bool delegate(std::function<
void ()>&& fn) {
192 return continueParsing();
196 std::shared_ptr<ResultType> _result;
198 std::string _statusText;
199 uint32_t _statusCode;
201 std::string _lastKey;
207 #endif //LEDGER_CORE_LEDGERAPIPARSER_HPP
Definition: Either.hpp:43
Definition: Option.hpp:49
Definition: Account.cpp:8
Definition: LedgerApiParser.hpp:42