From 194b50840c8ccdaf743b4b2285a603540d4ac417 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 13 Feb 2020 09:20:26 -0800 Subject: [PATCH] release: v1.4.0-arduino includes changes at: https://github.com/ArkEcosystem/cpp-client/tree/0d72bb9e1b02ca87718ec1c003b6cb57e0055a58 --- CHANGELOG.md | 13 + CODEOWNERS | 2 +- README.md | 19 +- examples/ESP32/ESP32.ino | 2 +- extras/ARDUINO_IDE.sh | 16 +- library.json | 2 +- library.properties | 2 +- platformio.ini | 36 --- src/api/api.h | 9 + src/api/bridgechains/bridgechains.cpp | 40 ++++ src/api/bridgechains/bridgechains.h | 49 ++++ src/api/businesses/businesses.cpp | 49 ++++ src/api/businesses/businesses.h | 51 ++++ src/api/locks/locks.cpp | 47 ++++ src/api/locks/locks.h | 51 ++++ src/api/paths.cpp | 330 +++++++++++++++++++++----- src/api/paths.h | 90 +++++-- src/api/wallets/wallets.cpp | 9 + src/api/wallets/wallets.h | 2 + src/host/host.cpp | 2 +- src/http/http.cpp | 66 ++---- 21 files changed, 717 insertions(+), 170 deletions(-) mode change 100644 => 100755 extras/ARDUINO_IDE.sh delete mode 100644 platformio.ini create mode 100644 src/api/bridgechains/bridgechains.cpp create mode 100644 src/api/bridgechains/bridgechains.h create mode 100644 src/api/businesses/businesses.cpp create mode 100644 src/api/businesses/businesses.h create mode 100644 src/api/locks/locks.cpp create mode 100644 src/api/locks/locks.h diff --git a/CHANGELOG.md b/CHANGELOG.md index c3367de6..2e653749 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.4.0] - 2020-02-13 + +## [1.4.0-arduino] - 2020-02-13 + +### Added +- added Core 2.6 endpoints ([#159]) + +## [1.3.0] - 2019-10-09 + ## [1.3.0-arduino] - 2019-10-09 ### Added @@ -72,3 +81,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [#114]: https://github.com/ArkEcosystem/cpp-client/pull/114 [#140]: https://github.com/ArkEcosystem/cpp-client/pull/140 [1.3.0-arduino]: https://github.com/ArkEcosystem/cpp-client/compare/1.2.0-arduino...1.3.0-arduino +[1.3.0]: https://github.com/ArkEcosystem/cpp-client/compare/1.2.0...1.3.0 +[#159]: https://github.com/ArkEcosystem/cpp-client/pull/159 +[1.4.0]: https://github.com/ArkEcosystem/cpp-client/compare/1.3.0...1.4.0 +[1.4.0]: https://github.com/ArkEcosystem/cpp-client/compare/1.3.0-arduino...1.4.0-arduino diff --git a/CODEOWNERS b/CODEOWNERS index 3c0b9182..858e28fa 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1 +1 @@ -* @faustbrian @kristjank @supaiku0 +* @faustbrian @kristjank @supaiku0 @sleepdefic1t \ No newline at end of file diff --git a/README.md b/README.md index 7afdd012..a3fd940b 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,16 @@ > A simple C++ API client for the Ark Blockchain. -[![Build Status](https://badgen.now.sh/circleci/github/ArkEcosystem/cpp-client)](https://circleci.com/gh/ArkEcosystem/cpp-client) -[![Codecov](https://badgen.now.sh/codecov/c/github/arkecosystem/cpp-client)](https://codecov.io/gh/arkecosystem/cpp-client) -[![Latest Version](https://badgen.now.sh/github/release/ArkEcosystem/cpp-client)](https://github.com/ArkEcosystem/cpp-client/releases) -[![License: MIT](https://badgen.now.sh/badge/license/MIT/green)](https://opensource.org/licenses/MIT) +[![Test Actions](https://github.com/ArkEcosystem/cpp-client/workflows/Test/badge.svg)](https://github.com/ArkEcosystem/cpp-client/actions) +[![Coverage Actions](https://github.com/ArkEcosystem/cpp-client/workflows/Coverage/badge.svg)](https://github.com/ArkEcosystem/cpp-client/actions) +[![Latest Version](https://badgen.now.sh/github/release/ArkEcosystem/cpp-client?labelColor=black)](https://github.com/ArkEcosystem/cpp-client/releases) +[![License: MIT](https://badgen.now.sh/badge/license/MIT/green?labelColor=black)](https://opensource.org/licenses/MIT) + +> Lead Maintainer: [Simon Downey](https://github.com/sleepdefic1t) ## Documentation -You can find installation instructions and detailed instructions on how to use this package at the [dedicated documentation site](https://docs.ark.io/sdk/clients/cpp.html). +You can find installation instructions and detailed instructions on how to use this package at the [dedicated documentation site](https://docs.ark.io/sdk/clients/usage.html). ## Security @@ -21,11 +23,8 @@ If you discover a security vulnerability within this package, please send an e-m ## Credits -- [Simon Downey](https://github.com/sleepdeficit) -- [Chris Johnson](https://github.com/ciband) -- [supaiku0](https://github.com/supaiku0) -- [All Contributors](../../../../contributors) +This project exists thanks to all the people who [contribute](../../contributors). ## License -[MIT](LICENSE) © [ArkEcosystem](https://ark.io) +[MIT](LICENSE) © [ARK Ecosystem](https://ark.io) diff --git a/examples/ESP32/ESP32.ino b/examples/ESP32/ESP32.ino index 76bd4b65..96f4620a 100644 --- a/examples/ESP32/ESP32.ino +++ b/examples/ESP32/ESP32.ino @@ -337,7 +337,7 @@ void checkAPI() { // } // ] // } - const auto topWallets = connection.api.wallets.top("limit=1&page=1"); + const auto topWallets = connection.api.wallets.top("?limit=1&page=1"); Serial.print("\nTop Wallets: "); Serial.println(topWallets.c_str()); }; diff --git a/extras/ARDUINO_IDE.sh b/extras/ARDUINO_IDE.sh old mode 100644 new mode 100755 index 5d3b8579..b7f13230 --- a/extras/ARDUINO_IDE.sh +++ b/extras/ARDUINO_IDE.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash - # This file is part of Ark Cpp Client. # # (c) Ark Ecosystem @@ -38,7 +37,10 @@ SRC_API_DIR=${SRC_DIR}/api INCLUDE_BLOCKCHAIN_DIR=${INCLUDE_DIR}/api/blockchain INCLUDE_BLOCKS_DIR=${INCLUDE_DIR}/api/blocks +INCLUDE_BUSINESSES_DIR=${INCLUDE_DIR}/api/businesses +INCLUDE_BRIDGECHAINS_DIR=${INCLUDE_DIR}/api/bridgechains INCLUDE_DELEGATES_DIR=${INCLUDE_DIR}/api/delegates +INCLUDE_LOCKS_DIR=${INCLUDE_DIR}/api/locks INCLUDE_NODE_DIR=${INCLUDE_DIR}/api/node INCLUDE_PEERS_DIR=${INCLUDE_DIR}/api/peers INCLUDE_ROUNDS_DIR=${INCLUDE_DIR}/api/rounds @@ -48,7 +50,10 @@ INCLUDE_WALLETS_DIR=${INCLUDE_DIR}/api/wallets SRC_BLOCKCHAIN_DIR=${SRC_DIR}/api/blockchain SRC_BLOCKS_DIR=${SRC_DIR}/api/blocks +SRC_BUSINESSES_DIR=${SRC_DIR}/api/businesses +SRC_BRIDGECHAINS_DIR=${SRC_DIR}/api/bridgechains SRC_DELEGATES_DIR=${SRC_DIR}/api/delegates +SRC_LOCKS_DIR=${SRC_DIR}/api/locks SRC_NODE_DIR=${SRC_DIR}/api/node SRC_PEERS_DIR=${SRC_DIR}/api/peers SRC_ROUNDS_DIR=${SRC_DIR}/api/rounds @@ -112,7 +117,10 @@ if [[ -d ${INCLUDE_DIR} ]]; then mv ${INCLUDE_BLOCKCHAIN_DIR}/blockchain.hpp ${SRC_BLOCKCHAIN_DIR} mv ${INCLUDE_BLOCKS_DIR}/blocks.h ${SRC_BLOCKS_DIR} + mv ${INCLUDE_BUSINESSES_DIR}/businesses.h ${SRC_BUSINESSES_DIR} + mv ${INCLUDE_BRIDGECHAINS_DIR}/bridgechains.h ${SRC_BRIDGECHAINS_DIR} mv ${INCLUDE_DELEGATES_DIR}/delegates.h ${SRC_DELEGATES_DIR} + mv ${INCLUDE_LOCKS_DIR}/locks.h ${SRC_LOCKS_DIR} mv ${INCLUDE_NODE_DIR}/node.h ${SRC_NODE_DIR} mv ${INCLUDE_PEERS_DIR}/peers.h ${SRC_PEERS_DIR} mv ${INCLUDE_ROUNDS_DIR}/rounds.h ${SRC_ROUNDS_DIR} @@ -156,7 +164,10 @@ else mkdir ${INCLUDE_API_DIR} mkdir ${INCLUDE_BLOCKCHAIN_DIR} mkdir ${INCLUDE_BLOCKS_DIR} + mkdir ${INCLUDE_BUSINESSES_DIR} + mkdir ${INCLUDE_BRIDGECHAINS_DIR} mkdir ${INCLUDE_DELEGATES_DIR} + mkdir ${INCLUDE_LOCKS_DIR} mkdir ${INCLUDE_NODE_DIR} mkdir ${INCLUDE_PEERS_DIR} mkdir ${INCLUDE_ROUNDS_DIR} @@ -172,7 +183,10 @@ else mv ${SRC_BLOCKCHAIN_DIR}/blockchain.hpp ${INCLUDE_BLOCKCHAIN_DIR} mv ${SRC_BLOCKS_DIR}/blocks.h ${INCLUDE_BLOCKS_DIR} + mv ${SRC_BUSINESSES_DIR}/businesses.h ${INCLUDE_BUSINESSES_DIR} + mv ${SRC_BRIDGECHAINS_DIR}/bridgechains.h ${INCLUDE_BRIDGECHAINS_DIR} mv ${SRC_DELEGATES_DIR}/delegates.h ${INCLUDE_DELEGATES_DIR} + mv ${SRC_LOCKS_DIR}/locks.h ${INCLUDE_LOCKS_DIR} mv ${SRC_NODE_DIR}/node.h ${INCLUDE_NODE_DIR} mv ${SRC_PEERS_DIR}/peers.h ${INCLUDE_PEERS_DIR} mv ${SRC_ROUNDS_DIR}/rounds.h ${INCLUDE_ROUNDS_DIR} diff --git a/library.json b/library.json index d4a29ea7..9c2ef6e2 100644 --- a/library.json +++ b/library.json @@ -7,7 +7,7 @@ "type": "git", "url": "https://github.com/ArkEcosystem/Cpp-Client.git" }, - "version": "1.3.0-arduino", + "version": "1.4.0-arduino", "authors": [ { "name": "Ark Ecosystem", diff --git a/library.properties b/library.properties index e9cfa75e..6a6adcd3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Ark-Cpp-Client -version=1.3.0-arduino +version=1.4.0-arduino author=Ark Ecosystem maintainer=Ark Ecosystem sentence=A simple API client implementation in C++ for the ARK Blockchain. diff --git a/platformio.ini b/platformio.ini deleted file mode 100644 index 9a601184..00000000 --- a/platformio.ini +++ /dev/null @@ -1,36 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; http://docs.platformio.org/page/projectconf.html - -[platformio] -description = "A simple API client implementation in C++ for the ARK Blockchain." -src_dir = ./src -build_dir = build/.pioenvs -libdeps_dir = extern/.piolibdeps - -[common] -build_flags = -I./src/ -src_filter = +<*> -upload_speed = 921600 - -[env:esp8266] -platform = espressif8266 -board = huzzah -framework = arduino -build_flags = ${common.build_flags} -src_filter = ${common.src_filter} -upload_speed = ${common.upload_speed} - -[env:esp32] -platform = espressif32 -board = esp32dev -framework = arduino -build_flags = ${common.build_flags} -src_filter = ${common.src_filter} -upload_speed = ${common.upload_speed} diff --git a/src/api/api.h b/src/api/api.h index 6acddcf0..f33e43c1 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -14,7 +14,10 @@ #include "api/api.h" #include "api/blockchain/blockchain.hpp" #include "api/blocks/blocks.h" +#include "api/businesses/businesses.h" +#include "api/bridgechains/bridgechains.h" #include "api/delegates/delegates.h" +#include "api/locks/locks.h" #include "api/node/node.h" #include "api/peers/peers.h" #include "api/rounds/rounds.h" @@ -29,7 +32,10 @@ class Api : public api::Abstract { public: api::Blockchain blockchain; api::Blocks blocks; + api::Businesses businesses; + api::Bridgechains bridgechains; api::Delegates delegates; + api::Locks locks; api::Node node; api::Peers peers; api::Rounds rounds; @@ -40,7 +46,10 @@ class Api : public api::Abstract { Api() : Abstract(), blockchain(host_, *http_), blocks(host_, *http_), + businesses(host_, *http_), + bridgechains(host_, *http_), delegates(host_, *http_), + locks(host_, *http_), node(host_, *http_), peers(host_, *http_), rounds(host_, *http_), diff --git a/src/api/bridgechains/bridgechains.cpp b/src/api/bridgechains/bridgechains.cpp new file mode 100644 index 00000000..0f9bcea2 --- /dev/null +++ b/src/api/bridgechains/bridgechains.cpp @@ -0,0 +1,40 @@ +/** + * This file is part of Ark Cpp Client. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "api/bridgechains/bridgechains.h" + +namespace Ark { +namespace Client { +namespace api { + +std::string Bridgechains::get(const char* bridgechainId) { + return http_->get(paths::Bridgechains::get(this->host_, bridgechainId).c_str()); +} + +/**/ + +std::string Bridgechains::all(const char* const query) { + return http_->get(paths::Bridgechains::all(this->host_, query).c_str()); +} + +/**/ + +std::string Bridgechains::search( + const std::map &bodyParameters, + const char* const query) { + const auto searchPathPair = paths::Bridgechains::search(this->host_, + bodyParameters, + query); + return http_->post(searchPathPair.first.c_str(), + searchPathPair.second.c_str()); +} + +} // namespace api +} // namespace Client +} // namespace Ark diff --git a/src/api/bridgechains/bridgechains.h b/src/api/bridgechains/bridgechains.h new file mode 100644 index 00000000..72c6394c --- /dev/null +++ b/src/api/bridgechains/bridgechains.h @@ -0,0 +1,49 @@ +/** + * This file is part of Ark Cpp Client. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef BRIDGECHAINS_H +#define BRIDGECHAINS_H + +#include +#include + +#include "api/base.h" +#include "api/paths.h" + +namespace Ark { +namespace Client { +namespace api { // NOLINT + +class IBridgechains : public Base { + public: + virtual ~IBridgechains() {} + virtual std::string get(const char* const bridgechainId) = 0; + virtual std::string all(const char* const query) = 0; + virtual std::string search(const std::map& bodyParameters, const char* const query) = 0; + + protected: + IBridgechains(Host& host, IHTTP& http) : Base(host, http) {} +}; + +/**/ + +class Bridgechains : public IBridgechains { + public: + Bridgechains(Host& host, IHTTP& http) : IBridgechains(host, http) {} + + std::string get(const char* const bridgechainId) override; + std::string all(const char* const query) override; + std::string search(const std::map& bodyParameters, const char* const query) override; +}; + +} // namespace api +} // namespace Client +} // namespace Ark + +#endif diff --git a/src/api/businesses/businesses.cpp b/src/api/businesses/businesses.cpp new file mode 100644 index 00000000..fc4aa7fa --- /dev/null +++ b/src/api/businesses/businesses.cpp @@ -0,0 +1,49 @@ +/** + * This file is part of Ark Cpp Client. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "api/businesses/businesses.h" + +namespace Ark { +namespace Client { +namespace api { + +std::string Businesses::get(const char* businessId) { + return http_->get(paths::Businesses::get(this->host_, businessId).c_str()); +} + +/**/ + +std::string Businesses::all(const char* const query) { + return http_->get(paths::Businesses::all(this->host_, query).c_str()); +} + +/**/ + +std::string Businesses::bridgechains(const char* businessId, + const char* const query) { + return http_->get(paths::Businesses::bridgechains(this->host_, + businessId, + query).c_str()); +} + +/**/ + +std::string Businesses::search( + const std::map &bodyParameters, + const char* const query) { + const auto searchPathPair = paths::Businesses::search(this->host_, + bodyParameters, + query); + return http_->post(searchPathPair.first.c_str(), + searchPathPair.second.c_str()); +} + +} // namespace api +} // namespace Client +} // namespace Ark diff --git a/src/api/businesses/businesses.h b/src/api/businesses/businesses.h new file mode 100644 index 00000000..5dc3a011 --- /dev/null +++ b/src/api/businesses/businesses.h @@ -0,0 +1,51 @@ +/** + * This file is part of Ark Cpp Client. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef BUSINESSES_H +#define BUSINESSES_H + +#include +#include + +#include "api/base.h" +#include "api/paths.h" + +namespace Ark { +namespace Client { +namespace api { // NOLINT + +class IBusinesses : public Base { + public: + virtual ~IBusinesses() {} + virtual std::string get(const char* const businessId) = 0; + virtual std::string all(const char* const query) = 0; + virtual std::string bridgechains(const char* const businessId, const char* const query) = 0; + virtual std::string search(const std::map& bodyParameters, const char* const query) = 0; + + protected: + IBusinesses(Host& host, IHTTP& http) : Base(host, http) {} +}; + +/**/ + +class Businesses : public IBusinesses { + public: + Businesses(Host& host, IHTTP& http) : IBusinesses(host, http) {} + + std::string get(const char* const businessId) override; + std::string all(const char* const query) override; + std::string bridgechains(const char* const businessId, const char* const query) override; + std::string search(const std::map& bodyParameters, const char* const query) override; +}; + +} // namespace api +} // namespace Client +} // namespace Ark + +#endif diff --git a/src/api/locks/locks.cpp b/src/api/locks/locks.cpp new file mode 100644 index 00000000..88cfc964 --- /dev/null +++ b/src/api/locks/locks.cpp @@ -0,0 +1,47 @@ +/** + * This file is part of Ark Cpp Client. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "api/locks/locks.h" + +namespace Ark { +namespace Client { +namespace api { + +std::string Locks::get(const char* lockId) { + return http_->get(paths::Locks::get(this->host_, lockId).c_str()); +} + +/**/ + +std::string Locks::all(const char* const query) { + return http_->get(paths::Locks::all(this->host_, query).c_str()); +} + +/**/ + +std::string Locks::search( + const std::map &bodyParameters, + const char* const query) { + const auto searchPathPair = paths::Locks::search(this->host_, + bodyParameters, + query); + return http_->post(searchPathPair.first.c_str(), + searchPathPair.second.c_str()); +} + +/**/ + +std::string Locks::unlocked(std::string& jsonIds, const char* const query) { + const auto pathPair = paths::Locks::unlocked(this->host_, jsonIds, query); + return http_->post(pathPair.first.c_str(), pathPair.second.c_str()); +} + +} // namespace api +} // namespace Client +} // namespace Ark diff --git a/src/api/locks/locks.h b/src/api/locks/locks.h new file mode 100644 index 00000000..04ed4834 --- /dev/null +++ b/src/api/locks/locks.h @@ -0,0 +1,51 @@ +/** + * This file is part of Ark Cpp Client. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef LOCKS_H +#define LOCKS_H + +#include +#include + +#include "api/base.h" +#include "api/paths.h" + +namespace Ark { +namespace Client { +namespace api { // NOLINT + +class ILocks : public Base { + public: + virtual ~ILocks() {} + virtual std::string get(const char* const lockId) = 0; + virtual std::string all(const char* const query) = 0; + virtual std::string search(const std::map& bodyParameters, const char* const query) = 0; + virtual std::string unlocked(std::string& jsonIds, const char* const query) = 0; + + protected: + ILocks(Host& host, IHTTP& http) : Base(host, http) {} +}; + +/**/ + +class Locks : public ILocks { + public: + Locks(Host& host, IHTTP& http) : ILocks(host, http) {} + + std::string get(const char* const lockId) override; + std::string all(const char* const query) override; + std::string search(const std::map& bodyParameters, const char* const query) override; + std::string unlocked(std::string& jsonIds, const char* const query) override; +}; + +} // namespace api +} // namespace Client +} // namespace Ark + +#endif diff --git a/src/api/paths.cpp b/src/api/paths.cpp index 2303c356..f308927a 100644 --- a/src/api/paths.cpp +++ b/src/api/paths.cpp @@ -11,22 +11,23 @@ #include - -#include - namespace Ark { namespace Client { namespace api { namespace paths { +//////////////////////////////////////////////////////////////////////////////// + namespace { constexpr const uint8_t URL_MAX_LEN = 128U; } //namespace +//////////////////////////////////////////////////////////////////////////////// + /** * Blockchain **/ -const char* Blockchain::base() { return "/api/blockchain"; } +std::string Blockchain::base() { return "/api/blockchain"; } /**/ std::string Blockchain::get(Host& newHost) { @@ -37,12 +38,13 @@ std::string Blockchain::get(Host& newHost) { return url; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** * Blocks **/ -const char* Blocks::base() { return "/api/blocks"; } +std::string Blocks::base() { return "/api/blocks"; } /**/ std::string Blocks::get(Host& newHost, const char* blockId) { @@ -55,7 +57,7 @@ std::string Blocks::get(Host& newHost, const char* blockId) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Blocks::all(Host& newHost, const char* const query) { std::string url; @@ -66,7 +68,7 @@ std::string Blocks::all(Host& newHost, const char* const query) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Blocks::transactions(Host& newHost, const char* blockId) { std::string url; @@ -79,7 +81,7 @@ std::string Blocks::transactions(Host& newHost, const char* blockId) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::pair Blocks::search( Host& newHost, @@ -104,14 +106,140 @@ std::pair Blocks::search( return { url, parameterBuffer }; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** - * Delegates + * Businesses **/ -const char* Delegates::base() { return "/api/delegates"; } +std::string Businesses::base() { return "/api/businesses"; } /**/ +std::string Businesses::get(Host& newHost, const char* businessId) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Businesses::base(); + url += "/"; + url += businessId; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string Businesses::all(Host& newHost, const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Businesses::base(); + url += query; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string Businesses::bridgechains(Host& newHost, const char* businessId, const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Businesses::base(); + url += "/"; + url += businessId; + url += "/bridgechains"; + url += query; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::pair Businesses::search( + Host& newHost, + const std::map& bodyParameters, + const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Businesses::base(); + url += "/search"; + url += query; + + std::string parameterBuffer; + auto count = 0UL; + for (const auto& p : bodyParameters) { + ++count; + parameterBuffer += p.first + '=' + p.second; + if (bodyParameters.size() > 1 && count < bodyParameters.size()) { + parameterBuffer += '&'; + }; + }; + return { url, parameterBuffer }; +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +/** + * Bridgechains + **/ +std::string Bridgechains::base() { return "/api/bridgechains"; } + +//////////////////////////////////////////////////////////////////////////////// + +std::string Bridgechains::get(Host& newHost, const char* bridgechainId) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Bridgechains::base(); + url += "/"; + url += bridgechainId; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string Bridgechains::all(Host& newHost, const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Bridgechains::base(); + url += query; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::pair Bridgechains::search( + Host& newHost, + const std::map& bodyParameters, + const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Bridgechains::base(); + url += "/search"; + url += query; + + std::string parameterBuffer; + auto count = 0UL; + for (const auto& p : bodyParameters) { + ++count; + parameterBuffer += p.first + '=' + p.second; + if (bodyParameters.size() > 1 && count < bodyParameters.size()) { + parameterBuffer += '&'; + }; + }; + return { url, parameterBuffer }; +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +/** + * Delegates + **/ +std::string Delegates::base() { return "/api/delegates"; } + +//////////////////////////////////////////////////////////////////////////////// std::string Delegates::get(Host& newHost, const char* identifier) { std::string url; @@ -123,7 +251,7 @@ std::string Delegates::get(Host& newHost, const char* identifier) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Delegates::all( Host& newHost, const char* const query) { std::string url; @@ -134,7 +262,7 @@ std::string Delegates::all( Host& newHost, const char* const query) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Delegates::blocks(Host& newHost, const char* const identifier, @@ -150,7 +278,7 @@ std::string Delegates::blocks(Host& newHost, return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Delegates::voters(Host& newHost, const char* const identifier, @@ -166,14 +294,87 @@ std::string Delegates::voters(Host& newHost, return url; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +/** + * Locks + **/ +std::string Locks::base() { return "/api/locks"; } + +//////////////////////////////////////////////////////////////////////////////// + +std::string Locks::get(Host& newHost, const char* lockId) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Locks::base(); + url += "/"; + url += lockId; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string Locks::all(Host& newHost, const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Locks::base(); + url += query; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::pair Locks::search( + Host& newHost, + const std::map& bodyParameters, + const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Locks::base(); + url += "/search"; + url += query; + + std::string parameterBuffer; + auto count = 0UL; + for (const auto& p : bodyParameters) { + ++count; + parameterBuffer += p.first + '=' + p.second; + if (bodyParameters.size() > 1 && count < bodyParameters.size()) { + parameterBuffer += '&'; + }; + }; + return { url, parameterBuffer }; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::pair Locks::unlocked( + Host& newHost, + std::string& jsonIds, + const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Locks::base(); + url += "/unlocked"; + url += query; + + return { url.c_str(), jsonIds.c_str() }; +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** * Node **/ -const char* Node::base() { return "/api/node"; } +std::string Node::base() { return "/api/node"; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Node::configuration(Host& newHost) { std::string url; @@ -184,7 +385,7 @@ std::string Node::configuration(Host& newHost) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Node::crypto(Host& newHost) { std::string url; @@ -195,7 +396,7 @@ std::string Node::crypto(Host& newHost) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Node::fees(Host& newHost, const char* const query) { std::string url; @@ -207,7 +408,7 @@ std::string Node::fees(Host& newHost, const char* const query) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Node::status(Host& newHost) { std::string url; @@ -218,7 +419,7 @@ std::string Node::status(Host& newHost) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Node::syncing(Host& newHost) { std::string url; @@ -229,14 +430,15 @@ std::string Node::syncing(Host& newHost) { return url; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** * Peers **/ -const char* Peers::base() { return "/api/peers"; } +std::string Peers::base() { return "/api/peers"; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Peers::get(Host& newHost, const char* ip) { std::string url; @@ -248,7 +450,7 @@ std::string Peers::get(Host& newHost, const char* ip) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Peers::all(Host& newHost, const char* const query) { std::string url; @@ -259,14 +461,15 @@ std::string Peers::all(Host& newHost, const char* const query) { return url; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** * Rounds **/ -const char* Rounds::base() { return "/api/rounds"; } +std::string Rounds::base() { return "/api/rounds"; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Rounds::delegates(Host& newHost, const char* roundId) { std::string url; @@ -279,14 +482,15 @@ std::string Rounds::delegates(Host& newHost, const char* roundId) { return url; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** * Transactions **/ -const char* Transactions::base() { return "/api/transactions"; } +std::string Transactions::base() { return "/api/transactions"; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Transactions::getUnconfirmed(Host& newHost, const char* identifier) { @@ -299,7 +503,7 @@ std::string Transactions::getUnconfirmed(Host& newHost, return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Transactions::all(Host& newHost, const char* const query) { std::string url; @@ -310,7 +514,7 @@ std::string Transactions::all(Host& newHost, const char* const query) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Transactions::get(Host& newHost, const char* identifier) { std::string url; @@ -322,7 +526,7 @@ std::string Transactions::get(Host& newHost, const char* identifier) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Transactions::allUnconfirmed(Host& newHost, const char* const query) { @@ -335,7 +539,7 @@ std::string Transactions::allUnconfirmed(Host& newHost, return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Transactions::types(Host& newHost) { std::string url; @@ -346,7 +550,7 @@ std::string Transactions::types(Host& newHost) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Transactions::fees(Host& newHost) { std::string url; @@ -357,7 +561,7 @@ std::string Transactions::fees(Host& newHost) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::pair Transactions::search( Host& newHost, @@ -382,7 +586,7 @@ std::pair Transactions::search( return { url, parameterBuffer.c_str() }; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::pair Transactions::send( Host& newHost, @@ -395,14 +599,15 @@ std::pair Transactions::send( return { url.c_str(), jsonTransaction.c_str() }; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** * Votes **/ -const char* Votes::base() { return "/api/votes"; } +std::string Votes::base() { return "/api/votes"; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Votes::get(Host& newHost, const char* identifier) { std::string url; @@ -414,7 +619,7 @@ std::string Votes::get(Host& newHost, const char* identifier) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Votes::all(Host& newHost, const char* const query) { std::string url; @@ -425,14 +630,15 @@ std::string Votes::all(Host& newHost, const char* const query) { return url; } -/****/ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// /** * Wallets **/ -const char* Wallets::base() { return "/api/wallets"; } +std::string Wallets::base() { return "/api/wallets"; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Wallets::get(Host& newHost, const char* identifier) { std::string url; @@ -444,7 +650,7 @@ std::string Wallets::get(Host& newHost, const char* identifier) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Wallets::all(Host& newHost, const char* const query) { std::string url; @@ -455,7 +661,7 @@ std::string Wallets::all(Host& newHost, const char* const query) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Wallets::top(Host& newHost, const char* const query) { std::string url; @@ -467,7 +673,23 @@ std::string Wallets::top(Host& newHost, const char* const query) { return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// + +std::string Wallets::locks(Host& newHost, + const char* const identifier, + const char* const query) { + std::string url; + url.reserve(URL_MAX_LEN); + url += newHost.toString().c_str(); + url += Wallets::base(); + url += "/"; + url += identifier; + url += "/locks"; + url += query; + return url; +} + +//////////////////////////////////////////////////////////////////////////////// std::string Wallets::transactions(Host& newHost, const char* const identifier, @@ -483,7 +705,7 @@ std::string Wallets::transactions(Host& newHost, return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Wallets::transactionsSent(Host& newHost, const char* const identifier, @@ -499,7 +721,7 @@ std::string Wallets::transactionsSent(Host& newHost, return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Wallets::transactionsReceived(Host& newHost, const char* const identifier, @@ -515,7 +737,7 @@ std::string Wallets::transactionsReceived(Host& newHost, return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::string Wallets::votes(Host& newHost, const char* const identifier, @@ -531,7 +753,7 @@ std::string Wallets::votes(Host& newHost, return url; } -/**/ +//////////////////////////////////////////////////////////////////////////////// std::pair Wallets::search( Host& newHost, @@ -556,6 +778,8 @@ std::pair Wallets::search( return { url.c_str(), parameterBuffer.c_str() }; } +//////////////////////////////////////////////////////////////////////////////// + } // namespace paths } // namespace api } // namespace Client diff --git a/src/api/paths.h b/src/api/paths.h index 97bc407e..d3ec730c 100644 --- a/src/api/paths.h +++ b/src/api/paths.h @@ -21,18 +21,22 @@ namespace Client { namespace api { namespace paths { // NOLINT +//////////////////////////////////////////////////////////////////////////////// + static const char* DEFAULT_QUERY = "?page=1&limit=5"; static const char* DEFAULT_DAYS_QUERY = "?days=7"; +//////////////////////////////////////////////////////////////////////////////// + struct Blockchain { - static const char* base(); + static std::string base(); static std::string get(Host& newHost); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// -struct Blocks { - static const char* base(); +struct Blocks { // NOLINT + static std::string base(); static std::string get(Host& newHost, const char* const blockId); static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); static std::string transactions(Host& newHost, const char* const blockId); @@ -41,20 +45,57 @@ struct Blocks { const char* const query = DEFAULT_QUERY); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// + +struct Businesses { // NOLINT + static std::string base(); + static std::string get(Host& newHost, const char* const businessId); + static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); + static std::string bridgechains(Host& newHost, const char* const businessId, const char* const query = DEFAULT_QUERY); + static std::pair search(Host& newHost, + const std::map& bodyParameters, + const char* const query = DEFAULT_QUERY); +}; + +//////////////////////////////////////////////////////////////////////////////// + +struct Bridgechains { // NOLINT + static std::string base(); + static std::string get(Host& newHost, const char* const bridgechainId); + static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); + static std::pair search(Host& newHost, + const std::map& bodyParameters, + const char* const query = DEFAULT_QUERY); +}; + +//////////////////////////////////////////////////////////////////////////////// -struct Delegates { - static const char* base(); +struct Delegates { // NOLINT + static std::string base(); static std::string get(Host& newHost, const char* const identifier); static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); static std::string blocks(Host& newHost, const char* const identifier, const char* const query = DEFAULT_QUERY); static std::string voters(Host& newHost, const char* const identifier, const char* const query = DEFAULT_QUERY); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// + +struct Locks { // NOLINT + static std::string base(); + static std::string get(Host& newHost, const char* const lockId); + static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); + static std::pair search(Host& newHost, + const std::map& bodyParameters, + const char* const query = DEFAULT_QUERY); + static std::pair unlocked(Host& newHost, + std::string& jsonIds, + const char* const query = DEFAULT_QUERY); +}; + +//////////////////////////////////////////////////////////////////////////////// -struct Node { - static const char* base(); +struct Node { // NOLINT + static std::string base(); static std::string configuration(Host& newHost); static std::string crypto(Host& newHost); static std::string fees(Host& newHost, const char* const query = DEFAULT_DAYS_QUERY); @@ -62,25 +103,25 @@ struct Node { static std::string syncing(Host& newHost); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// struct Peers { - static const char* base(); + static std::string base(); static std::string get(Host& newHost, const char* const ip); static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// struct Rounds { - static const char* base(); + static std::string base(); static std::string delegates(Host& newHost, const char* const roundId); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// -struct Transactions { - static const char* base(); +struct Transactions { // NOLINT + static std::string base(); static std::string getUnconfirmed(Host& newHost, const char* const identifier); static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); static std::string get(Host& newHost, const char* const identifier); @@ -93,21 +134,22 @@ struct Transactions { static std::pair send(Host& newHost, std::string& jsonTransaction); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// -struct Votes { - static const char* base(); +struct Votes { // NOLINT + static std::string base(); static std::string get(Host& newHost, const char* const identifier); static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); }; -/***/ +//////////////////////////////////////////////////////////////////////////////// -struct Wallets { - static const char* base(); +struct Wallets { // NOLINT + static std::string base(); static std::string get(Host& newHost, const char* const identifier); static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY); static std::string top(Host& newHost, const char* const query = DEFAULT_QUERY); + static std::string locks(Host& newHost, const char* const identifier, const char* const query = DEFAULT_QUERY); static std::string transactions(Host& newHost, const char* const identifier, const char* const query = DEFAULT_QUERY); static std::string transactionsSent(Host& newHost, const char* const identifier, const char* const query = DEFAULT_QUERY); static std::string transactionsReceived(Host& newHost, const char* const identifier, const char* const query = DEFAULT_QUERY); @@ -117,6 +159,8 @@ struct Wallets { const char* const query = DEFAULT_QUERY); }; +//////////////////////////////////////////////////////////////////////////////// + } // namespace paths } // namespace api } // namespace Client diff --git a/src/api/wallets/wallets.cpp b/src/api/wallets/wallets.cpp index c2f8e2e5..5a0b50a8 100644 --- a/src/api/wallets/wallets.cpp +++ b/src/api/wallets/wallets.cpp @@ -31,6 +31,15 @@ std::string Wallets::top(const char* const query) { /**/ +std::string Wallets::locks(const char *const identifier, + const char* const query) { + return http_->get(paths::Wallets::locks(this->host_, + identifier, + query).c_str()); +} + +/**/ + std::string Wallets::transactions(const char *const identifier, const char* const query) { return http_->get(paths::Wallets::transactions(this->host_, diff --git a/src/api/wallets/wallets.h b/src/api/wallets/wallets.h index 34843de7..9e842001 100644 --- a/src/api/wallets/wallets.h +++ b/src/api/wallets/wallets.h @@ -27,6 +27,7 @@ class IWallets : public Base { virtual std::string get(const char *const identifier) = 0; virtual std::string all(const char* const query) = 0; virtual std::string top(const char* const query) = 0; + virtual std::string locks(const char *const identifier, const char* const query) = 0; virtual std::string transactions(const char *const identifier, const char* const query) = 0; virtual std::string transactionsReceived(const char *const identifier, const char* const query) = 0; virtual std::string transactionsSent(const char *const identifier, const char* const query) = 0; @@ -46,6 +47,7 @@ class Wallets : public IWallets { std::string get(const char *const identifier) override; std::string all(const char* const query) override; std::string top(const char* const query) override; + std::string locks(const char *const identifier, const char* const query) override; std::string transactions(const char *const identifier, const char* const query) override; std::string transactionsReceived(const char *const identifier, const char* const query) override; std::string transactionsSent(const char *const identifier, const char* const query) override; diff --git a/src/host/host.cpp b/src/host/host.cpp index 4acd3efe..5fb239ea 100644 --- a/src/host/host.cpp +++ b/src/host/host.cpp @@ -41,7 +41,7 @@ std::string Host::toString() { out.reserve(IP_MAX_STRING_LEN + PORT_MAX_STRING_LEN); out += (this->ip_); out += ":"; - snprintf(&out[out.length()], PORT_MAX_STRING_LEN, "%d", this->port_); + snprintf(&out[out.find(":") + 1U], PORT_MAX_STRING_LEN, "%d", this->port_); return out; } diff --git a/src/http/http.cpp b/src/http/http.cpp index 8b26c490..3c8ff44b 100644 --- a/src/http/http.cpp +++ b/src/http/http.cpp @@ -8,9 +8,9 @@ **/ #include "http/http.h" + #include -#include -#include "helpers/client_helpers.h" +#include #ifdef ESP8266 #include @@ -18,75 +18,55 @@ #include #endif +#include "helpers/client_helpers.h" + namespace Ark { namespace Client { namespace { +//////////////////////////////////////////////////////////////////////////////// + class PlatformHTTP : public AbstractHTTP { public: PlatformHTTP() = default; - int tryConnection(HTTPClient &httpClient, const char* request ) { - if (!httpClient.begin(request)) { - return -1; - }; - int code = httpClient.GET(); - int count = 0; - while (code != HTTP_CODE_OK) { - httpClient.end(); - if (count >=2) { - return code; - }; - delay(1000); - httpClient.addHeader("Content-Type", "application/json"); - httpClient.begin(request); - code = httpClient.GET(); - count++; - } - return code; - } - - /**/ - - // Arduino's HTTPClient requires that a single-line HTTP request string begins with 'http://'. - // This is only a consideration on IoT platforms. - inline std::string toHttpStr(const char* request) { - // char[7 (size of char string "http://") + 'request' string-length + 1 (for the null terminator '\0')] - size_t length = 0; - while (request[length] != 0) { length++; }; - char httpRequest[7 + length + 1]; - snprintf(httpRequest, length, "http://%s%c", request, '\0'); - return httpRequest; - } - - /**/ +//////////////////////////////////////////////////////////////////////////////// std::string get(const char* request) override { HTTPClient httpClient; httpClient.setReuse(false); httpClient.setTimeout(3000); - if (int code = tryConnection(httpClient, - toHttpStr(request).c_str()) != 200) { - return httpClient.errorToString(-code).c_str(); // <- note `-` symbol. - } + + httpClient.addHeader("Content-Type", "application/json"); + httpClient.begin(std::string("http://" + std::string(request)).c_str()); + + httpClient.GET(); + return httpClient.getString().c_str(); } - /**/ +//////////////////////////////////////////////////////////////////////////////// std::string post(const char* request, const char *body) override { HTTPClient httpClient; httpClient.setReuse(true); httpClient.setTimeout(3000); - httpClient.begin(toHttpStr(request).c_str()); + httpClient.addHeader("Content-Type", "application/json"); + httpClient.begin(std::string("http://" + std::string(request)).c_str()); + httpClient.POST(body); return httpClient.getString().c_str(); } }; +//////////////////////////////////////////////////////////////////////////////// + } // namespace +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + /** * HTTP object factory **/ @@ -94,5 +74,7 @@ std::unique_ptr makeHTTP() { return std::unique_ptr(new PlatformHTTP()); } +//////////////////////////////////////////////////////////////////////////////// + } // namespace Client } // namespace Ark