2021-08-17 16:56:41 +02:00
|
|
|
#pragma once
|
|
|
|
#include "http_request.hpp"
|
|
|
|
|
|
|
|
namespace big::api
|
|
|
|
{
|
2021-09-18 22:10:09 +02:00
|
|
|
const std::string domain = "http://home.damon.sh:8089/api/v1";
|
2021-08-17 16:56:41 +02:00
|
|
|
inline std::string session_id;
|
|
|
|
|
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
static std::string authorization_header()
|
|
|
|
{
|
|
|
|
return std::string("Authorization: ") + api::session_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_body(http::Response& res, nlohmann::json& out)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
out = nlohmann::json::parse(res.body.begin(), res.body.end());
|
|
|
|
|
|
|
|
return out["status"] == std::string("success");
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
out = nullptr;
|
|
|
|
|
|
|
|
LOG(INFO) << "Failed to parse request body: " << std::endl << e.what();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool signed_in()
|
|
|
|
{
|
|
|
|
return !session_id.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace auth
|
|
|
|
{
|
|
|
|
static bool create_session()
|
|
|
|
{
|
2021-09-18 22:10:09 +02:00
|
|
|
static std::atomic_bool busy = false;
|
|
|
|
if (busy || g_local_player == nullptr) return false;
|
|
|
|
busy = true;
|
2021-08-17 16:56:41 +02:00
|
|
|
|
|
|
|
const std::string path = "/auth/create_session";
|
|
|
|
|
|
|
|
http::Request request(domain + path);
|
|
|
|
|
|
|
|
CPlayerInfo* player_info = g_local_player->m_player_info;
|
|
|
|
nlohmann::json body = {
|
|
|
|
{ "username", std::string(player_info->m_name) },
|
|
|
|
{ "rockstar_id", player_info->m_rockstar_id2 }
|
|
|
|
};
|
|
|
|
|
2021-09-18 22:10:09 +02:00
|
|
|
try
|
2021-08-17 16:56:41 +02:00
|
|
|
{
|
2021-09-18 22:10:09 +02:00
|
|
|
http::Response res = request.send("POST", body.dump(), {
|
|
|
|
"Content-Type: application/json"
|
|
|
|
}, 10000ms);
|
|
|
|
|
|
|
|
nlohmann::json json;
|
|
|
|
if (util::parse_body(res, json))
|
|
|
|
{
|
|
|
|
session_id = json["data"]["sessionId"].get<std::string>();
|
2021-08-17 16:56:41 +02:00
|
|
|
|
2021-09-18 22:10:09 +02:00
|
|
|
LOG(INFO) << "Create session and received ID: " << session_id.c_str();
|
2021-08-17 16:56:41 +02:00
|
|
|
|
2021-09-18 22:10:09 +02:00
|
|
|
busy = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception&)
|
|
|
|
{
|
|
|
|
LOG(INFO) << "Host is down, unable to create session.";
|
|
|
|
|
|
|
|
busy = false;
|
|
|
|
return false;
|
2021-08-17 16:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Failed to create a session.";
|
|
|
|
|
2021-09-18 22:10:09 +02:00
|
|
|
busy = false;
|
2021-08-17 16:56:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace vehicle
|
|
|
|
{
|
|
|
|
namespace handling
|
|
|
|
{
|
2021-09-18 22:10:09 +02:00
|
|
|
static bool create_profile(uint32_t handling_hash, const char* name, const char* description, nlohmann::json& handling_data, nlohmann::json& out)
|
2021-08-17 16:56:41 +02:00
|
|
|
{
|
2021-09-18 22:10:09 +02:00
|
|
|
if (!util::signed_in()) return false;
|
|
|
|
|
2021-08-17 16:56:41 +02:00
|
|
|
const std::string path = "/vehicle/handling/create";
|
|
|
|
|
|
|
|
http::Request request(domain + path);
|
|
|
|
|
2021-09-18 22:10:09 +02:00
|
|
|
out["handling_hash"] = handling_hash;
|
|
|
|
out["name"] = std::string(name);
|
|
|
|
out["description"] = std::string(description);
|
|
|
|
out["data"] = handling_data;
|
2021-08-17 16:56:41 +02:00
|
|
|
|
2021-09-18 22:10:09 +02:00
|
|
|
http::Response res = request.send("POST", out.dump(), {
|
2021-08-17 16:56:41 +02:00
|
|
|
util::authorization_header()
|
|
|
|
});
|
2021-09-18 22:10:09 +02:00
|
|
|
return util::parse_body(res, out);
|
2021-08-17 16:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_by_share_code(std::string share_code, nlohmann::json& out)
|
|
|
|
{
|
2021-09-18 22:10:09 +02:00
|
|
|
if (!util::signed_in()) return false;
|
|
|
|
|
2021-08-17 16:56:41 +02:00
|
|
|
const std::string path = "/vehicle/handling/get_by_share_code?share_code=";
|
|
|
|
|
|
|
|
http::Request request(domain + path + share_code);
|
|
|
|
|
|
|
|
http::Response res = request.send("GET", "", {
|
|
|
|
util::authorization_header()
|
|
|
|
});
|
|
|
|
|
|
|
|
return util::parse_body(res, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_my_handling(uint32_t handling_hash, nlohmann::json &out)
|
|
|
|
{
|
2021-09-18 22:10:09 +02:00
|
|
|
if (!util::signed_in()) return false;
|
|
|
|
|
2021-08-17 16:56:41 +02:00
|
|
|
const std::string path = "/vehicle/handling/get_mine?handling_hash=";
|
|
|
|
|
|
|
|
http::Request request(domain + path + std::to_string(handling_hash));
|
|
|
|
|
|
|
|
http::Response res = request.send("GET", "", {
|
|
|
|
util::authorization_header()
|
|
|
|
});
|
|
|
|
|
|
|
|
return util::parse_body(res, out);
|
|
|
|
}
|
2021-09-18 22:10:09 +02:00
|
|
|
|
|
|
|
static bool update(uint32_t handling_hash, const char* name, const char* description, std::string share_code, nlohmann::json &update)
|
|
|
|
{
|
|
|
|
if (!util::signed_in()) return false;
|
|
|
|
|
|
|
|
const std::string path = "/vehicle/handling/update";
|
|
|
|
|
|
|
|
http::Request request(domain + path);
|
|
|
|
|
|
|
|
nlohmann::json json;
|
|
|
|
json["handling_hash"] = handling_hash;
|
|
|
|
json["name"] = std::string(name);
|
|
|
|
json["description"] = std::string(description);
|
|
|
|
json["data"] = update;
|
|
|
|
json["share_code"] = share_code;
|
|
|
|
|
|
|
|
http::Response res = request.send("POST", json.dump(), {
|
|
|
|
util::authorization_header()
|
|
|
|
});
|
|
|
|
return util::parse_body(res, update);
|
|
|
|
}
|
2021-08-17 16:56:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|