Skip to content

Commit

Permalink
chore: add parsing of servers and characters
Browse files Browse the repository at this point in the history
Add parsing of json for servers and characters request.

Signed-off-by: Melg Eight <public.melg8@gmail.com>
  • Loading branch information
melg8 committed May 11, 2024
1 parent 108a358 commit 12d2f06
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ CMakeUserPresets.json
# Coverage
*coverage
*.info
.config.json
4 changes: 2 additions & 2 deletions Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Start testing: May 10 00:39 UTC
Start testing: May 10 21:00 UTC
----------------------------------------------------------
End testing: May 10 00:39 UTC
End testing: May 10 21:00 UTC
31 changes: 11 additions & 20 deletions sources/coal/application/sources/auth_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <ctre.hpp>
#include <glaze/glaze.hpp>

#include <algorithm>
#include <cctype>

namespace coal {
Expand Down Expand Up @@ -50,7 +49,6 @@ cobalt::promise<Result<HttpResponse>> CallApiMethod(std::string_view url_text,
const auto& field_name = field.name_string();
if (field_name == "Set-Cookie" || field_name == "set-cookie") {
const auto& cookie = field.value();
spdlog::info("Got set-cookie: {}", cookie);
return FromCookie(cookie);
}
}
Expand Down Expand Up @@ -80,28 +78,21 @@ cobalt::promise<Result<UserAuthData>> AuthTo(std::string_view server_url,
return fmt::format("auth={}-{}", user_auth_data.id, user_auth_data.token);
}

static inline void Prettify(const auto& in, auto& out) noexcept {
glz::context ctx{};
glz::detail::prettify_json<glz::opts{}>(ctx, in, out);
spdlog::info("After prettify ctx error?: {}, code: {}", ctx.includer_error,
static_cast<int>(ctx.error));
}

static std::string RemoveSpaces(std::string str) {
str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end());
return str;
}

[[nodiscard]] static Result<ServersAndCharactersResponse>
ServersAndCharactersFrom(std::string json_body) {
std::string beautiful;
Prettify(RemoveSpaces(json_body), beautiful);
spdlog::info("Got servers and characters json:{}", beautiful);
ServersAndCharactersFrom(std::string_view json_body) {
if (json_body.empty()) {
spdlog::error("Got empty json body for servers and characters parsing");
return {}; // TODO(melg): add proper error code.
return std::make_error_code(std::errc::invalid_argument);
}
const auto s =
glz::read_json<ServersAndCharactersResponse>(ReducedFrom(json_body));
if (!s) {
spdlog::error("Failed to parse servers and charactersjson: {}",
ReducedFrom(json_body));
return std::make_error_code(std::errc::bad_message);
}
return {};
spdlog::info("Servers and characters json parse succeeded");
return s.value();
}

cobalt::promise<Result<ServersAndCharactersResponse>> GetServersAndCharacters(
Expand Down
43 changes: 33 additions & 10 deletions sources/coal/application/sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <boost/cobalt/this_coro.hpp>
#include <boost/cobalt/this_thread.hpp>
#include <boost/url.hpp>
#include <glaze/glaze.hpp>

#include <chrono>
#include <coroutine>
Expand Down Expand Up @@ -58,8 +59,14 @@ struct HandlesRegistry {

struct SubscribableSocket {
cobalt::generator<int> socket;
HandlesRegistry
registry; // TODO(melg): split to repeatable and single shot registry.

// TODO(melg): split to repeatable and single shot registry.
HandlesRegistry registry;
};

struct Configuration {
std::string url = {};
Credentials credentials = {};
};

static auto OnValue(HandlesRegistry& registry, int value) {
Expand Down Expand Up @@ -134,9 +141,8 @@ static cobalt::task<void> TestHttpRequests() {
co_return;
}

static cobalt::task<void> TestAuthTo(std::string_view url,
const Credentials& credentials) {
const auto result = co_await AuthTo(url, credentials);
static cobalt::task<void> TestAuthTo(Configuration config) {
const auto result = co_await AuthTo(config.url, config.credentials);
if (result.has_error()) {
spdlog::error("Error occured while login attempt: {} bailing out",
result.error().message());
Expand All @@ -146,13 +152,15 @@ static cobalt::task<void> TestAuthTo(std::string_view url,
result.value().token);

const auto servers_result =
co_await GetServersAndCharacters(url, result.value());
co_await GetServersAndCharacters(config.url, result.value());
if (servers_result.has_error()) {
spdlog::error("Error occured while getting servers and characters: {}",
servers_result.error().message());
co_return;
}
spdlog::info("Got servers and characters information");
spdlog::info("Got {} servers and {} characters information",
servers_result.value().servers.size(),
servers_result.value().characters.size());
}

static void SetupSpdLog() noexcept {
Expand All @@ -170,10 +178,25 @@ static cobalt::task<void> Test() {
spdlog::info("After distributing messages");
}

[[nodiscard]] static Configuration ConfigurationFromFile(
const std::string& path) {
Configuration config = {"http://127.0.0.1:8083",
{"test@test.com", "123456789"}};

const auto read_error = glz::read_file_json(config, path, std::string{});
if (read_error) {
spdlog::warn("Can't get configuration file, will use default values");
const auto write_error = glz::write_file_json(config, path, std::string{});
if (write_error) {
spdlog::warn("Can't write default configuration file");
}
}
return config;
}

static cobalt::task<void> TestAuthConnectivity() {
const auto url = "http://127.0.0.1:8083";
Credentials credentials = {"test@test.com", "123456789"};
co_await TestAuthTo(url, credentials);
const auto config = ConfigurationFromFile("./.config.json");
co_await TestAuthTo(config);
}

} // namespace coal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#define SERVERS_AND_CHARACTERS_RESPONSE_FROM_JSON_H

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <variant>
#include <vector>
Expand All @@ -18,14 +20,16 @@ struct Character {
std::string name = {};
size_t level = {};
std::string type = {};
int online = {};
double online = {};
std::string skin = {};
std::unordered_map<std::string, std::string> cx = {};
std::string in = {};
std::string map = {};
double x = 0.0;
double y = 0.0;
std::string home = {};
std::optional<std::string> server = {};
std::optional<std::string> secret = {};
};

using Characters = std::vector<Character>;
Expand Down Expand Up @@ -68,6 +72,10 @@ struct ServersAndCharactersResponse {
Rewards rewards = {};
};

[[nodiscard]] inline auto ReducedFrom(std::string_view json_data) {
return json_data.substr(1, json_data.size() - 2);
}

} // namespace coal

#endif // SERVERS_AND_CHARACTERS_RESPONSE_FROM_JSON_H
3 changes: 2 additions & 1 deletion sources/coal/tests/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT

find_package(glaze REQUIRED)
find_package(spdlog REQUIRED)

file(GLOB_RECURSE ALL_COAL_TESTS_APPLICATION_HEADERS
"${CMAKE_CURRENT_SOURCE_DIR}/*.h")
Expand All @@ -14,7 +15,7 @@ add_executable(coal_tests "${ALL_COAL_TESTS_APPLICATION_HEADERS}"
"${ALL_COAL_TESTS_APPLICATION_SOURCES}")

target_link_libraries(
coal_tests PUBLIC coal testing_framework glaze::glaze)
coal_tests PUBLIC coal testing_framework glaze::glaze spdlog::spdlog)

add_test(coal_tests coal_tests)

Expand Down

0 comments on commit 12d2f06

Please sign in to comment.