Fix URL encoding issues; remove request shortcuts.
This commit is contained in:
@@ -14,22 +14,14 @@ int main() {
|
||||
try {
|
||||
r = session.Request(webcc::RequestBuilder{}.
|
||||
Get("http://httpbin.org/get").
|
||||
Query("key1", "value1").
|
||||
Query("key2", "value2").
|
||||
Date().
|
||||
Header("Accept", "application/json")
|
||||
Query("name", "Adam Gu", /*encode*/true).
|
||||
Header("Accept", "application/json").
|
||||
Date()
|
||||
());
|
||||
|
||||
assert(r->status() == webcc::Status::kOK);
|
||||
assert(!r->data().empty());
|
||||
|
||||
r = session.Get("http://httpbin.org/get",
|
||||
{ "key1", "value1", "key2", "value2" },
|
||||
{ "Accept", "application/json" });
|
||||
|
||||
assert(r->status() == webcc::Status::kOK);
|
||||
assert(!r->data().empty());
|
||||
|
||||
r = session.Request(webcc::RequestBuilder{}.
|
||||
Post("http://httpbin.org/post").
|
||||
Body("{'name'='Adam', 'age'=20}").
|
||||
@@ -41,7 +33,9 @@ int main() {
|
||||
|
||||
#if WEBCC_ENABLE_SSL
|
||||
|
||||
r = session.Get("https://httpbin.org/get");
|
||||
r = session.Request(webcc::RequestBuilder{}.
|
||||
Get("https://httpbin.org/get")
|
||||
());
|
||||
|
||||
assert(r->status() == webcc::Status::kOK);
|
||||
assert(!r->data().empty());
|
||||
@@ -51,9 +45,6 @@ int main() {
|
||||
} catch (const webcc::Error& error) {
|
||||
std::cerr << error << std::endl;
|
||||
return 1;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -55,7 +55,9 @@ void PrettyPrintJsonString(const std::string& str) {
|
||||
// List public events.
|
||||
void ListEvents(webcc::ClientSession& session) {
|
||||
try {
|
||||
auto r = session.Get(kUrlRoot + "/events");
|
||||
auto r = session.Request(webcc::RequestBuilder{}.
|
||||
Get(kUrlRoot).Path("events")
|
||||
());
|
||||
|
||||
PRINT_JSON_STRING(r->data());
|
||||
|
||||
@@ -66,10 +68,13 @@ void ListEvents(webcc::ClientSession& session) {
|
||||
|
||||
// List the followers of the given user.
|
||||
// Example:
|
||||
// ListUserFollowers(session, "<login>")
|
||||
// ListUserFollowers(session, "<user>")
|
||||
void ListUserFollowers(webcc::ClientSession& session, const std::string& user) {
|
||||
try {
|
||||
auto r = session.Get(kUrlRoot + "/users/" + user + "/followers");
|
||||
auto r = session.Request(webcc::RequestBuilder{}.
|
||||
Get(kUrlRoot).Path("users").Path(user).
|
||||
Path("followers")
|
||||
());
|
||||
|
||||
PRINT_JSON_STRING(r->data());
|
||||
|
||||
@@ -86,7 +91,7 @@ void ListAuthUserFollowers(webcc::ClientSession& session,
|
||||
const std::string& password) {
|
||||
try {
|
||||
auto r = session.Request(webcc::RequestBuilder{}.
|
||||
Get(kUrlRoot + "/user/followers").
|
||||
Get(kUrlRoot).Path("user/followers").
|
||||
AuthBasic(login, password)
|
||||
());
|
||||
|
||||
@@ -108,7 +113,7 @@ void CreateAuthorization(webcc::ClientSession& session,
|
||||
"}";
|
||||
|
||||
auto r = session.Request(webcc::RequestBuilder{}.
|
||||
Post(kUrlRoot + "/authorizations").
|
||||
Post(kUrlRoot).Path("authorizations").
|
||||
Body(std::move(data)).
|
||||
Json().Utf8().
|
||||
AuthBasic(login, password)
|
||||
@@ -130,5 +135,8 @@ int main() {
|
||||
|
||||
ListEvents(session);
|
||||
|
||||
//ListUserFollowers(session, "sprinfall");
|
||||
//ListAuthUserFollowers(session, "sprinfall@gmail.com", "<password>");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ BookClient::BookClient(const std::string& url, int timeout)
|
||||
|
||||
bool BookClient::ListBooks(std::list<Book>* books) {
|
||||
try {
|
||||
auto r = session_.Get(url_ + "/books");
|
||||
auto r = session_.Request(WEBCC_GET(url_).Path("books")());
|
||||
|
||||
if (!CheckStatus(r, webcc::Status::kOK)) {
|
||||
// Response HTTP status error.
|
||||
@@ -89,7 +89,9 @@ bool BookClient::CreateBook(const std::string& title, double price,
|
||||
req_json["price"] = price;
|
||||
|
||||
try {
|
||||
auto r = session_.Post(url_ + "/books", JsonToString(req_json), true);
|
||||
auto r = session_.Request(WEBCC_POST(url_).Path("books").
|
||||
Body(JsonToString(req_json))
|
||||
());
|
||||
|
||||
if (!CheckStatus(r, webcc::Status::kCreated)) {
|
||||
return false;
|
||||
@@ -108,7 +110,7 @@ bool BookClient::CreateBook(const std::string& title, double price,
|
||||
|
||||
bool BookClient::GetBook(const std::string& id, Book* book) {
|
||||
try {
|
||||
auto r = session_.Get(url_ + "/books/" + id);
|
||||
auto r = session_.Request(WEBCC_GET(url_).Path("books").Path(id)());
|
||||
|
||||
if (!CheckStatus(r, webcc::Status::kOK)) {
|
||||
return false;
|
||||
@@ -129,7 +131,9 @@ bool BookClient::UpdateBook(const std::string& id, const std::string& title,
|
||||
json["price"] = price;
|
||||
|
||||
try {
|
||||
auto r = session_.Put(url_ + "/books/" + id, JsonToString(json), true);
|
||||
auto r = session_.Request(WEBCC_PUT(url_).Path("books").Path(id).
|
||||
Body(JsonToString(json))
|
||||
());
|
||||
|
||||
if (!CheckStatus(r, webcc::Status::kOK)) {
|
||||
return false;
|
||||
@@ -145,7 +149,7 @@ bool BookClient::UpdateBook(const std::string& id, const std::string& title,
|
||||
|
||||
bool BookClient::DeleteBook(const std::string& id) {
|
||||
try {
|
||||
auto r = session_.Delete(url_ + "/books/" + id);
|
||||
auto r = session_.Request(WEBCC_DELETE(url_).Path("books").Path(id)());
|
||||
|
||||
if (!CheckStatus(r, webcc::Status::kOK)) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user