Add who_am_i() method to Api.
This commit is contained in:
parent
d6a27f814a
commit
939bcffc61
6 changed files with 53 additions and 7 deletions
14
src/main.cpp
14
src/main.cpp
|
@ -2,8 +2,13 @@
|
|||
#include "orotool_config.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
oro::Api oro_api(duck::g_base_url, "RESTC_CPP", "Testing");
|
||||
int main(int argc, char* argv[]) {
|
||||
if (2 != argc) {
|
||||
std::cerr << "Please provide your API key\n";
|
||||
return 2;
|
||||
}
|
||||
|
||||
oro::Api oro_api(duck::g_base_url, argv[1], "RESTC_CPP", "Testing");
|
||||
auto ping = oro_api.ping();
|
||||
|
||||
std::cout << "date: " << ping.first.date << '\n';
|
||||
|
@ -16,5 +21,10 @@ int main() {
|
|||
std::cout << "timestamp: " << ping.second.generation_timestamp << '\n';
|
||||
std::cout << "answer: " << ping.second.message << '\n';
|
||||
std::cout << "version: " << ping.second.version << '\n';
|
||||
|
||||
{
|
||||
auto whoami = oro_api.who_am_i();
|
||||
std::cout << "master id: " << whoami.second.master_id << '\n';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -14,14 +14,23 @@ BOOST_FUSION_ADAPT_STRUCT(
|
|||
(int, version)
|
||||
)
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
oro::WhoAmI,
|
||||
(oro::Timestamp, generation_timestamp)
|
||||
(int, version)
|
||||
(unsigned int, master_id)
|
||||
)
|
||||
|
||||
namespace oro {
|
||||
namespace {
|
||||
constexpr const char g_endpoint_ping[] = "api/v1/ping";
|
||||
constexpr const char g_endpoint_whoami[] = "api/v1/whoami";
|
||||
|
||||
template <typename T>
|
||||
std::pair<Header, T> call_rest_api (
|
||||
rc::RestClient& rest_client,
|
||||
const std::string& url,
|
||||
const std::string& api_key,
|
||||
const std::string& client_name,
|
||||
const std::string& client_purpose
|
||||
) {
|
||||
|
@ -30,6 +39,7 @@ namespace {
|
|||
.Get(url)
|
||||
.Header("X-Client", client_name)
|
||||
.Header("X-Client-Purpose", client_purpose)
|
||||
.Header("x-api-key", api_key)
|
||||
.Execute();
|
||||
|
||||
Header h;
|
||||
|
@ -60,8 +70,14 @@ namespace {
|
|||
}
|
||||
} //unnamed namespace
|
||||
|
||||
Api::Api (std::string&& root_address, std::string&& client_name, std::string&& client_purpose) :
|
||||
Api::Api (
|
||||
std::string&& root_address,
|
||||
std::string&& api_key,
|
||||
std::string&& client_name,
|
||||
std::string&& client_purpose
|
||||
) :
|
||||
m_prefix(std::move(root_address)),
|
||||
m_api_key(std::move(api_key)),
|
||||
m_client_name(std::move(client_name)),
|
||||
m_client_purpose(std::move(client_purpose)),
|
||||
m_client(rc::RestClient::Create())
|
||||
|
@ -73,7 +89,11 @@ Api::Api (std::string&& root_address, std::string&& client_name, std::string&& c
|
|||
Api::~Api() noexcept = default;
|
||||
|
||||
std::pair<Header, Ping> Api::ping() {
|
||||
return call_rest_api<oro::Ping>(*m_client, m_prefix + g_endpoint_ping, m_client_name, m_client_purpose);
|
||||
return call_rest_api<oro::Ping>(*m_client, m_prefix + g_endpoint_ping, m_api_key, m_client_name, m_client_purpose);
|
||||
}
|
||||
|
||||
std::pair<Header, WhoAmI> Api::who_am_i() {
|
||||
return call_rest_api<oro::WhoAmI>(*m_client, m_prefix + g_endpoint_whoami, m_api_key, m_client_name, m_client_purpose);
|
||||
}
|
||||
|
||||
} //namespace oro
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "ping.hpp"
|
||||
#include "whoami.hpp"
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
|
@ -47,15 +48,18 @@ class Api {
|
|||
public:
|
||||
Api (
|
||||
std::string&& root_address,
|
||||
std::string&& api_key,
|
||||
std::string&& client_name,
|
||||
std::string&& client_purpose
|
||||
);
|
||||
~Api() noexcept;
|
||||
|
||||
std::pair<Header, Ping> ping();
|
||||
std::pair<Header, WhoAmI> who_am_i();
|
||||
|
||||
private:
|
||||
std::string m_prefix;
|
||||
std::string m_api_key;
|
||||
std::string m_client_name;
|
||||
std::string m_client_purpose;
|
||||
|
||||
|
|
|
@ -14,5 +14,10 @@ namespace oro {
|
|||
timestamp_t ts;
|
||||
};
|
||||
|
||||
struct BaseJsonReply {
|
||||
Timestamp generation_timestamp;
|
||||
int version;
|
||||
};
|
||||
|
||||
std::ostream& operator<< (std::ostream& os, const Timestamp& ts);
|
||||
} //namespace oro
|
||||
|
|
|
@ -4,9 +4,7 @@
|
|||
#include <string>
|
||||
|
||||
namespace oro {
|
||||
struct Ping {
|
||||
Timestamp generation_timestamp;
|
||||
struct Ping : BaseJsonReply {
|
||||
std::string message;
|
||||
int version;
|
||||
};
|
||||
} //namespace oro
|
||||
|
|
9
src/oro/whoami.hpp
Normal file
9
src/oro/whoami.hpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "datatypes.hpp"
|
||||
|
||||
namespace oro {
|
||||
struct WhoAmI : BaseJsonReply {
|
||||
unsigned int master_id;
|
||||
};
|
||||
} //namespace oro
|
Loading…
Reference in a new issue