Skip to content

Commit b932426

Browse files
committed
move GAHttpWrapper to public include and allow registering a custom HTTP implementation
1 parent d544623 commit b932426

6 files changed

Lines changed: 40 additions & 4 deletions

File tree

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3-
#include "GACommon.h"
3+
#include <string>
4+
#include <string_view>
5+
#include <vector>
6+
#include <cstdint>
47

58
namespace gameanalytics
69
{

include/GameAnalytics/GameAnalytics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#pragma once
77

88
#include "GameAnalytics/GATypes.h"
9+
#include "GameAnalytics/GAHttpWrapper.h"
910

1011
namespace gameanalytics
1112
{
13+
1214
class GameAnalytics
1315
{
1416
public:
@@ -61,6 +63,10 @@ namespace gameanalytics
6163

6264
static void configureExternalUserId(std::string const& extId);
6365

66+
// Set a custom HTTP implementation. Must be called before initialize().
67+
// If not called, the default cURL implementation is used.
68+
static void configureHttpClient(std::unique_ptr<GAHttpWrapper> httpClient);
69+
6470
// initialize - starting SDK (need configuration before starting)
6571
static void initialize(std::string const& gameKey, std::string const& gameSecret);
6672

source/gameanalytics/GAHTTPApi.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ namespace gameanalytics
2323
constexpr int HTTP_RESPONSE_UNAUTHORIZED = 401;
2424
constexpr int HTTP_RESPONSE_INTERNAL_ERROR = 500;
2525

26+
std::unique_ptr<GAHttpWrapper> GAHTTPApi::pendingCustomImpl = nullptr;
27+
2628
// Constructor - setup the basic information for HTTP
2729
GAHTTPApi::GAHTTPApi():
28-
impl(std::make_unique<GAHttpCurl>())
30+
impl(pendingCustomImpl ? std::move(pendingCustomImpl) : std::make_unique<GAHttpCurl>())
2931
{
3032
if(impl)
3133
{
@@ -56,6 +58,11 @@ namespace gameanalytics
5658
return state::GAState::getInstance()._gaHttp;
5759
}
5860

61+
void GAHTTPApi::setCustomHttpImpl(std::unique_ptr<GAHttpWrapper> customImpl)
62+
{
63+
pendingCustomImpl = std::move(customImpl);
64+
}
65+
5966
EGAHTTPApiResponse GAHTTPApi::requestInitReturningDict(json& json_out, std::string const& configsHash)
6067
{
6168
if(!impl)

source/gameanalytics/GAHTTPApi.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#pragma once
77

88
#include "GACommon.h"
9-
#include "Http/GAHttpWrapper.h"
9+
#include "GameAnalytics/GAHttpWrapper.h"
1010

1111
#include <vector>
1212
#include <map>
@@ -127,6 +127,8 @@ namespace gameanalytics
127127

128128
static GAHTTPApi& getInstance();
129129

130+
static void setCustomHttpImpl(std::unique_ptr<GAHttpWrapper> customImpl);
131+
130132
EGAHTTPApiResponse requestInitReturningDict(json& json_out, std::string const& configsHash);
131133
EGAHTTPApiResponse sendEventsInArray(json& json_out, const json& eventArray);
132134
void sendSdkErrorEvent(EGASdkErrorCategory category, EGASdkErrorArea area, EGASdkErrorAction action, EGASdkErrorParameter parameter, std::string const& reason, std::string const& gameKey, std::string const& secretKey);
@@ -159,6 +161,8 @@ namespace gameanalytics
159161
static constexpr int MaxCount = 10;
160162
std::map<ErrorType, int> countMap;
161163
std::map<ErrorType, int64_t> timestampMap;
164+
165+
static std::unique_ptr<GAHttpWrapper> pendingCustomImpl;
162166
};
163167

164168
constexpr const char* GAHTTPApi::sdkErrorCategoryString(EGASdkErrorCategory value)

source/gameanalytics/GameAnalytics.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,22 @@ namespace gameanalytics
350350

351351
// ----------------------- INITIALIZE ---------------------- //
352352

353+
void GameAnalytics::configureHttpClient(std::unique_ptr<GAHttpWrapper> httpClient)
354+
{
355+
if(_endThread)
356+
{
357+
return;
358+
}
359+
360+
if (isSdkReady(true, false))
361+
{
362+
logging::GALogger::w("HTTP client must be set before SDK is initialized.");
363+
return;
364+
}
365+
366+
http::GAHTTPApi::setCustomHttpImpl(std::move(httpClient));
367+
}
368+
353369
void GameAnalytics::initialize(std::string const& gameKey, std::string const& gameSecret)
354370
{
355371
if(_endThread)

source/gameanalytics/Http/GAHttpCurl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "Http/GAHttpWrapper.h"
3+
#include "GameAnalytics/GAHttpWrapper.h"
44
#include <curl/curl.h>
55

66
namespace gameanalytics

0 commit comments

Comments
 (0)