Skip to content

Commit

Permalink
redirect server request through AchievementRuntime for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed May 19, 2024
1 parent 6b61901 commit 99c3d27
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
16 changes: 15 additions & 1 deletion src/services/AchievementRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AsyncServerCallCallback, void*>(fCallback, pCallbackData);
ServerCallAsync(pRequest,
[](const rc_api_server_response_t* server_response, void* callback_data)
{
auto* pCallbackPair = reinterpret_cast<std::pair<AsyncServerCallCallback, void*>*>(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);
Expand Down
6 changes: 6 additions & 0 deletions src/services/AchievementRuntime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public:
/// </summary>
void SetPaused(bool bValue) noexcept { m_bPaused = bValue; }

typedef void (*AsyncServerCallCallback)(const rc_api_server_response_t& pResponse, void* pCallbackData);
/// <summary>
/// Makes an asynchronous rc_api server call
/// </summary>
void AsyncServerCall(const rc_api_request_t* pRequest, AsyncServerCallCallback fCallback, void* pCallbackData) const;

class Synchronizer
{
public:
Expand Down
34 changes: 13 additions & 21 deletions src/ui/viewmodels/OverlayRecentGamesPageViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<ra::services::AchievementRuntime>().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<OverlayRecentGamesPageViewModel*>(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);
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/viewmodels/OverlayRecentGamesPageViewModel_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit 99c3d27

Please sign in to comment.