Breaking changes on the client, push only for keeping the history.

This commit is contained in:
Chunting Gu
2019-03-01 09:29:03 +08:00
parent d42556ce5a
commit b38b7e6d38
12 changed files with 263 additions and 129 deletions
+72 -46
View File
@@ -1,13 +1,27 @@
#include <iostream>
#include <map>
#include "json/json.h"
#include "webcc/logger.h"
#include "webcc/rest_ssl_client.h"
const bool kSslVerify = false;
// -----------------------------------------------------------------------------
#define PRINT_CONTENT 0
// Change to 1 to print response JSON.
#define PRINT_RESPONSE 0
#if (defined(WIN32) || defined(_WIN64))
// You need to set environment variable SSL_CERT_FILE properly to enable
// SSL verification.
bool kSslVerify = false;
#else
bool kSslVerify = true;
#endif
const std::string kGithubHost = "api.github.com";
// -----------------------------------------------------------------------------
static Json::Value StringToJson(const std::string& str) {
Json::Value json;
@@ -22,64 +36,76 @@ static Json::Value StringToJson(const std::string& str) {
return json;
}
void ListPublicEvents() {
webcc::RestSslClient client("api.github.com", "", kSslVerify);
// Print the JSON string in pretty format.
static void PrettyPrintJsonString(const std::string& str) {
Json::Value json = StringToJson(str);
Json::StreamWriterBuilder builder;
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &std::cout);
std::cout << std::endl;
}
// -----------------------------------------------------------------------------
#if PRINT_RESPONSE
#define PRINT_JSON_STRING(str) PrettyPrintJsonString(str)
#else
#define PRINT_JSON_STRING(str)
#endif // PRINT_RESPONSE
static void PrintError(const webcc::RestSslClient& client) {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
std::cout << " (timed out)";
}
std::cout << std::endl;
}
// -----------------------------------------------------------------------------
// List public events.
static void ListEvents(webcc::RestSslClient& client) {
if (client.Get("/events")) {
#if PRINT_CONTENT
Json::Value json = StringToJson(client.response()->content());
// Pretty print the JSON.
Json::StreamWriterBuilder builder;
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &std::cout);
std::cout << std::endl;
#endif // PRINT_CONTENT
PRINT_JSON_STRING(client.response_content());
} else {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
std::cout << " (timed out)";
}
std::cout << std::endl;
PrintError(client);
}
}
void ListUserFollowers() {
webcc::RestSslClient client("api.github.com", "", kSslVerify);
if (client.Get("/users/sprinfall/followers")) {
#if PRINT_CONTENT
Json::Value json = StringToJson(client.response()->content());
// Pretty print the JSON.
Json::StreamWriterBuilder builder;
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &std::cout);
std::cout << std::endl;
#endif // PRINT_CONTENT
// List the followers of the given user.
static void ListUserFollowers(webcc::RestSslClient& client,
const std::string& user) {
if (client.Get("/users/" + user + "/followers")) {
PRINT_JSON_STRING(client.response_content());
} else {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
std::cout << " (timed out)";
}
std::cout << std::endl;
PrintError(client);
}
}
// List the followers of the current authorized user.
// Header syntax: Authorization: <type> <credentials>
static void ListAuthorizedUserFollowers(webcc::RestSslClient& client,
const std::string& auth) {
if (client.Get("/user/followers", { { "Authorization", auth } })) {
PRINT_JSON_STRING(client.response_content());
} else {
PrintError(client);
}
}
// -----------------------------------------------------------------------------
int main() {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
//ListPublicEvents();
webcc::RestSslClient client(kGithubHost, "", kSslVerify, {}, 1500);
ListUserFollowers();
//ListAuthorizedUserFollowers(client, "Basic c3ByaW5mYWxsQGdtYWlsLmNvbTpYaWFvTHVhbjFA");
ListAuthorizedUserFollowers(client, "Token 1d42e2cce49929f2d24b1b6e96260003e5b3e1b0");
return 0;
}