diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/web_service/web_backend.cpp | 41 | ||||
| -rw-r--r-- | src/web_service/web_backend.h | 22 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index c7bd8a38a..6d5470157 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp | |||
| @@ -2,8 +2,49 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <cpr/cpr.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include "common/logging/log.h" | ||
| 5 | #include "web_service/web_backend.h" | 8 | #include "web_service/web_backend.h" |
| 6 | 9 | ||
| 7 | namespace WebService { | 10 | namespace WebService { |
| 8 | 11 | ||
| 12 | static constexpr char ENV_VAR_USERNAME[]{"CITRA_WEB_SERVICES_USERNAME"}; | ||
| 13 | static constexpr char ENV_VAR_TOKEN[]{"CITRA_WEB_SERVICES_TOKEN"}; | ||
| 14 | |||
| 15 | static std::string GetEnvironmentVariable(const char* name) { | ||
| 16 | const char* value{getenv(name)}; | ||
| 17 | if (value) { | ||
| 18 | return value; | ||
| 19 | } | ||
| 20 | return {}; | ||
| 21 | } | ||
| 22 | |||
| 23 | const std::string& GetUsername() { | ||
| 24 | static const std::string username{GetEnvironmentVariable(ENV_VAR_USERNAME)}; | ||
| 25 | return username; | ||
| 26 | } | ||
| 27 | |||
| 28 | const std::string& GetToken() { | ||
| 29 | static const std::string token{GetEnvironmentVariable(ENV_VAR_TOKEN)}; | ||
| 30 | return token; | ||
| 31 | } | ||
| 32 | |||
| 33 | void PostJson(const std::string& url, const std::string& data) { | ||
| 34 | if (url.empty()) { | ||
| 35 | LOG_ERROR(WebService, "URL is invalid"); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | if (GetUsername().empty() || GetToken().empty()) { | ||
| 40 | LOG_ERROR(WebService, "Environment variables %s and %s must be set to POST JSON", | ||
| 41 | ENV_VAR_USERNAME, ENV_VAR_TOKEN); | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | |||
| 45 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"}, | ||
| 46 | {"x-username", GetUsername()}, | ||
| 47 | {"x-token", GetToken()}}); | ||
| 48 | } | ||
| 49 | |||
| 9 | } // namespace WebService | 50 | } // namespace WebService |
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index 1f2e08f54..2753d3b68 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h | |||
| @@ -4,6 +4,28 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <string> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | |||
| 7 | namespace WebService { | 10 | namespace WebService { |
| 8 | 11 | ||
| 12 | /** | ||
| 13 | * Gets the current username for accessing services.citra-emu.org. | ||
| 14 | * @returns Username as a string, empty if not set. | ||
| 15 | */ | ||
| 16 | const std::string& GetUsername(); | ||
| 17 | |||
| 18 | /** | ||
| 19 | * Gets the current token for accessing services.citra-emu.org. | ||
| 20 | * @returns Token as a string, empty if not set. | ||
| 21 | */ | ||
| 22 | const std::string& GetToken(); | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Posts JSON to services.citra-emu.org. | ||
| 26 | * @param url URL of the services.citra-emu.org endpoint to post data to. | ||
| 27 | * @param data String of JSON data to use for the body of the POST request. | ||
| 28 | */ | ||
| 29 | void PostJson(const std::string& url, const std::string& data); | ||
| 30 | |||
| 9 | } // namespace WebService | 31 | } // namespace WebService |