diff options
Diffstat (limited to '')
| -rw-r--r-- | src/web_service/web_backend.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h new file mode 100644 index 000000000..d75fbcc15 --- /dev/null +++ b/src/web_service/web_backend.h | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | #include <mutex> | ||
| 9 | #include <string> | ||
| 10 | #include <tuple> | ||
| 11 | #include <httplib.h> | ||
| 12 | #include "common/common_types.h" | ||
| 13 | #include "common/web_result.h" | ||
| 14 | |||
| 15 | namespace httplib { | ||
| 16 | class Client; | ||
| 17 | } | ||
| 18 | |||
| 19 | namespace WebService { | ||
| 20 | |||
| 21 | class Client { | ||
| 22 | public: | ||
| 23 | Client(const std::string& host, const std::string& username, const std::string& token); | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Posts JSON to the specified path. | ||
| 27 | * @param path the URL segment after the host address. | ||
| 28 | * @param data String of JSON data to use for the body of the POST request. | ||
| 29 | * @param allow_anonymous If true, allow anonymous unauthenticated requests. | ||
| 30 | * @return the result of the request. | ||
| 31 | */ | ||
| 32 | Common::WebResult PostJson(const std::string& path, const std::string& data, | ||
| 33 | bool allow_anonymous) { | ||
| 34 | return GenericJson("POST", path, data, allow_anonymous); | ||
| 35 | } | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Gets JSON from the specified path. | ||
| 39 | * @param path the URL segment after the host address. | ||
| 40 | * @param allow_anonymous If true, allow anonymous unauthenticated requests. | ||
| 41 | * @return the result of the request. | ||
| 42 | */ | ||
| 43 | Common::WebResult GetJson(const std::string& path, bool allow_anonymous) { | ||
| 44 | return GenericJson("GET", path, "", allow_anonymous); | ||
| 45 | } | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Deletes JSON to the specified path. | ||
| 49 | * @param path the URL segment after the host address. | ||
| 50 | * @param data String of JSON data to use for the body of the DELETE request. | ||
| 51 | * @param allow_anonymous If true, allow anonymous unauthenticated requests. | ||
| 52 | * @return the result of the request. | ||
| 53 | */ | ||
| 54 | Common::WebResult DeleteJson(const std::string& path, const std::string& data, | ||
| 55 | bool allow_anonymous) { | ||
| 56 | return GenericJson("DELETE", path, data, allow_anonymous); | ||
| 57 | } | ||
| 58 | |||
| 59 | private: | ||
| 60 | /// A generic function handles POST, GET and DELETE request together | ||
| 61 | Common::WebResult GenericJson(const std::string& method, const std::string& path, | ||
| 62 | const std::string& data, bool allow_anonymous); | ||
| 63 | |||
| 64 | /** | ||
| 65 | * A generic function with explicit authentication method specified | ||
| 66 | * JWT is used if the jwt parameter is not empty | ||
| 67 | * username + token is used if jwt is empty but username and token are not empty | ||
| 68 | * anonymous if all of jwt, username and token are empty | ||
| 69 | */ | ||
| 70 | Common::WebResult GenericJson(const std::string& method, const std::string& path, | ||
| 71 | const std::string& data, const std::string& jwt = "", | ||
| 72 | const std::string& username = "", const std::string& token = ""); | ||
| 73 | |||
| 74 | // Retrieve a new JWT from given username and token | ||
| 75 | void UpdateJWT(); | ||
| 76 | |||
| 77 | std::string host; | ||
| 78 | std::string username; | ||
| 79 | std::string token; | ||
| 80 | std::string jwt; | ||
| 81 | std::unique_ptr<httplib::Client> cli; | ||
| 82 | |||
| 83 | struct JWTCache { | ||
| 84 | std::mutex mutex; | ||
| 85 | std::string username; | ||
| 86 | std::string token; | ||
| 87 | std::string jwt; | ||
| 88 | }; | ||
| 89 | static JWTCache jwt_cache; | ||
| 90 | }; | ||
| 91 | |||
| 92 | } // namespace WebService | ||