Refine folder structure; add rapidjson as submodule.

This commit is contained in:
Adam Gu
2018-04-10 17:34:00 +08:00
parent 9abf8d99d5
commit 5bc988b093
61 changed files with 43 additions and 28 deletions
+10
View File
@@ -0,0 +1,10 @@
if(UNIX)
add_definitions(-std=c++11)
endif()
file(GLOB SRCS *.cc *.h)
add_executable(rest_book_client ${SRCS})
target_link_libraries(rest_book_client webcc ${Boost_LIBRARIES})
target_link_libraries(rest_book_client "${CMAKE_THREAD_LIBS_INIT}")
+87
View File
@@ -0,0 +1,87 @@
#include <iostream>
#include "webcc/http_client.h"
#include "webcc/http_request.h"
#include "webcc/http_response.h"
class BookListClient {
public:
BookListClient() {
host_ = "localhost";
port_ = "8080";
}
bool ListBooks() {
webcc::HttpRequest http_request;
http_request.set_method(webcc::kHttpGet);
http_request.set_url("/books");
http_request.SetHost(host_, port_);
http_request.Build();
webcc::HttpResponse http_response;
webcc::HttpClient http_client;
webcc::Error error = http_client.SendRequest(http_request, &http_response);
if (error != webcc::kNoError) {
return false;
}
std::cout << "Book list: " << std::endl
<< http_response.content() << std::endl;
return true;
}
private:
std::string host_;
std::string port_;
};
class BookDetailClient {
public:
BookDetailClient() {
host_ = "localhost";
port_ = "8080";
}
bool GetBook(const std::string& id) {
webcc::HttpRequest http_request;
http_request.set_method(webcc::kHttpGet);
http_request.set_url("/books/" + id);
http_request.SetHost(host_, port_);
http_request.Build();
webcc::HttpResponse http_response;
webcc::HttpClient http_client;
webcc::Error error = http_client.SendRequest(http_request, &http_response);
if (error != webcc::kNoError) {
return false;
}
std::cout << "Book: " << id << std::endl
<< http_response.content() << std::endl;
return true;
}
private:
std::string host_;
std::string port_;
};
int main() {
BookListClient book_list_client;
book_list_client.ListBooks();
BookDetailClient book_detail_client;
book_detail_client.GetBook("1");
return 0;
}