Add query parameters to RestService::Handle()

master
Adam Gu 7 years ago
parent f921256a33
commit bcfb1e4c92

@ -117,6 +117,7 @@ static bool BookFromJson(const std::string& json, Book* book) {
bool BookListService::Handle(const std::string& http_method, bool BookListService::Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const std::map<std::string, std::string>& query,
const std::string& request_content, const std::string& request_content,
std::string* response_content) { std::string* response_content) {
if (http_method == webcc::kHttpGet) { if (http_method == webcc::kHttpGet) {
@ -149,6 +150,7 @@ bool BookListService::Handle(const std::string& http_method,
bool BookDetailService::Handle(const std::string& http_method, bool BookDetailService::Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const std::map<std::string, std::string>& query,
const std::string& request_content, const std::string& request_content,
std::string* response_content) { std::string* response_content) {
if (url_sub_matches.size() != 1) { if (url_sub_matches.size() != 1) {

@ -22,6 +22,7 @@ public:
bool Handle(const std::string& http_method, bool Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const std::map<std::string, std::string>& query,
const std::string& request_content, const std::string& request_content,
std::string* response_content) override; std::string* response_content) override;
}; };
@ -41,6 +42,7 @@ public:
bool Handle(const std::string& http_method, bool Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const std::map<std::string, std::string>& query,
const std::string& request_content, const std::string& request_content,
std::string* response_content) override; std::string* response_content) override;
}; };

@ -84,10 +84,14 @@ HttpStatus::Enum RestRequestHandler::HandleSession(HttpSessionPtr session) {
return HttpStatus::kBadRequest; return HttpStatus::kBadRequest;
} }
// TODO: Only for GET?
Url::Query query = Url::SplitQuery(url.query());
// TODO: Error handling. // TODO: Error handling.
std::string content; std::string content;
service->Handle(session->request().method(), service->Handle(session->request().method(),
sub_matches, sub_matches,
query,
session->request().content(), session->request().content(),
&content); &content);

@ -1,6 +1,7 @@
#ifndef WEBCC_REST_SERVICE_H_ #ifndef WEBCC_REST_SERVICE_H_
#define WEBCC_REST_SERVICE_H_ #define WEBCC_REST_SERVICE_H_
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -16,10 +17,15 @@ public:
} }
// Handle REST request, output the response. // Handle REST request, output the response.
// Both the request and response parameters should be JSON. // \param http_method GET, POST, etc.
// TODO: Query parameters. // \param url_sub_matches The regex sub-matches in the URL,
// usually resource ID.
// \param query Query parameters in the URL, key value pairs.
// \param request_content Request JSON.
// \param response_content Output response JSON.
virtual bool Handle(const std::string& http_method, virtual bool Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const std::map<std::string, std::string>& query,
const std::string& request_content, const std::string& request_content,
std::string* response_content) = 0; std::string* response_content) = 0;
}; };

@ -9,6 +9,7 @@ class TestRestService : public RestService {
public: public:
bool Handle(const std::string& http_method, bool Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const std::map<std::string, std::string>& query,
const std::string& request_content, const std::string& request_content,
std::string* response_content) override { std::string* response_content) override {
return true; return true;

Loading…
Cancel
Save