Skip to content

Commit

Permalink
refactor: small changes
Browse files Browse the repository at this point in the history
Small changes.

Signed-off-by: Melg Eight <public.melg8@gmail.com>
Signed-off-by: melg8 <public.melg8@gmail.com>
  • Loading branch information
melg8 committed Jun 9, 2024
1 parent 5a0ab71 commit f3de488
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
83 changes: 43 additions & 40 deletions sources/coal/application/sources/auth_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@

namespace coal {

auto CallApiMethod(std::string_view url_text,
std::string_view method,
std::string_view args,
Cookies cookies) -> cobalt::promise<Result<HttpResponse>> {
const auto api_url = fmt::format("{}/api/{}", url_text, method);
return SendHttpPostRequestTo(api_url, method, args, cookies);
}

[[nodiscard]] static auto FormatOnlyLoginData(const Credentials& credentials)
namespace {
[[nodiscard]] auto FormatOnlyLoginData(const Credentials& credentials)
-> std::string {
return fmt::format(
"{{\"email\":\"{}\",\"password\":\"{}\",\"only_login\":true}}",
credentials.email, credentials.password);
return fmt::format(R"({{"email":"{}","password":"{}","only_login":true}})",
credentials.email, credentials.password);
}

[[nodiscard]] static auto FromCookie(std::string_view cookie)
-> Result<UserAuthData> {
[[nodiscard]] auto FromCookie(std::string_view cookie) -> Result<UserAuthData> {
const auto match = ctre::match<R"(.*auth=(.*?)-(.*?);.*)">(cookie);
if (match) {
return UserAuthData{.id = match.get<1>().to_string(),
Expand All @@ -43,9 +34,9 @@ auto CallApiMethod(std::string_view url_text,
}
}

[[nodiscard]] static auto FromResponse(const HttpResponse& repsonse) noexcept
[[nodiscard]] auto FromResponse(const HttpResponse& response) noexcept
-> Result<UserAuthData> {
const auto header = repsonse.base();
const auto& header = response.base();
for (const auto& field : header) {
const auto& field_name = field.name_string();
if (field_name == "Set-Cookie" || field_name == "set-cookie") {
Expand All @@ -57,36 +48,18 @@ auto CallApiMethod(std::string_view url_text,
return std::make_error_code(std::errc::protocol_error);
}

auto AuthTo(std::string_view server_url, const Credentials& credentials)
-> cobalt::promise<Result<UserAuthData>> {
const auto maybe_response = co_await CallApiMethod(
server_url, "signup_or_login", FormatOnlyLoginData(credentials));
if (maybe_response.has_error()) {
co_return maybe_response.error();
}
const auto response = maybe_response.value();
const auto body = response.body();
if (body.find("Logged In!") == std::string::npos) {
spdlog::error("Can't login, with credentials: {} server response: {}",
credentials, body);
co_return Result<UserAuthData>{
std::make_error_code(std::errc::permission_denied)};
}
co_return FromResponse(response);
}

[[nodiscard]] static auto AuthCookieFrom(const UserAuthData& user_auth_data)
[[nodiscard]] auto AuthCookieFrom(const UserAuthData& user_auth_data)
-> std::string {
return fmt::format("auth={}-{}", user_auth_data.id, user_auth_data.token);
}

[[nodiscard]] static auto ServersAndCharactersFrom(std::string_view json_body)
[[nodiscard]] auto ServersAndCharactersFrom(std::string_view json_body)
-> Result<ServersAndCharactersResponse> {
if (json_body.empty()) {
spdlog::error("Got empty json body for servers and characters parsing");
return std::make_error_code(std::errc::invalid_argument);
}
spdlog::info("Got json_body anwer: {}", json_body);
spdlog::info("Got json_body answer: {}", json_body);
const auto response =
glz::read_json<ServersAndCharactersResponse>(ReducedFrom(json_body));
if (!response) {
Expand All @@ -98,15 +71,45 @@ auto AuthTo(std::string_view server_url, const Credentials& credentials)
return response.value();
}

auto GetServersAndCharacters(std::string_view url_text,
UserAuthData user_auth_data)
} // namespace

auto CallApiMethod(std::string_view url_text,
std::string_view method,
std::string_view args,
Cookies cookies) -> cobalt::promise<Result<HttpResponse>> {
const auto api_url = fmt::format("{}/api/{}", url_text, method);
return SendHttpPostRequestTo(api_url, method, args, cookies);
}

auto AuthTo(std::string_view server_url, const Credentials& credentials)
-> cobalt::promise<Result<UserAuthData>> {
const auto maybe_response = co_await CallApiMethod(
server_url, "signup_or_login", FormatOnlyLoginData(credentials));
if (maybe_response.has_error()) {
spdlog::error("Can't login, with credentials: {} server response: {}",
credentials, maybe_response.error().message());
co_return maybe_response.error();
}
const auto response = maybe_response.value();
const auto body = response.body();
if (body.find("Logged In!") == std::string::npos) {
spdlog::error("Can't login, with credentials: {} server response: {}",
credentials, body);
co_return Result<UserAuthData>{
std::make_error_code(std::errc::permission_denied)};
}
co_return FromResponse(response);
}

auto ServersAndCharactersFrom(std::string_view url_text,
UserAuthData user_auth_data)
-> cobalt::promise<Result<ServersAndCharactersResponse>> {
const auto auth_cookie = AuthCookieFrom(user_auth_data);
Cookies cookies = {auth_cookie};
const auto maybe_answer =
co_await CallApiMethod(url_text, "servers_and_characters", "{}", cookies);
if (maybe_answer.has_error()) {
spdlog::error("Can't obtains servers and characters: {}",
spdlog::error("Can't obtain servers and characters: {}",
maybe_answer.error().message());
co_return maybe_answer.error();
}
Expand Down
4 changes: 2 additions & 2 deletions sources/coal/application/sources/auth_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ auto CallApiMethod(std::string_view url_text,
auto AuthTo(std::string_view server_url, const Credentials& credentials)
-> cobalt::promise<Result<UserAuthData>>;

auto GetServersAndCharacters(std::string_view url_text,
UserAuthData user_auth_data)
auto ServersAndCharactersFrom(std::string_view url_text,
UserAuthData user_auth_data)
-> cobalt::promise<Result<ServersAndCharactersResponse>>;

} // namespace coal
Expand Down
2 changes: 1 addition & 1 deletion sources/coal/application/sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static auto TestAuthTo(Configuration config) -> cobalt::task<void> {
result.value().token);

const auto servers_result =
co_await GetServersAndCharacters(config.url, result.value());
co_await ServersAndCharactersFrom(config.url, result.value());
if (servers_result.has_error()) {
spdlog::error("Error occured while getting servers and characters: {}",
servers_result.error().message());
Expand Down

0 comments on commit f3de488

Please sign in to comment.