Skip to content

Commit

Permalink
Merge pull request #25 from RaidcoreGG/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
DeltaGW2 authored Mar 5, 2024
2 parents ea600e6 + 7b8ffc2 commit f3f64f6
Show file tree
Hide file tree
Showing 68 changed files with 1,617 additions and 912 deletions.
1 change: 1 addition & 0 deletions src/API/APIResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct APIResponse
{
int Status;
json Content;
//void Headers;
};

#endif
52 changes: 31 additions & 21 deletions src/API/APIClient.cpp → src/API/CAPIClient.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "APIClient.h"
#include "CAPIClient.h"

#include <fstream>

#include "core.h"
#include "Shared.h"

APIClient::APIClient(std::string aBaseURL, bool aEnableSSL, std::filesystem::path aCacheDirectory, int aCacheLifetime, int aBucketCapacity, int aRefillAmount, int aRefillInterval)
CAPIClient::CAPIClient(std::string aBaseURL, bool aEnableSSL, std::filesystem::path aCacheDirectory, int aCacheLifetime, int aBucketCapacity, int aRefillAmount, int aRefillInterval)
{
BaseURL = GetBaseURL(aBaseURL); // sanitize url just to be sure
Client = new httplib::Client(BaseURL);
Expand All @@ -22,13 +22,13 @@ APIClient::APIClient(std::string aBaseURL, bool aEnableSSL, std::filesystem::pat
RefillAmount = aRefillAmount;
RefillInterval = aRefillInterval;

LogDebug(("APIClient::" + BaseURL).c_str(), "APIClient(BaseURL: %s, EnableSSL: %s, CacheDirectory: %s, CacheLifetime: %d, BucketCapacity: %d, RefillAmount: %d, RefillInterval: %d)",
LogDebug(("CAPIClient::" + BaseURL).c_str(), "CAPIClient(BaseURL: %s, EnableSSL: %s, CacheDirectory: %s, CacheLifetime: %d, BucketCapacity: %d, RefillAmount: %d, RefillInterval: %d)",
BaseURL.c_str(),
aEnableSSL ? "true" : "false",
CacheDirectory.string().c_str(),
CacheLifetime, BucketCapacity, RefillAmount, RefillInterval);

WorkerThread = std::thread(&APIClient::ProcessRequests, this);
WorkerThread = std::thread(&CAPIClient::ProcessRequests, this);
WorkerThread.detach();

std::filesystem::path timeOffset = aCacheDirectory / "0";
Expand All @@ -38,7 +38,7 @@ APIClient::APIClient(std::string aBaseURL, bool aEnableSSL, std::filesystem::pat

FileTimeOffset = Timestamp() - std::chrono::duration_cast<std::chrono::seconds>(std::filesystem::last_write_time(timeOffset).time_since_epoch()).count();
}
APIClient::~APIClient()
CAPIClient::~CAPIClient()
{
const std::lock_guard<std::mutex> lock(Mutex);
while (ResponseCache.size() > 0)
Expand All @@ -52,10 +52,10 @@ APIClient::~APIClient()

delete Client;

LogDebug(("APIClient::" + BaseURL).c_str(), "~APIClient(%s)", BaseURL.c_str());
LogDebug(("CAPIClient::" + BaseURL).c_str(), "~CAPIClient(%s)", BaseURL.c_str());
}

json APIClient::Get(std::string aEndpoint, std::string aParameters)
json CAPIClient::Get(std::string aEndpoint, std::string aParameters)
{
std::string query = GetQuery(aEndpoint, aParameters);

Expand All @@ -67,12 +67,12 @@ json APIClient::Get(std::string aEndpoint, std::string aParameters)

if (diff < CacheLifetime && cachedResponse->Content != nullptr)
{
//LogDebug(("APIClient::" + BaseURL).c_str(), "Cached message %d seconds old. Reading from cache.", diff);
//LogDebug(("CAPIClient::" + BaseURL).c_str(), "Cached message %d seconds old. Reading from cache.", diff);
return cachedResponse->Content;
}
else
{
//LogDebug(("APIClient::" + BaseURL).c_str(), "Cached message %d seconds old. CacheLifetime %d. Queueing request.", diff, CacheLifetime);
//LogDebug(("CAPIClient::" + BaseURL).c_str(), "Cached message %d seconds old. CacheLifetime %d. Queueing request.", diff, CacheLifetime);
}
}

Expand Down Expand Up @@ -108,7 +108,7 @@ json APIClient::Get(std::string aEndpoint, std::string aParameters)

return cachedResponse != nullptr ? cachedResponse->Content : json{};
}
void APIClient::Download(std::filesystem::path aOutPath, std::string aEndpoint, std::string aParameters)
void CAPIClient::Download(std::filesystem::path aOutPath, std::string aEndpoint, std::string aParameters)
{
std::string query = GetQuery(aEndpoint, aParameters);

Expand All @@ -123,12 +123,12 @@ void APIClient::Download(std::filesystem::path aOutPath, std::string aEndpoint,

if (!downloadResult || downloadResult->status != 200 || bytesWritten == 0)
{
LogWarning(("APIClient::" + BaseURL).c_str(), "Error fetching %s", query.c_str());
LogWarning(("CAPIClient::" + BaseURL).c_str(), "Error fetching %s", query.c_str());
return;
}
}

CachedResponse* APIClient::GetCachedResponse(const std::string& aQuery)
CachedResponse* CAPIClient::GetCachedResponse(const std::string& aQuery)
{
std::filesystem::path path = GetNormalizedPath(aQuery);

Expand Down Expand Up @@ -157,13 +157,13 @@ CachedResponse* APIClient::GetCachedResponse(const std::string& aQuery)
}
catch (json::parse_error& ex)
{
Log(("APIClient::" + BaseURL).c_str(), "%s could not be parsed. Error: %s", path.string().c_str(), ex.what());
Log(("CAPIClient::" + BaseURL).c_str(), "%s could not be parsed. Error: %s", path.string().c_str(), ex.what());
}
}

return nullptr;
}
std::filesystem::path APIClient::GetNormalizedPath(const std::string& aQuery) const
std::filesystem::path CAPIClient::GetNormalizedPath(const std::string& aQuery) const
{
std::string pathStr = aQuery;

Expand Down Expand Up @@ -208,7 +208,7 @@ std::filesystem::path APIClient::GetNormalizedPath(const std::string& aQuery) co
return CacheDirectory.string() + pathStr.c_str();
}

void APIClient::ProcessRequests()
void CAPIClient::ProcessRequests()
{
for (;;)
{
Expand Down Expand Up @@ -312,7 +312,7 @@ void APIClient::ProcessRequests()
std::error_code err;
if (!CreateDirectoryRecursive(normalizedPath.parent_path().string(), err))
{
LogWarning(("APIClient::" + BaseURL).c_str(), "CreateDirectoryRecursive FAILED, err: % s", err.message());
LogWarning(("CAPIClient::" + BaseURL).c_str(), "CreateDirectoryRecursive FAILED, err: % s", err.message());
}

std::ofstream file(normalizedPath);
Expand Down Expand Up @@ -344,7 +344,7 @@ void APIClient::ProcessRequests()
IsSuspended = true;
}
}
APIResponse APIClient::DoHttpReq(APIRequest aRequest)
APIResponse CAPIClient::DoHttpReq(APIRequest aRequest)
{
APIResponse response{
0,
Expand All @@ -355,14 +355,14 @@ APIResponse APIClient::DoHttpReq(APIRequest aRequest)

if (!result)
{
LogWarning(("APIClient::" + BaseURL).c_str(), "Error fetching %s", aRequest.Query.c_str());
LogWarning(("CAPIClient::" + BaseURL).c_str(), "Error fetching %s", aRequest.Query.c_str());
response.Status = 1;
return response;
}

if (result->status != 200) // not HTTP_OK
{
LogWarning(("APIClient::" + BaseURL).c_str(), "Status %d when fetching %s", result->status, aRequest.Query.c_str());
LogWarning(("CAPIClient::" + BaseURL).c_str(), "Status %d when fetching %s", result->status, aRequest.Query.c_str());
response.Status = result->status;
return response;
}
Expand All @@ -375,16 +375,26 @@ APIResponse APIClient::DoHttpReq(APIRequest aRequest)
}
catch (json::parse_error& ex)
{
LogWarning(("APIClient::" + BaseURL).c_str(), "Response from %s could not be parsed. Error: %s", aRequest.Query.c_str(), ex.what());
LogWarning(("CAPIClient::" + BaseURL).c_str(), "Response from %s could not be parsed. Error: %s", aRequest.Query.c_str(), ex.what());
return response;
}

if (jsonResult.is_null())
{
LogWarning(("APIClient::" + BaseURL).c_str(), "Error parsing API response from %s.", aRequest.Query.c_str());
LogWarning(("CAPIClient::" + BaseURL).c_str(), "Error parsing API response from %s.", aRequest.Query.c_str());
return response;
}

//response.Headers = result->headers;
/*Log(("CAPIClient::" + BaseURL).c_str(), "%s", aRequest.Query.c_str());
for (auto it = result->headers.begin(); it != result->headers.end(); ++it)
{
if (it->first == "Last-Modified")
{
Log("meme", "Last-Modified: %d", LastModifiedToTimestamp(it->second));
}
Log(("CAPIClient::" + BaseURL).c_str(), "%s : %s", it->first.c_str(), it->second.c_str());
}*/
response.Content = jsonResult;
return response;
}
8 changes: 4 additions & 4 deletions src/API/APIClient.h → src/API/CAPIClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
#include "nlohmann/json.hpp"
using json = nlohmann::json;

class APIClient
class CAPIClient
{
public:
/*
APIClient:
CAPIClient:
- aBaseUrl is the API base
- aCacheDirectory is the directory to where the requests will be cached on disk
- aCacheLifetime (seconds) refers to how long a response should still be considered valid, if it's a cached one, before refetching it
- aBucketCapacity refers to the bucket size for requests
- aRefillAmount refers to how many tokens you get back after each interval
- aRefillInterval (seconds) refers to when the bucket gets refilled
*/
APIClient(std::string aBaseURL, bool aEnableSSL, std::filesystem::path aCacheDirectory, int aCacheLifetime, int aBucketCapacity, int aRefillAmount, int aRefillInterval);
~APIClient();
CAPIClient(std::string aBaseURL, bool aEnableSSL, std::filesystem::path aCacheDirectory, int aCacheLifetime, int aBucketCapacity, int aRefillAmount, int aRefillInterval);
~CAPIClient();

/*
Get:
Expand Down
6 changes: 3 additions & 3 deletions src/Consts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ const char* ICON_GENERIC_HOVER = "ICON_GENERIC_HOVER";
const char* ICON_NOTIFICATION = "ICON_NOTIFICATION";
const char* ICON_ADDONS = "ICON_ADDONS";
const char* ICON_OPTIONS = "ICON_OPTIONS";
const char* ICON_OPTIONS_HOVER = "ICON_OPTIONS_HOVER";
const char* ICON_CHANGELOG = "ICON_CHANGELOG";
const char* ICON_LOG = "ICON_LOG";
const char* ICON_DEBUG = "ICON_NOTIFICATION";
const char* ICON_DEBUG = "ICON_DEBUG";
const char* ICON_ABOUT = "ICON_ABOUT";
const char* TEX_MENU_BACKGROUND = "TEX_MENU_BACKGROUND";
const char* TEX_MENU_BUTTON = "TEX_MENU_BUTTON";
Expand All @@ -49,10 +50,9 @@ const char* QA_MENU = "0_QA_MENU";
/* Events */
const char* EV_WINDOW_RESIZED = "EV_WINDOW_RESIZED";
const char* EV_MUMBLE_IDENTITY_UPDATED = "EV_MUMBLE_IDENTITY_UPDATED";
const char* EV_OPTIONS_CALLED = "EV_OPTIONS_CALLED";
const char* EV_EULA_ACCEPTED = "EV_EULA_ACCEPTED";
const char* EV_ADDON_LOADED = "EV_ADDON_LOADED";
const char* EV_ADDON_UNLOADED = "EV_ADDON_UNLOADED";
const char* EV_VOLATILE_ADDONS_DISABLED = "EV_VOLATILE_ADDONS_DISABLED";

/* DataLink */
const char* DL_MUMBLE_LINK = "DL_MUMBLE_LINK";
Expand Down
4 changes: 2 additions & 2 deletions src/Consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern const char* ICON_GENERIC_HOVER;
extern const char* ICON_NOTIFICATION;
extern const char* ICON_ADDONS;
extern const char* ICON_OPTIONS;
extern const char* ICON_OPTIONS_HOVER;
extern const char* ICON_CHANGELOG;
extern const char* ICON_LOG;
extern const char* ICON_DEBUG;
Expand All @@ -50,10 +51,9 @@ extern const char* QA_MENU;
/* Events */
extern const char* EV_WINDOW_RESIZED;
extern const char* EV_MUMBLE_IDENTITY_UPDATED;
extern const char* EV_OPTIONS_CALLED;
extern const char* EV_EULA_ACCEPTED;
extern const char* EV_ADDON_LOADED;
extern const char* EV_ADDON_UNLOADED;
extern const char* EV_VOLATILE_ADDONS_DISABLED;

/* DataLink */
extern const char* DL_MUMBLE_LINK;
Expand Down
Loading

0 comments on commit f3f64f6

Please sign in to comment.