Merge pull request #4559 from lioncash/webresult
web_service: Move web_result.h into web_service
This commit is contained in:
commit
9619964e8c
7 changed files with 41 additions and 47 deletions
|
@ -172,7 +172,6 @@ add_library(common STATIC
|
||||||
virtual_buffer.h
|
virtual_buffer.h
|
||||||
wall_clock.cpp
|
wall_clock.cpp
|
||||||
wall_clock.h
|
wall_clock.h
|
||||||
web_result.h
|
|
||||||
zstd_compression.cpp
|
zstd_compression.cpp
|
||||||
zstd_compression.h
|
zstd_compression.h
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,6 +5,7 @@ add_library(web_service STATIC
|
||||||
verify_login.h
|
verify_login.h
|
||||||
web_backend.cpp
|
web_backend.cpp
|
||||||
web_backend.h
|
web_backend.h
|
||||||
|
web_result.h
|
||||||
)
|
)
|
||||||
|
|
||||||
create_target_directory_groups(web_service)
|
create_target_directory_groups(web_service)
|
||||||
|
|
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include "common/detached_tasks.h"
|
#include "common/detached_tasks.h"
|
||||||
#include "common/web_result.h"
|
|
||||||
#include "web_service/telemetry_json.h"
|
#include "web_service/telemetry_json.h"
|
||||||
#include "web_service/web_backend.h"
|
#include "web_service/web_backend.h"
|
||||||
|
#include "web_service/web_result.h"
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
|
||||||
|
@ -125,7 +125,7 @@ bool TelemetryJson::SubmitTestcase() {
|
||||||
Client client(impl->host, impl->username, impl->token);
|
Client client(impl->host, impl->username, impl->token);
|
||||||
auto value = client.PostJson("/gamedb/testcase", content, false);
|
auto value = client.PostJson("/gamedb/testcase", content, false);
|
||||||
|
|
||||||
return value.result_code == Common::WebResult::Code::Success;
|
return value.result_code == WebResult::Code::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace WebService
|
} // namespace WebService
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include "common/web_result.h"
|
|
||||||
#include "web_service/verify_login.h"
|
#include "web_service/verify_login.h"
|
||||||
#include "web_service/web_backend.h"
|
#include "web_service/web_backend.h"
|
||||||
|
#include "web_service/web_result.h"
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,14 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <LUrlParser.h>
|
#include <LUrlParser.h>
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
#include "common/common_types.h"
|
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "common/web_result.h"
|
|
||||||
#include "web_service/web_backend.h"
|
#include "web_service/web_backend.h"
|
||||||
|
#include "web_service/web_result.h"
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ struct Client::Impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A generic function handles POST, GET and DELETE request together
|
/// A generic function handles POST, GET and DELETE request together
|
||||||
Common::WebResult GenericRequest(const std::string& method, const std::string& path,
|
WebResult GenericRequest(const std::string& method, const std::string& path,
|
||||||
const std::string& data, bool allow_anonymous,
|
const std::string& data, bool allow_anonymous,
|
||||||
const std::string& accept) {
|
const std::string& accept) {
|
||||||
if (jwt.empty()) {
|
if (jwt.empty()) {
|
||||||
|
@ -42,8 +43,7 @@ struct Client::Impl {
|
||||||
|
|
||||||
if (jwt.empty() && !allow_anonymous) {
|
if (jwt.empty() && !allow_anonymous) {
|
||||||
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
||||||
return Common::WebResult{Common::WebResult::Code::CredentialsMissing,
|
return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""};
|
||||||
"Credentials needed", ""};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = GenericRequest(method, path, data, accept, jwt);
|
auto result = GenericRequest(method, path, data, accept, jwt);
|
||||||
|
@ -62,7 +62,7 @@ struct Client::Impl {
|
||||||
* username + token is used if jwt is empty but username and token are
|
* username + token is used if jwt is empty but username and token are
|
||||||
* not empty anonymous if all of jwt, username and token are empty
|
* not empty anonymous if all of jwt, username and token are empty
|
||||||
*/
|
*/
|
||||||
Common::WebResult GenericRequest(const std::string& method, const std::string& path,
|
WebResult GenericRequest(const std::string& method, const std::string& path,
|
||||||
const std::string& data, const std::string& accept,
|
const std::string& data, const std::string& accept,
|
||||||
const std::string& jwt = "", const std::string& username = "",
|
const std::string& jwt = "", const std::string& username = "",
|
||||||
const std::string& token = "") {
|
const std::string& token = "") {
|
||||||
|
@ -81,12 +81,12 @@ struct Client::Impl {
|
||||||
cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port);
|
cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port);
|
||||||
} else {
|
} else {
|
||||||
LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme);
|
LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme);
|
||||||
return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme", ""};
|
return WebResult{WebResult::Code::InvalidURL, "Bad URL scheme", ""};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cli == nullptr) {
|
if (cli == nullptr) {
|
||||||
LOG_ERROR(WebService, "Invalid URL {}", host + path);
|
LOG_ERROR(WebService, "Invalid URL {}", host + path);
|
||||||
return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL", ""};
|
return WebResult{WebResult::Code::InvalidURL, "Invalid URL", ""};
|
||||||
}
|
}
|
||||||
cli->set_timeout_sec(TIMEOUT_SECONDS);
|
cli->set_timeout_sec(TIMEOUT_SECONDS);
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ struct Client::Impl {
|
||||||
std::string(API_VERSION.begin(), API_VERSION.end()));
|
std::string(API_VERSION.begin(), API_VERSION.end()));
|
||||||
if (method != "GET") {
|
if (method != "GET") {
|
||||||
params.emplace(std::string("Content-Type"), std::string("application/json"));
|
params.emplace(std::string("Content-Type"), std::string("application/json"));
|
||||||
};
|
}
|
||||||
|
|
||||||
httplib::Request request;
|
httplib::Request request;
|
||||||
request.method = method;
|
request.method = method;
|
||||||
|
@ -118,29 +118,28 @@ struct Client::Impl {
|
||||||
|
|
||||||
if (!cli->send(request, response)) {
|
if (!cli->send(request, response)) {
|
||||||
LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
|
LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
|
||||||
return Common::WebResult{Common::WebResult::Code::LibError, "Null response", ""};
|
return WebResult{WebResult::Code::LibError, "Null response", ""};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.status >= 400) {
|
if (response.status >= 400) {
|
||||||
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
||||||
response.status);
|
response.status);
|
||||||
return Common::WebResult{Common::WebResult::Code::HttpError,
|
return WebResult{WebResult::Code::HttpError, std::to_string(response.status), ""};
|
||||||
std::to_string(response.status), ""};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto content_type = response.headers.find("content-type");
|
auto content_type = response.headers.find("content-type");
|
||||||
|
|
||||||
if (content_type == response.headers.end()) {
|
if (content_type == response.headers.end()) {
|
||||||
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
|
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
|
||||||
return Common::WebResult{Common::WebResult::Code::WrongContent, "", ""};
|
return WebResult{WebResult::Code::WrongContent, "", ""};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content_type->second.find(accept) == std::string::npos) {
|
if (content_type->second.find(accept) == std::string::npos) {
|
||||||
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
||||||
content_type->second);
|
content_type->second);
|
||||||
return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content", ""};
|
return WebResult{WebResult::Code::WrongContent, "Wrong content", ""};
|
||||||
}
|
}
|
||||||
return Common::WebResult{Common::WebResult::Code::Success, "", response.body};
|
return WebResult{WebResult::Code::Success, "", response.body};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve a new JWT from given username and token
|
// Retrieve a new JWT from given username and token
|
||||||
|
@ -150,7 +149,7 @@ struct Client::Impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
|
auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
|
||||||
if (result.result_code != Common::WebResult::Code::Success) {
|
if (result.result_code != WebResult::Code::Success) {
|
||||||
LOG_ERROR(WebService, "UpdateJWT failed");
|
LOG_ERROR(WebService, "UpdateJWT failed");
|
||||||
} else {
|
} else {
|
||||||
std::lock_guard lock{jwt_cache.mutex};
|
std::lock_guard lock{jwt_cache.mutex};
|
||||||
|
@ -180,29 +179,28 @@ Client::Client(std::string host, std::string username, std::string token)
|
||||||
|
|
||||||
Client::~Client() = default;
|
Client::~Client() = default;
|
||||||
|
|
||||||
Common::WebResult Client::PostJson(const std::string& path, const std::string& data,
|
WebResult Client::PostJson(const std::string& path, const std::string& data, bool allow_anonymous) {
|
||||||
bool allow_anonymous) {
|
|
||||||
return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json");
|
return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
|
WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
|
||||||
return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json");
|
return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::WebResult Client::DeleteJson(const std::string& path, const std::string& data,
|
WebResult Client::DeleteJson(const std::string& path, const std::string& data,
|
||||||
bool allow_anonymous) {
|
bool allow_anonymous) {
|
||||||
return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json");
|
return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) {
|
WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) {
|
||||||
return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain");
|
return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain");
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::WebResult Client::GetImage(const std::string& path, bool allow_anonymous) {
|
WebResult Client::GetImage(const std::string& path, bool allow_anonymous) {
|
||||||
return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png");
|
return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png");
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::WebResult Client::GetExternalJWT(const std::string& audience) {
|
WebResult Client::GetExternalJWT(const std::string& audience) {
|
||||||
return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false,
|
return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false,
|
||||||
"text/html");
|
"text/html");
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,10 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace Common {
|
|
||||||
struct WebResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
|
||||||
|
struct WebResult;
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
public:
|
public:
|
||||||
Client(std::string host, std::string username, std::string token);
|
Client(std::string host, std::string username, std::string token);
|
||||||
|
@ -25,8 +23,7 @@ public:
|
||||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult PostJson(const std::string& path, const std::string& data,
|
WebResult PostJson(const std::string& path, const std::string& data, bool allow_anonymous);
|
||||||
bool allow_anonymous);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets JSON from the specified path.
|
* Gets JSON from the specified path.
|
||||||
|
@ -34,7 +31,7 @@ public:
|
||||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult GetJson(const std::string& path, bool allow_anonymous);
|
WebResult GetJson(const std::string& path, bool allow_anonymous);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes JSON to the specified path.
|
* Deletes JSON to the specified path.
|
||||||
|
@ -43,8 +40,7 @@ public:
|
||||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult DeleteJson(const std::string& path, const std::string& data,
|
WebResult DeleteJson(const std::string& path, const std::string& data, bool allow_anonymous);
|
||||||
bool allow_anonymous);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a plain string from the specified path.
|
* Gets a plain string from the specified path.
|
||||||
|
@ -52,7 +48,7 @@ public:
|
||||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult GetPlain(const std::string& path, bool allow_anonymous);
|
WebResult GetPlain(const std::string& path, bool allow_anonymous);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an PNG image from the specified path.
|
* Gets an PNG image from the specified path.
|
||||||
|
@ -60,14 +56,14 @@ public:
|
||||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult GetImage(const std::string& path, bool allow_anonymous);
|
WebResult GetImage(const std::string& path, bool allow_anonymous);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests an external JWT for the specific audience provided.
|
* Requests an external JWT for the specific audience provided.
|
||||||
* @param audience the audience of the JWT requested.
|
* @param audience the audience of the JWT requested.
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult GetExternalJWT(const std::string& audience);
|
WebResult GetExternalJWT(const std::string& audience);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Impl;
|
struct Impl;
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
|
||||||
namespace Common {
|
namespace WebService {
|
||||||
struct WebResult {
|
struct WebResult {
|
||||||
enum class Code : u32 {
|
enum class Code : u32 {
|
||||||
Success,
|
Success,
|
||||||
|
@ -22,4 +22,4 @@ struct WebResult {
|
||||||
std::string result_string;
|
std::string result_string;
|
||||||
std::string returned_data;
|
std::string returned_data;
|
||||||
};
|
};
|
||||||
} // namespace Common
|
} // namespace WebService
|
Loading…
Reference in a new issue