feat(API): Catch dead host and update handling of existing profile

This commit is contained in:
Yimura 2021-09-18 22:10:09 +02:00
parent 3188b044af
commit ce5ccbf48f
No known key found for this signature in database
GPG Key ID: 3D8FF4397E768682

View File

@ -3,7 +3,7 @@
namespace big::api namespace big::api
{ {
const std::string domain = "http://localhost:8080/api/v1"; const std::string domain = "http://home.damon.sh:8089/api/v1";
inline std::string session_id; inline std::string session_id;
namespace util namespace util
@ -41,7 +41,9 @@ namespace big::api
{ {
static bool create_session() static bool create_session()
{ {
if (g_local_player == nullptr) return false; static std::atomic_bool busy = false;
if (busy || g_local_player == nullptr) return false;
busy = true;
const std::string path = "/auth/create_session"; const std::string path = "/auth/create_session";
@ -53,9 +55,11 @@ namespace big::api
{ "rockstar_id", player_info->m_rockstar_id2 } { "rockstar_id", player_info->m_rockstar_id2 }
}; };
try
{
http::Response res = request.send("POST", body.dump(), { http::Response res = request.send("POST", body.dump(), {
"Content-Type: application/json" "Content-Type: application/json"
}); }, 10000ms);
nlohmann::json json; nlohmann::json json;
if (util::parse_body(res, json)) if (util::parse_body(res, json))
@ -64,11 +68,21 @@ namespace big::api
LOG(INFO) << "Create session and received ID: " << session_id.c_str(); LOG(INFO) << "Create session and received ID: " << session_id.c_str();
busy = false;
return true; return true;
} }
}
catch (const std::exception&)
{
LOG(INFO) << "Host is down, unable to create session.";
busy = false;
return false;
}
LOG(INFO) << "Failed to create a session."; LOG(INFO) << "Failed to create a session.";
busy = false;
return false; return false;
} }
} }
@ -77,26 +91,29 @@ namespace big::api
{ {
namespace handling namespace handling
{ {
static bool create_profile(uint32_t handling_hash, const char* name, const char* description, nlohmann::json &handling_data) static bool create_profile(uint32_t handling_hash, const char* name, const char* description, nlohmann::json& handling_data, nlohmann::json& out)
{ {
if (!util::signed_in()) return false;
const std::string path = "/vehicle/handling/create"; const std::string path = "/vehicle/handling/create";
http::Request request(domain + path); http::Request request(domain + path);
nlohmann::json json; out["handling_hash"] = handling_hash;
json["handling_hash"] = handling_hash; out["name"] = std::string(name);
json["name"] = std::string(name); out["description"] = std::string(description);
json["description"] = std::string(description); out["data"] = handling_data;
json["data"] = handling_data;
http::Response res = request.send("POST", json.dump(), { http::Response res = request.send("POST", out.dump(), {
util::authorization_header() util::authorization_header()
}); });
return util::parse_body(res, json); return util::parse_body(res, out);
} }
static bool get_by_share_code(std::string share_code, nlohmann::json& out) static bool get_by_share_code(std::string share_code, nlohmann::json& out)
{ {
if (!util::signed_in()) return false;
const std::string path = "/vehicle/handling/get_by_share_code?share_code="; const std::string path = "/vehicle/handling/get_by_share_code?share_code=";
http::Request request(domain + path + share_code); http::Request request(domain + path + share_code);
@ -110,6 +127,8 @@ namespace big::api
static bool get_my_handling(uint32_t handling_hash, nlohmann::json &out) static bool get_my_handling(uint32_t handling_hash, nlohmann::json &out)
{ {
if (!util::signed_in()) return false;
const std::string path = "/vehicle/handling/get_mine?handling_hash="; const std::string path = "/vehicle/handling/get_mine?handling_hash=";
http::Request request(domain + path + std::to_string(handling_hash)); http::Request request(domain + path + std::to_string(handling_hash));
@ -120,6 +139,27 @@ namespace big::api
return util::parse_body(res, out); return util::parse_body(res, out);
} }
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);
}
} }
} }
} }