diff options
| author | 2017-06-27 23:18:52 -0400 | |
|---|---|---|
| committer | 2017-07-11 18:33:25 -0400 | |
| commit | a634efa40e988d86ae21d86ab1a93e062614fd0b (patch) | |
| tree | ea3b5d77f869b8f5f59567cb5949057f17467502 /src/web_service/web_backend.cpp | |
| parent | web_service: Add skeleton project. (diff) | |
| download | yuzu-a634efa40e988d86ae21d86ab1a93e062614fd0b.tar.gz yuzu-a634efa40e988d86ae21d86ab1a93e062614fd0b.tar.xz yuzu-a634efa40e988d86ae21d86ab1a93e062614fd0b.zip | |
web_backend: Add initial interface to POST data to Citra Web Services.
Diffstat (limited to '')
| -rw-r--r-- | src/web_service/web_backend.cpp | 41 |
1 files changed, 41 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 |