From bcfb1e4c928f3df3da2c8fb9165a9e6fbe3fefe5 Mon Sep 17 00:00:00 2001 From: Adam Gu Date: Mon, 23 Apr 2018 13:22:59 +0800 Subject: [PATCH] Add query parameters to RestService::Handle() --- example/rest_book_server/book_services.cc | 2 ++ example/rest_book_server/book_services.h | 2 ++ src/webcc/rest_server.cc | 4 ++++ src/webcc/rest_service.h | 10 ++++++++-- unittest/rest_service_manager_test.cc | 1 + 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/example/rest_book_server/book_services.cc b/example/rest_book_server/book_services.cc index 4c6f6de..e84339e 100644 --- a/example/rest_book_server/book_services.cc +++ b/example/rest_book_server/book_services.cc @@ -117,6 +117,7 @@ static bool BookFromJson(const std::string& json, Book* book) { bool BookListService::Handle(const std::string& http_method, const std::vector& url_sub_matches, + const std::map& query, const std::string& request_content, std::string* response_content) { 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, const std::vector& url_sub_matches, + const std::map& query, const std::string& request_content, std::string* response_content) { if (url_sub_matches.size() != 1) { diff --git a/example/rest_book_server/book_services.h b/example/rest_book_server/book_services.h index f3fcf73..ab78180 100644 --- a/example/rest_book_server/book_services.h +++ b/example/rest_book_server/book_services.h @@ -22,6 +22,7 @@ public: bool Handle(const std::string& http_method, const std::vector& url_sub_matches, + const std::map& query, const std::string& request_content, std::string* response_content) override; }; @@ -41,6 +42,7 @@ public: bool Handle(const std::string& http_method, const std::vector& url_sub_matches, + const std::map& query, const std::string& request_content, std::string* response_content) override; }; diff --git a/src/webcc/rest_server.cc b/src/webcc/rest_server.cc index c115e31..39199c9 100644 --- a/src/webcc/rest_server.cc +++ b/src/webcc/rest_server.cc @@ -84,10 +84,14 @@ HttpStatus::Enum RestRequestHandler::HandleSession(HttpSessionPtr session) { return HttpStatus::kBadRequest; } + // TODO: Only for GET? + Url::Query query = Url::SplitQuery(url.query()); + // TODO: Error handling. std::string content; service->Handle(session->request().method(), sub_matches, + query, session->request().content(), &content); diff --git a/src/webcc/rest_service.h b/src/webcc/rest_service.h index 0a69bfc..49fba3d 100644 --- a/src/webcc/rest_service.h +++ b/src/webcc/rest_service.h @@ -1,6 +1,7 @@ #ifndef WEBCC_REST_SERVICE_H_ #define WEBCC_REST_SERVICE_H_ +#include #include #include #include @@ -16,10 +17,15 @@ public: } // Handle REST request, output the response. - // Both the request and response parameters should be JSON. - // TODO: Query parameters. + // \param http_method GET, POST, etc. + // \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, const std::vector& url_sub_matches, + const std::map& query, const std::string& request_content, std::string* response_content) = 0; }; diff --git a/unittest/rest_service_manager_test.cc b/unittest/rest_service_manager_test.cc index 6b08d95..652c888 100644 --- a/unittest/rest_service_manager_test.cc +++ b/unittest/rest_service_manager_test.cc @@ -9,6 +9,7 @@ class TestRestService : public RestService { public: bool Handle(const std::string& http_method, const std::vector& url_sub_matches, + const std::map& query, const std::string& request_content, std::string* response_content) override { return true;