Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Paths Interface #46

Merged
merged 1 commit into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,45 @@ const auto walletsSearch = connection.api.wallets.search({"username", "baldninja

#

### Getting an API Path

There are instances when a client may use a gateway or bridge to do http get/post;
for this we have provided an interface for obtaining a properly formatted API Path for a given API Endpoint.
Below are examples of how to access the Path interface:

```cpp
Ark::Client::Host dummyHost("0.0.0.0:4003");

std::string blocksAllPath = Ark::Client::API::Paths::Blocks::all(dummyHost, 5 /* limit */, 1 /* page */);
// blocksAllPath will be the string "0.0.0.0:4003/api/v2/blocks?limit=5&page=1"

std::string delegatesGetPath = Ark::Client::API::Paths::Delegates::get(dummyHost, "boldninja");
// delegatesGetPath will be the string "0.0.0.0:4003/api/v2/delegates/boldninja"

std::string nodeConfigurationPath = Ark::Client::API::Paths::Node::configuration(dummyHost);
// nodeConfigurationPath will be the string "0.0.0.0:4003/api/v2/node/configuration"

std::string peersAllPath = Ark::Client::API::Paths::Peers::all(dummyHost, 5 /* limit */, 1 /* page */);
// peersAllPath will be the string "0.0.0.0:4003/api/v2/peers?limit=5&page=1"

std::string transactionsTypesPath = Ark::Client::API::Paths::Transactions::types(dummyHost);
// transactionsTypesPath will be the string "0.0.0.0:4003/api/v2/transactions/types"

std::string votesGetPath = Ark::Client::API::Paths::Votes::get(dummyHost, "d202acbfa947acac53ada2ac8a0eb662c9f75421ede3b10a42759352968b4ed2");
// votesGetPath will be the string "0.0.0.0:4003/api/v2/votes/d202acbfa947acac53ada2ac8a0eb662c9f75421ede3b10a42759352968b4ed2"

// the following is an example of formatting a path and body parameters for an http post
const std::map<std::string, std::string> searchBody = {
{"username", "baldninja"},
{"address", "DFJ5Z51F1euNNdRUQJKQVdG4h495LZkc6T"},
{"publicKey", "03d3c6889608074b44155ad2e6577c3368e27e6e129c457418eb3e5ed029544e8d"}
};

std::pair<std::string, std::string> walletsSearchPath = Ark::Client::API::Paths::Wallets::search(testHost, searchBody, 5, 1);
// walletsSearchPath.first will be the string "0.0.0.0:4003/api/v2/wallets/search?limit=5&page=1"
// walletsSearchPath.second will be the string "address=DFJ5Z51F1euNNdRUQJKQVdG4h495LZkc6T&publicKey=03d3c6889608074b44155ad2e6577c3368e27e6e129c457418eb3e5ed029544e8d&username=baldninja"
```

# Arduino
**Arduino IDE:**
Download and install the Arduino IDE (>=1.8.5) from the following link:
Expand Down
15 changes: 13 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ project(Ark-Cpp-Client-lib)
hunter_add_package(CURL)
find_package(CURL CONFIG REQUIRED)

set(PLATFORM_SRC
set(PLATFORM_SRC
http/os/http.cpp
)

set(API_SRC
set(HOST_SRC
host/host.cpp
)

set(API_PATHS_SRC
api/paths.cpp
)

set(API_SRC
api/blocks/blocks.cpp
api/delegates/delegates.cpp
api/node/node.cpp
Expand All @@ -22,13 +30,16 @@ set(API_SRC

add_library(Ark-Cpp-Client-lib STATIC
${PLATFORM_SRC}
${HOST_SRC}
${API_PATHS_SRC}
${API_SRC}
)

set(cpp_client_build_include_dirs
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include/cpp-client
)

include_directories(${cpp_client_build_include_dirs})

target_include_directories( ${PROJECT_NAME}
Expand Down
31 changes: 13 additions & 18 deletions src/api/blocks/blocks.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

#include "api/blocks/blocks.h"

#include <cstdio>

std::string Ark::Client::API::Blocks::get(const char *const blockId)
{
char uri[80] = { };
snprintf(uri, sizeof(uri), "%s/%s", Ark::Client::API::Paths::Blocks::base, blockId);
return http_->get(uri);
return http_->get(
Ark::Client::API::Paths::Blocks::get(this->host_, blockId).c_str()
);
}

/***/
Expand All @@ -16,18 +14,18 @@ std::string Ark::Client::API::Blocks::all(
int limit /* = 5 */,
int page /* = 1 */
) {
char uri[256] = { };
snprintf(uri, sizeof(uri), "%s?limit=%d&page=%d", Ark::Client::API::Paths::Blocks::base, limit, page);
return http_->get(uri);
return http_->get(
Ark::Client::API::Paths::Blocks::all(this->host_, limit, page).c_str()
);
}

/***/

std::string Ark::Client::API::Blocks::transactions(const char *const blockId)
{
char uri[256] = { };
snprintf(uri, sizeof(uri), "%s/%s/transactions", Ark::Client::API::Paths::Blocks::base, blockId);
return http_->get(uri);
return http_->get(
Ark::Client::API::Paths::Blocks::transactions(this->host_, blockId).c_str()
);
}

/***/
Expand All @@ -37,11 +35,8 @@ std::string Ark::Client::API::Blocks::search(
int limit /* = 5 */,
int page /* = 1 */
) {
char uri[96] = { };
snprintf(uri, sizeof(uri), "%s?limit=%d&page=%d", Ark::Client::API::Paths::Blocks::search, limit, page);
std::string parameterBuffer;
for (const auto& p : bodyParameters) {
parameterBuffer += p.first + '=' + p.second + '&';
}
return http_->post(uri, parameterBuffer.c_str());
const auto searchPathPair = Ark::Client::API::Paths::Blocks::search(this->host_, bodyParameters, limit, page);
return http_->post(
searchPathPair.first.c_str(), searchPathPair.second.c_str()
);
}
26 changes: 12 additions & 14 deletions src/api/delegates/delegates.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

#include "api/delegates/delegates.h"

#include <cstdio>

std::string Ark::Client::API::Delegates::get(const char *const identifier)
{
char uri[128] = { };
snprintf(uri, sizeof(uri), "%s/%s", Ark::Client::API::Paths::Delegates::base, identifier);
return http_->get(uri);
return http_->get(
Ark::Client::API::Paths::Delegates::get(this->host_, identifier).c_str()
);
}

/***/
Expand All @@ -16,9 +14,9 @@ std::string Ark::Client::API::Delegates::all(
int limit /* = 5 */,
int page /* = 1 */
) {
char uri[128] = { };
snprintf(uri, sizeof(uri), "%s?limit=%d&page=%d", Ark::Client::API::Paths::Delegates::base, limit, page);
return http_->get(uri);
return http_->get(
Ark::Client::API::Paths::Delegates::all(this->host_, limit, page).c_str()
);
}

/***/
Expand All @@ -28,9 +26,9 @@ std::string Ark::Client::API::Delegates::blocks(
int limit /* = 5 */,
int page /* = 1 */
) {
char uri[128] = { };
snprintf(uri, sizeof(uri), "%s/%s/blocks?limit=%d&page=%d", Ark::Client::API::Paths::Delegates::base, identifier, limit, page);
return http_->get(uri);
return http_->get(
Ark::Client::API::Paths::Delegates::blocks(this->host_, identifier, limit, page).c_str()
);
}

/***/
Expand All @@ -40,7 +38,7 @@ std::string Ark::Client::API::Delegates::voters(
int limit /* = 5 */,
int page /* = 1 */
) {
char uri[128] = { };
snprintf(uri, sizeof(uri), "%s/%s/voters?limit=%d&page=%d", Ark::Client::API::Paths::Delegates::base, identifier, limit, page);
return http_->get(uri);
return http_->get(
Ark::Client::API::Paths::Delegates::voters(this->host_, identifier, limit, page).c_str()
);
}
12 changes: 9 additions & 3 deletions src/api/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@

std::string Ark::Client::API::Node::configuration()
{
return http_->get(Ark::Client::API::Paths::Node::configuration);
return http_->get(
Ark::Client::API::Paths::Node::configuration(this->host_).c_str()
);
}

/***/

std::string Ark::Client::API::Node::status()
{
return http_->get(Ark::Client::API::Paths::Node::status);
return http_->get(
Ark::Client::API::Paths::Node::status(this->host_).c_str()
);
}

/***/

std::string Ark::Client::API::Node::syncing()
{
return http_->get(Ark::Client::API::Paths::Node::syncing);
return http_->get(
Ark::Client::API::Paths::Node::syncing(this->host_).c_str()
);
}
Loading