Skip to content

Commit

Permalink
refactor: use v2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Serein207 committed Oct 20, 2024
1 parent d3a2939 commit 2ea137f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ target_compile_definitions(${PROJECT_NAME}
$<$<CONFIG:Release>:EVENTO_RELEASE>
${PLATFORM}
LOCALE_DIR="${SOURCE_LOCALE_DIR}"
EVENTO_API_V1
# EVENTO_API_V1
)

target_link_libraries(${PROJECT_NAME}
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Core/AccountManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void AccountManager::performLogin() {
#else
self.setKeychainRefreshToken(data.refreshToken);
self.scheduleRenewAccessToken();
self.expiredTime = std::chrono::system_clock::now() + std::chrono::days(7);
self._expiredTime = std::chrono::system_clock::now() + std::chrono::days(7);
#endif
self.setNetworkAccessToken(data.accessToken);
self.setLoginState(true);
Expand Down Expand Up @@ -147,7 +147,7 @@ void AccountManager::requestLogout() {
self._userInfo = UserInfoEntity();
#ifndef EVENTO_API_V1
setKeychainRefreshToken("");
renewAccessTokenTimer.cancel();
_renewAccessTokenTimer.cancel();
#endif
setNetworkAccessToken("");

Expand Down
4 changes: 4 additions & 0 deletions src/Infrastructure/Network/Api/Evento.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ struct Evento {
req.set(http::field::host, url.host());
req.set(http::field::user_agent, "SAST-Evento-Desktop/2");
if (token) // set token if exists
#ifdef EVENTO_API_V1
req.set("TOKEN", *token);
#else
req.set(http::field::authorization, std::format("Bearer {}", *token));
#endif

if (verb == http::verb::post) {
req.set(http::field::content_type, MIME_FORM_URL_ENCODED);
Expand Down
30 changes: 16 additions & 14 deletions src/Infrastructure/Network/NetworkClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Task<Result<LoginResEntity>> NetworkClient::loginViaSastLink(std::string code) {

#else
auto result = co_await this->request<api::Evento>(http::verb::post,
endpoint("/login/link"),
endpoint("/v2/login/link"),
{{"code", code}, {"type", "0"}});

if (result.isErr())
Expand Down Expand Up @@ -136,7 +136,7 @@ Task<Result<UserInfoEntity>> NetworkClient::getUserInfo() {

Task<Result<void>> NetworkClient::refreshAccessToken(std::string refreshToken) {
auto result = co_await this->request<api::Evento>(http::verb::post,
endpoint("/refresh-token"),
endpoint("/v2/refresh-token"),
{{"refreshToken", refreshToken}});
if (result.isErr())
co_return Err(result.unwrapErr());
Expand Down Expand Up @@ -211,11 +211,12 @@ Task<Result<EventQueryRes>> NetworkClient::getLatestEventList(

co_return Ok(eventEntityListV1ToV2(list));
#else
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/query",
{{"start", "now"}}),
{},
cacheTtl);
auto result = co_await this->request<api::Evento>(
http::verb::get,
endpoint("/v2/client/event/query",
{{"start", stdChrono2Iso8601Utc(std::chrono::system_clock::now())}}),
{},
cacheTtl);
if (result.isErr())
co_return Err(result.unwrapErr());

Expand Down Expand Up @@ -249,13 +250,14 @@ Task<Result<EventQueryRes>> NetworkClient::getHistoryEventList(

co_return Ok(eventEntityListV1ToV2(list));
#else
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/query",
{{"page", std::to_string(page)},
{"size", std::to_string(size)},
{"end", "now"}}),
{},
cacheTtl);
auto result = co_await this->request<api::Evento>(
http::verb::get,
endpoint("/v2/client/event/query",
{{"page", std::to_string(page)},
{"size", std::to_string(size)},
{"end", stdChrono2Iso8601Utc(std::chrono::system_clock::now())}}),
{},
cacheTtl);
if (result.isErr())
co_return Err(result.unwrapErr());

Expand Down
7 changes: 7 additions & 0 deletions src/Infrastructure/Utils/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ inline time_t parseIso8601Utc(const char* date) {
#endif
}

inline std::string stdChrono2Iso8601Utc(std::chrono::system_clock::time_point time) {
std::stringstream ss;
auto timeT = std::chrono::system_clock::to_time_t(time);
ss << std::put_time(std::gmtime(&timeT), "%Y-%m-%dT%H:%M:%S.000Z");
return ss.str();
}

inline std::string firstDateTimeOfWeek() {
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
Expand Down

0 comments on commit 2ea137f

Please sign in to comment.