Simplify RestRequest struct.
This commit is contained in:
+6
-2
@@ -195,14 +195,18 @@ public:
|
|||||||
explicit Exception(Error error, const std::string& details = "",
|
explicit Exception(Error error, const std::string& details = "",
|
||||||
bool timeout = false);
|
bool timeout = false);
|
||||||
|
|
||||||
Error error() const { return error_; }
|
Error error() const {
|
||||||
|
return error_;
|
||||||
|
}
|
||||||
|
|
||||||
// Note that `noexcept` is required by GCC.
|
// Note that `noexcept` is required by GCC.
|
||||||
const char* what() const WEBCC_NOEXCEPT override{
|
const char* what() const WEBCC_NOEXCEPT override{
|
||||||
return msg_.c_str();
|
return msg_.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool timeout() const { return timeout_; }
|
bool timeout() const {
|
||||||
|
return timeout_;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Error error_;
|
Error error_;
|
||||||
|
|||||||
@@ -20,11 +20,7 @@ void RestRequestHandler::HandleConnection(HttpConnectionPtr connection) {
|
|||||||
|
|
||||||
const Url& url = http_request->url();
|
const Url& url = http_request->url();
|
||||||
|
|
||||||
// TODO
|
RestRequest rest_request{ http_request };
|
||||||
RestRequest rest_request{
|
|
||||||
http_request,
|
|
||||||
http_request->method(), http_request->content(), url.query()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get service by URL path.
|
// Get service by URL path.
|
||||||
std::string path = "/" + url.path();
|
std::string path = "/" + url.path();
|
||||||
|
|||||||
+23
-15
@@ -8,13 +8,16 @@ namespace webcc {
|
|||||||
|
|
||||||
void RestListService::Handle(const RestRequest& request,
|
void RestListService::Handle(const RestRequest& request,
|
||||||
RestResponse* response) {
|
RestResponse* response) {
|
||||||
if (request.method == http::methods::kGet) {
|
const std::string& method = request.http->method();
|
||||||
Get(UrlQuery(request.url_query_str), response);
|
|
||||||
} else if (request.method == http::methods::kPost) {
|
if (method == http::methods::kGet) {
|
||||||
Post(request.content, response);
|
Get(UrlQuery(request.http->url().query()), response);
|
||||||
|
|
||||||
|
} else if (method == http::methods::kPost) {
|
||||||
|
Post(request.http->content(), response);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LOG_ERRO("RestListService doesn't support '%s' method.",
|
LOG_ERRO("RestListService doesn't support '%s' method.", method.c_str());
|
||||||
request.method.c_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,17 +25,22 @@ void RestListService::Handle(const RestRequest& request,
|
|||||||
|
|
||||||
void RestDetailService::Handle(const RestRequest& request,
|
void RestDetailService::Handle(const RestRequest& request,
|
||||||
RestResponse* response) {
|
RestResponse* response) {
|
||||||
if (request.method == http::methods::kGet) {
|
const std::string& method = request.http->method();
|
||||||
Get(request.url_matches, UrlQuery(request.url_query_str), response);
|
|
||||||
} else if (request.method == http::methods::kPut) {
|
if (method == http::methods::kGet) {
|
||||||
Put(request.url_matches, request.content, response);
|
Get(request.url_matches, UrlQuery(request.http->url().query()), response);
|
||||||
} else if (request.method == http::methods::kPatch) {
|
|
||||||
Patch(request.url_matches, request.content, response);
|
} else if (method == http::methods::kPut) {
|
||||||
} else if (request.method == http::methods::kDelete) {
|
Put(request.url_matches, request.http->content(), response);
|
||||||
|
|
||||||
|
} else if (method == http::methods::kPatch) {
|
||||||
|
Patch(request.url_matches, request.http->content(), response);
|
||||||
|
|
||||||
|
} else if (method == http::methods::kDelete) {
|
||||||
Delete(request.url_matches, response);
|
Delete(request.url_matches, response);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LOG_ERRO("RestDetailService doesn't support '%s' method.",
|
LOG_ERRO("RestDetailService doesn't support '%s' method.", method.c_str());
|
||||||
request.method.c_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-11
@@ -28,15 +28,6 @@ struct RestRequest {
|
|||||||
// Original HTTP request.
|
// Original HTTP request.
|
||||||
HttpRequestPtr http;
|
HttpRequestPtr http;
|
||||||
|
|
||||||
// HTTP method (GET, POST, etc.).
|
|
||||||
const std::string& method;
|
|
||||||
|
|
||||||
// Request content (JSON string).
|
|
||||||
const std::string& content;
|
|
||||||
|
|
||||||
// Query string of the URL (only for GET).
|
|
||||||
const std::string& url_query_str;
|
|
||||||
|
|
||||||
// Regex sub-matches of the URL (usually resource ID's).
|
// Regex sub-matches of the URL (usually resource ID's).
|
||||||
UrlMatches url_matches;
|
UrlMatches url_matches;
|
||||||
};
|
};
|
||||||
@@ -67,7 +58,7 @@ typedef std::shared_ptr<RestService> RestServicePtr;
|
|||||||
|
|
||||||
class RestListService : public RestService {
|
class RestListService : public RestService {
|
||||||
public:
|
public:
|
||||||
void Handle(const RestRequest& request, RestResponse* response) final;
|
void Handle(const RestRequest& request, RestResponse* response) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void Get(const UrlQuery& query, RestResponse* response) {
|
virtual void Get(const UrlQuery& query, RestResponse* response) {
|
||||||
@@ -82,7 +73,7 @@ protected:
|
|||||||
|
|
||||||
class RestDetailService : public RestService {
|
class RestDetailService : public RestService {
|
||||||
public:
|
public:
|
||||||
void Handle(const RestRequest& request, RestResponse* response) final;
|
void Handle(const RestRequest& request, RestResponse* response) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void Get(const UrlMatches& url_matches,
|
virtual void Get(const UrlMatches& url_matches,
|
||||||
|
|||||||
Reference in New Issue
Block a user