diff options
| author | 2017-08-23 21:09:34 -0400 | |
|---|---|---|
| committer | 2017-08-25 23:10:02 -0400 | |
| commit | 04bd0c957e583a518121626deb029f214cc98cf6 (patch) | |
| tree | 5fa503204b059f95add63b40494b359c51de3422 /src/web_service/web_backend.cpp | |
| parent | qt: Add an option to view/regenerate telemetry ID. (diff) | |
| download | yuzu-04bd0c957e583a518121626deb029f214cc98cf6.tar.gz yuzu-04bd0c957e583a518121626deb029f214cc98cf6.tar.xz yuzu-04bd0c957e583a518121626deb029f214cc98cf6.zip | |
web_services: Refactor to remove dependency on Core.
Diffstat (limited to 'src/web_service/web_backend.cpp')
| -rw-r--r-- | src/web_service/web_backend.cpp | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 96ddf6c3c..e50c3a301 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp | |||
| @@ -5,36 +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" | ||
| 9 | #include "web_service/web_backend.h" | 8 | #include "web_service/web_backend.h" |
| 10 | 9 | ||
| 11 | namespace WebService { | 10 | namespace WebService { |
| 12 | 11 | ||
| 13 | static constexpr char API_VERSION[]{"1"}; | 12 | static constexpr char API_VERSION[]{"1"}; |
| 14 | 13 | ||
| 15 | void PostJson(const std::string& url, const std::string& data) { | 14 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 16 | if (!Settings::values.enable_telemetry) { | 15 | const std::string& username, const std::string& token) { |
| 17 | // Telemetry disabled by user configuration | 16 | if (url.empty()) { |
| 17 | LOG_ERROR(WebService, "URL is invalid"); | ||
| 18 | return; | 18 | return; |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | if (url.empty()) { | 21 | const bool are_credentials_provided{!token.empty() && !username.empty()}; |
| 22 | LOG_ERROR(WebService, "URL is invalid"); | 22 | if (!allow_anonymous && !are_credentials_provided) { |
| 23 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); | ||
| 23 | return; | 24 | return; |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 26 | if (Settings::values.citra_token.empty() || Settings::values.citra_username.empty()) { | 27 | if (are_credentials_provided) { |
| 27 | // Anonymous request if citra token or username are empty | 28 | // Authenticated request if credentials are provided |
| 28 | cpr::PostAsync( | ||
| 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}, | 29 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, |
| 34 | cpr::Header{{"Content-Type", "application/json"}, | 30 | cpr::Header{{"Content-Type", "application/json"}, |
| 35 | {"x-username", Settings::values.citra_username}, | 31 | {"x-username", username}, |
| 36 | {"x-token", Settings::values.citra_token}, | 32 | {"x-token", token}, |
| 37 | {"api-version", API_VERSION}}); | 33 | {"api-version", API_VERSION}}); |
| 34 | } else { | ||
| 35 | // Otherwise, anonymous request | ||
| 36 | cpr::PostAsync( | ||
| 37 | cpr::Url{url}, cpr::Body{data}, | ||
| 38 | cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}); | ||
| 38 | } | 39 | } |
| 39 | } | 40 | } |
| 40 | 41 | ||