summaryrefslogtreecommitdiff
path: root/src/web_service/web_backend.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2017-08-24 19:27:13 -0400
committerGravatar bunnei2017-08-25 23:37:47 -0400
commitc8562b21d91625333218d69cddff104057273e43 (patch)
treeff390bc1f78888fefcf633358fa5e914ffbece50 /src/web_service/web_backend.cpp
parentweb_services: Refactor to remove dependency on Core. (diff)
downloadyuzu-c8562b21d91625333218d69cddff104057273e43.tar.gz
yuzu-c8562b21d91625333218d69cddff104057273e43.tar.xz
yuzu-c8562b21d91625333218d69cddff104057273e43.zip
web_backend: Fix asynchronous JSON post by spawning new thread.
Diffstat (limited to '')
-rw-r--r--src/web_service/web_backend.cpp27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index e50c3a301..a6070fc0f 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -2,8 +2,9 @@
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 <cstdlib>
6#include <thread>
5#include <cpr/cpr.h> 7#include <cpr/cpr.h>
6#include <stdlib.h>
7#include "common/logging/log.h" 8#include "common/logging/log.h"
8#include "web_service/web_backend.h" 9#include "web_service/web_backend.h"
9 10
@@ -11,6 +12,19 @@ namespace WebService {
11 12
12static constexpr char API_VERSION[]{"1"}; 13static constexpr char API_VERSION[]{"1"};
13 14
15static void PostJsonAuthenticated(const std::string& url, const std::string& data,
16 const std::string& username, const std::string& token) {
17 cpr::Post(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"},
18 {"x-username", username},
19 {"x-token", token},
20 {"api-version", API_VERSION}});
21}
22
23static void PostJsonAnonymous(const std::string& url, const std::string& data) {
24 cpr::Post(cpr::Url{url}, cpr::Body{data},
25 cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}});
26}
27
14void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, 28void PostJson(const std::string& url, const std::string& data, bool allow_anonymous,
15 const std::string& username, const std::string& token) { 29 const std::string& username, const std::string& token) {
16 if (url.empty()) { 30 if (url.empty()) {
@@ -24,18 +38,13 @@ void PostJson(const std::string& url, const std::string& data, bool allow_anonym
24 return; 38 return;
25 } 39 }
26 40
41 // Post JSON asynchronously by spawning a new thread
27 if (are_credentials_provided) { 42 if (are_credentials_provided) {
28 // Authenticated request if credentials are provided 43 // Authenticated request if credentials are provided
29 cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, 44 std::thread{PostJsonAuthenticated, url, data, username, token}.detach();
30 cpr::Header{{"Content-Type", "application/json"},
31 {"x-username", username},
32 {"x-token", token},
33 {"api-version", API_VERSION}});
34 } else { 45 } else {
35 // Otherwise, anonymous request 46 // Otherwise, anonymous request
36 cpr::PostAsync( 47 std::thread{PostJsonAnonymous, url, data}.detach();
37 cpr::Url{url}, cpr::Body{data},
38 cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}});
39 } 48 }
40} 49}
41 50