summaryrefslogtreecommitdiff
path: root/src/web_service/web_backend.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2017-08-22 23:06:56 -0400
committerGravatar bunnei2017-08-25 23:10:01 -0400
commit41328afb5852230e1f7c486c4ca20fbc9354a7f8 (patch)
treeb33914155ba505fe76210ca19ee6f49ea7af9b87 /src/web_service/web_backend.cpp
parenttelemetry: Log frontend type. (diff)
downloadyuzu-41328afb5852230e1f7c486c4ca20fbc9354a7f8.tar.gz
yuzu-41328afb5852230e1f7c486c4ca20fbc9354a7f8.tar.xz
yuzu-41328afb5852230e1f7c486c4ca20fbc9354a7f8.zip
web_backend: User config for username and token, support anonymous post.
Diffstat (limited to '')
-rw-r--r--src/web_service/web_backend.cpp45
1 files changed, 17 insertions, 28 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index 13e4555ac..96ddf6c3c 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -5,48 +5,37 @@
5#include <cpr/cpr.h> 5#include <cpr/cpr.h>
6#include <stdlib.h> 6#include <stdlib.h>
7#include "common/logging/log.h" 7#include "common/logging/log.h"
8#include "core/settings.h"
8#include "web_service/web_backend.h" 9#include "web_service/web_backend.h"
9 10
10namespace WebService { 11namespace WebService {
11 12
12static constexpr char API_VERSION[]{"1"}; 13static constexpr char API_VERSION[]{"1"};
13static constexpr char ENV_VAR_USERNAME[]{"CITRA_WEB_SERVICES_USERNAME"};
14static constexpr char ENV_VAR_TOKEN[]{"CITRA_WEB_SERVICES_TOKEN"};
15 14
16static std::string GetEnvironmentVariable(const char* name) { 15void PostJson(const std::string& url, const std::string& data) {
17 const char* value{getenv(name)}; 16 if (!Settings::values.enable_telemetry) {
18 if (value) { 17 // Telemetry disabled by user configuration
19 return value; 18 return;
20 } 19 }
21 return {};
22}
23
24const std::string& GetUsername() {
25 static const std::string username{GetEnvironmentVariable(ENV_VAR_USERNAME)};
26 return username;
27}
28 20
29const std::string& GetToken() {
30 static const std::string token{GetEnvironmentVariable(ENV_VAR_TOKEN)};
31 return token;
32}
33
34void PostJson(const std::string& url, const std::string& data) {
35 if (url.empty()) { 21 if (url.empty()) {
36 LOG_ERROR(WebService, "URL is invalid"); 22 LOG_ERROR(WebService, "URL is invalid");
37 return; 23 return;
38 } 24 }
39 25
40 if (GetUsername().empty() || GetToken().empty()) { 26 if (Settings::values.citra_token.empty() || Settings::values.citra_username.empty()) {
41 LOG_ERROR(WebService, "Environment variables %s and %s must be set to POST JSON", 27 // Anonymous request if citra token or username are empty
42 ENV_VAR_USERNAME, ENV_VAR_TOKEN); 28 cpr::PostAsync(
43 return; 29 cpr::Url{url}, cpr::Body{data},
30 cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}});
31 } else {
32 // We have both, do an authenticated request
33 cpr::PostAsync(cpr::Url{url}, cpr::Body{data},
34 cpr::Header{{"Content-Type", "application/json"},
35 {"x-username", Settings::values.citra_username},
36 {"x-token", Settings::values.citra_token},
37 {"api-version", API_VERSION}});
44 } 38 }
45
46 cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"},
47 {"x-username", GetUsername()},
48 {"x-token", GetToken()},
49 {"api-version", API_VERSION}});
50} 39}
51 40
52} // namespace WebService 41} // namespace WebService