From 99c3d276d9925ea19688d45df63e62164c382c21 Mon Sep 17 00:00:00 2001 From: Jamiras Date: Sun, 19 May 2024 10:13:54 -0600 Subject: [PATCH] redirect server request through AchievementRuntime for logging --- src/services/AchievementRuntime.cpp | 16 ++++++++- src/services/AchievementRuntime.hh | 6 ++++ .../OverlayRecentGamesPageViewModel.cpp | 34 +++++++------------ .../OverlayRecentGamesPageViewModel_Tests.cpp | 2 ++ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/services/AchievementRuntime.cpp b/src/services/AchievementRuntime.cpp index 89dc5f81..5bca80f7 100644 --- a/src/services/AchievementRuntime.cpp +++ b/src/services/AchievementRuntime.cpp @@ -319,8 +319,22 @@ static ra::services::Http::Response HandleOfflineRequest(const ra::services::Htt ra::StringPrintf("{\"Success\":false,\"Error\":\"No offline implementation for %s\"}", sApi)); } +void AchievementRuntime::AsyncServerCall(const rc_api_request_t* pRequest, + AsyncServerCallCallback fCallback, void* pCallbackData) const +{ + auto* pCallbackPair = new std::pair(fCallback, pCallbackData); + ServerCallAsync(pRequest, + [](const rc_api_server_response_t* server_response, void* callback_data) + { + auto* pCallbackPair = reinterpret_cast*>(callback_data); + pCallbackPair->first(*server_response, pCallbackPair->second); + delete pCallbackPair; + }, + pCallbackPair, nullptr); +} + void AchievementRuntime::ServerCallAsync(const rc_api_request_t* pRequest, rc_client_server_callback_t fCallback, - void* pCallbackData, rc_client_t*) + void* pCallbackData, rc_client_t*) { ra::services::Http::Request httpRequest(pRequest->url); httpRequest.SetPostData(pRequest->post_data); diff --git a/src/services/AchievementRuntime.hh b/src/services/AchievementRuntime.hh index 9a58cb21..d1dbe61b 100644 --- a/src/services/AchievementRuntime.hh +++ b/src/services/AchievementRuntime.hh @@ -149,6 +149,12 @@ public: /// void SetPaused(bool bValue) noexcept { m_bPaused = bValue; } + typedef void (*AsyncServerCallCallback)(const rc_api_server_response_t& pResponse, void* pCallbackData); + /// + /// Makes an asynchronous rc_api server call + /// + void AsyncServerCall(const rc_api_request_t* pRequest, AsyncServerCallCallback fCallback, void* pCallbackData) const; + class Synchronizer { public: diff --git a/src/ui/viewmodels/OverlayRecentGamesPageViewModel.cpp b/src/ui/viewmodels/OverlayRecentGamesPageViewModel.cpp index d72be7f2..b38332ab 100644 --- a/src/ui/viewmodels/OverlayRecentGamesPageViewModel.cpp +++ b/src/ui/viewmodels/OverlayRecentGamesPageViewModel.cpp @@ -8,7 +8,7 @@ #include "data\context\SessionTracker.hh" #include "data\context\UserContext.hh" -#include "services\Http.hh" +#include "services\AchievementRuntime.hh" #include "services\IConfiguration.hh" #include "services\ILocalStorage.hh" #include "services\IThreadPool.hh" @@ -108,29 +108,21 @@ void OverlayRecentGamesPageViewModel::Refresh() if (rc_api_init_fetch_game_titles_request(&request, &api_params) != RC_OK) return; - ra::services::Http::Request httpRequest(request.url); - httpRequest.SetContentType(request.content_type); - httpRequest.SetPostData(request.post_data); + ra::services::ServiceLocator::Get().AsyncServerCall( + &request, [](const rc_api_server_response_t& server_response, void* pCallbackData) { + rc_api_fetch_game_titles_response_t response{}; - httpRequest.CallAsync([this](const ra::services::Http::Response& pResponse) { - rc_api_fetch_game_titles_response_t response{}; - rc_api_server_response_t server_response; - - memset(&server_response, 0, sizeof(server_response)); - server_response.body = pResponse.Content().c_str(); - server_response.body_length = pResponse.Content().length(); - server_response.http_status_code = ra::etoi(pResponse.StatusCode()); - - if (rc_api_process_fetch_game_titles_server_response(&response, &server_response) == RC_OK && - response.response.succeeded) - { - for (uint32_t i = 0; i < response.num_entries; i++) + if (rc_api_process_fetch_game_titles_server_response(&response, &server_response) == RC_OK && + response.response.succeeded) { - UpdateGameEntry(response.entries[i].id, ra::Widen(response.entries[i].title), - response.entries[i].image_name); + auto* pThis = reinterpret_cast(pCallbackData); + for (uint32_t i = 0; i < response.num_entries; i++) + { + pThis->UpdateGameEntry(response.entries[i].id, ra::Widen(response.entries[i].title), + response.entries[i].image_name); + } } - } - }); + }, this); } }); } diff --git a/tests/ui/viewmodels/OverlayRecentGamesPageViewModel_Tests.cpp b/tests/ui/viewmodels/OverlayRecentGamesPageViewModel_Tests.cpp index b8ee129b..3b71423e 100644 --- a/tests/ui/viewmodels/OverlayRecentGamesPageViewModel_Tests.cpp +++ b/tests/ui/viewmodels/OverlayRecentGamesPageViewModel_Tests.cpp @@ -2,6 +2,7 @@ #include "ui\viewmodels\OverlayRecentGamesPageViewModel.hh" +#include "tests\mocks\MockAchievementRuntime.hh" #include "tests\mocks\MockConfiguration.hh" #include "tests\mocks\MockHttpRequester.hh" #include "tests\mocks\MockImageRepository.hh" @@ -94,6 +95,7 @@ TEST_CLASS(OverlayRecentGamesPageViewModel_Tests) TEST_METHOD(TestRefreshOneGameDataFetched) { OverlayRecentGamesPageViewModelHarness gamesPage; + ra::services::mocks::MockAchievementRuntime achievementRuntime; gamesPage.mockSessions.MockSession(3U, 1234567890U, std::chrono::seconds(5000)); gamesPage.mockUserContext.Initialize("Username", "APITOKEN");