Refine REST book examples.
This commit is contained in:
+8
-4
@@ -24,30 +24,34 @@ class RestClient {
|
||||
timeout_seconds_ = timeout_seconds;
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
// HTTP GET request.
|
||||
// The return value indicates if the socket communication is successful
|
||||
// or not. If it's failed, check error() and timed_out() for more details.
|
||||
// For HTTP status, check response_status() instead.
|
||||
inline bool Get(const std::string& url) {
|
||||
return Request(kHttpGet, url, "");
|
||||
}
|
||||
|
||||
// HTTP POST request.
|
||||
inline bool Post(const std::string& url, std::string&& content) {
|
||||
return Request(kHttpPost, url, std::move(content));
|
||||
}
|
||||
|
||||
// HTTP PUT request.
|
||||
inline bool Put(const std::string& url, std::string&& content) {
|
||||
return Request(kHttpPut, url, std::move(content));
|
||||
}
|
||||
|
||||
// HTTP PATCH request.
|
||||
inline bool Patch(const std::string& url, std::string&& content) {
|
||||
return Request(kHttpPatch, url, std::move(content));
|
||||
}
|
||||
|
||||
// HTTP DELETE request.
|
||||
inline bool Delete(const std::string& url) {
|
||||
return Request(kHttpDelete, url, "");
|
||||
}
|
||||
|
||||
// Response & Result
|
||||
|
||||
HttpResponsePtr response() const { return response_; }
|
||||
|
||||
int response_status() const {
|
||||
|
||||
@@ -20,6 +20,9 @@ namespace webcc {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Regex sub-matches of the URL.
|
||||
typedef std::vector<std::string> UrlSubMatches;
|
||||
|
||||
struct RestRequest {
|
||||
// HTTP method (GET, POST, etc.).
|
||||
const std::string& method;
|
||||
@@ -31,7 +34,7 @@ struct RestRequest {
|
||||
const std::string& url_query_str;
|
||||
|
||||
// Regex sub-matches of the URL (usually resource ID's).
|
||||
std::vector<std::string> url_sub_matches;
|
||||
UrlSubMatches url_sub_matches;
|
||||
};
|
||||
|
||||
struct RestResponse {
|
||||
@@ -76,22 +79,22 @@ class RestDetailService : public RestService {
|
||||
void Handle(const RestRequest& request, RestResponse* response) final;
|
||||
|
||||
protected:
|
||||
virtual void Get(const std::vector<std::string>& url_sub_matches,
|
||||
virtual void Get(const UrlSubMatches& url_sub_matches,
|
||||
const UrlQuery& query,
|
||||
RestResponse* response) {
|
||||
}
|
||||
|
||||
virtual void Put(const std::vector<std::string>& url_sub_matches,
|
||||
virtual void Put(const UrlSubMatches& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
RestResponse* response) {
|
||||
}
|
||||
|
||||
virtual void Patch(const std::vector<std::string>& url_sub_matches,
|
||||
virtual void Patch(const UrlSubMatches& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
RestResponse* response) {
|
||||
}
|
||||
|
||||
virtual void Delete(const std::vector<std::string>& url_sub_matches,
|
||||
virtual void Delete(const UrlSubMatches& url_sub_matches,
|
||||
RestResponse* response) {
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user