183 lines
4.8 KiB
C++
183 lines
4.8 KiB
C++
#include "api.hpp"
|
|
#include "datatypes.hpp"
|
|
#include "dateconv.hpp"
|
|
#include <restc-cpp/restc-cpp.h>
|
|
#include <restc-cpp/RequestBuilder.h>
|
|
#include <boost/fusion/adapted.hpp>
|
|
|
|
namespace rc = restc_cpp;
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Ping,
|
|
(oro::Timestamp, generation_timestamp)
|
|
(std::string, message)
|
|
(int, version)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::WhoAmI,
|
|
(oro::Timestamp, generation_timestamp)
|
|
(int, version)
|
|
(unsigned int, master_id)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Item,
|
|
(unsigned int, item_id)
|
|
(std::string, unique_name)
|
|
(std::string, name)
|
|
(oro::ItemTypeWrapper, type)
|
|
(boost::optional<oro::ItemSubtypeWrapper>, subtype)
|
|
(unsigned int, npc_price)
|
|
(boost::optional<unsigned int>, slots)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Items,
|
|
(oro::Timestamp, generation_timestamp)
|
|
(int, version)
|
|
(std::vector<oro::Item>, items)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Icon,
|
|
(unsigned int, item_id)
|
|
(std::string, icon)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Icons,
|
|
(oro::Timestamp, generation_timestamp)
|
|
(int, version)
|
|
(std::vector<oro::Icon>, icons)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Location,
|
|
(std::string, map)
|
|
(int, x)
|
|
(int, y)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::ShopItem,
|
|
(unsigned int, items_id)
|
|
(unsigned int, amount)
|
|
(unsigned int, price)
|
|
(unsigned int, refine)
|
|
(std::vector<unsigned int>, cards)
|
|
(unsigned int, star_crumbs)
|
|
(boost::optional<std::string>, element)
|
|
(boost::optional<unsigned int>, creator)
|
|
(boost::optional<bool>, beloved)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Shop,
|
|
(std::string, title)
|
|
(std::string, owner)
|
|
(oro::Location, location)
|
|
(oro::Timestamp, creation_date)
|
|
(oro::ShopTypeWrapper, type)
|
|
(std::vector<oro::ShopItem>, items)
|
|
)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
oro::Shops,
|
|
(oro::Timestamp, generation_timestamp)
|
|
(int, version)
|
|
(std::vector<oro::Shop>, shops)
|
|
)
|
|
|
|
namespace oro {
|
|
namespace {
|
|
constexpr const std::int64_t g_max_json_mem = 1024 * 1024 * 20;
|
|
constexpr const char g_endpoint_ping[] = "api/v1/ping";
|
|
constexpr const char g_endpoint_whoami[] = "api/v1/whoami";
|
|
constexpr const char g_endpoint_items_list[] = "api/v1/items/list";
|
|
constexpr const char g_endpoint_items_icons[] = "api/v1/items/icons";
|
|
constexpr const char g_endpoint_market_list[] = "api/v1/market/list";
|
|
|
|
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
|
|
) {
|
|
return rest_client.template ProcessWithPromiseT< std::pair<Header, T> >([&](rc::Context& ctx) {
|
|
std::unique_ptr<rc::Reply> reply = rc::RequestBuilder(ctx)
|
|
.Get(url)
|
|
.Header("X-Client", client_name)
|
|
.Header("X-Client-Purpose", client_purpose)
|
|
.Header("x-api-key", api_key)
|
|
.Execute();
|
|
|
|
Header h;
|
|
if (auto val = reply->GetHeader("Date")) {
|
|
h.date = from_header_timestamp(*val);
|
|
}
|
|
if (auto val = reply->GetHeader("X-RateLimit-Limit")) {
|
|
h.rate_limit = std::stoi(*val);
|
|
}
|
|
if (auto val = reply->GetHeader("X-RateLimit-Remaining")) {
|
|
h.rate_limit_remaining = std::stoi(*val);
|
|
}
|
|
if (auto val = reply->GetHeader("X-RateLimit-Reset")) {
|
|
h.rate_limit_reset = std::stoul(*val);
|
|
}
|
|
if (auto val = reply->GetHeader("Retry-After")) {
|
|
h.retry_after = std::stoul(*val);
|
|
}
|
|
if (auto val = reply->GetHeader("Server")) {
|
|
h.server = std::move(*val);
|
|
}
|
|
|
|
T retval;
|
|
rc::SerializeFromJson(retval, std::move(reply), nullptr, g_max_json_mem);
|
|
|
|
return std::make_pair(std::move(h), std::move(retval));
|
|
}).get();
|
|
}
|
|
} //unnamed namespace
|
|
|
|
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())
|
|
{
|
|
if (not m_prefix.empty() and m_prefix[m_prefix.size() - 1] != '/')
|
|
m_prefix.push_back('/');
|
|
}
|
|
|
|
Api::~Api() noexcept = default;
|
|
|
|
std::pair<Header, Ping> Api::ping() {
|
|
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);
|
|
}
|
|
|
|
std::pair<Header, Items> Api::items_list() {
|
|
return call_rest_api<oro::Items>(*m_client, m_prefix + g_endpoint_items_list, m_api_key, m_client_name, m_client_purpose);
|
|
}
|
|
|
|
std::pair<Header, Icons> Api::items_icons() {
|
|
return call_rest_api<oro::Icons>(*m_client, m_prefix + g_endpoint_items_icons, m_api_key, m_client_name, m_client_purpose);
|
|
}
|
|
|
|
std::pair<Header, Shops> Api::market_list() {
|
|
return call_rest_api<oro::Shops>(*m_client, m_prefix + g_endpoint_market_list, m_api_key, m_client_name, m_client_purpose);
|
|
}
|
|
|
|
} //namespace oro
|