Add RestSslClient; add User-Agent header.
parent
b02ea902c9
commit
d159ce46bf
@ -0,0 +1,10 @@
|
||||
add_executable(rest_github_client main.cc)
|
||||
|
||||
set(SSL_LIBS ${OPENSSL_LIBRARIES})
|
||||
if(WIN32)
|
||||
set(SSL_LIBS ${SSL_LIBS} crypt32)
|
||||
endif()
|
||||
|
||||
target_link_libraries(rest_github_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(rest_github_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
target_link_libraries(rest_github_client ${SSL_LIBS})
|
@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/rest_ssl_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
void Test() {
|
||||
webcc::RestSslClient client("api.github.com");
|
||||
|
||||
if (client.Get("/events")) {
|
||||
std::cout << client.response()->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(client.error());
|
||||
if (client.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
Test();
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
#ifndef WEBCC_BASIC_REST_CLIENT_H_
|
||||
#define WEBCC_BASIC_REST_CLIENT_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <utility> // for move()
|
||||
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_response.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
template <class HttpClientType>
|
||||
class BasicRestClient {
|
||||
public:
|
||||
// If |port| is empty, |host| will be checked to see if it contains port or
|
||||
// not (separated by ':').
|
||||
explicit BasicRestClient(const std::string& host,
|
||||
const std::string& port = "")
|
||||
: host_(host), port_(port) {
|
||||
if (port_.empty()) {
|
||||
std::size_t i = host_.find_last_of(':');
|
||||
if (i != std::string::npos) {
|
||||
port_ = host_.substr(i + 1);
|
||||
host_ = host_.substr(0, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~BasicRestClient() = default;
|
||||
|
||||
WEBCC_DELETE_COPY_ASSIGN(BasicRestClient);
|
||||
|
||||
void SetTimeout(int seconds) {
|
||||
http_client_.SetTimeout(seconds);
|
||||
}
|
||||
|
||||
// NOTE:
|
||||
// The return value of the following methods (Get, Post, etc.) only indicates
|
||||
// if the socket communication is successful or not. Check error() and
|
||||
// timed_out() for more information if it's failed. Check response_status()
|
||||
// instead for the HTTP status code.
|
||||
|
||||
// HTTP GET request.
|
||||
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, "");
|
||||
}
|
||||
|
||||
HttpResponsePtr response() const {
|
||||
return http_client_.response();
|
||||
}
|
||||
|
||||
int response_status() const {
|
||||
assert(response());
|
||||
return response()->status();
|
||||
}
|
||||
|
||||
const std::string& response_content() const {
|
||||
assert(response());
|
||||
return response()->content();
|
||||
}
|
||||
|
||||
bool timed_out() const {
|
||||
return http_client_.timed_out();
|
||||
}
|
||||
|
||||
Error error() const {
|
||||
return http_client_.error();
|
||||
}
|
||||
|
||||
private:
|
||||
bool Request(const std::string& method, const std::string& url,
|
||||
std::string&& content) {
|
||||
HttpRequest http_request;
|
||||
|
||||
http_request.set_method(method);
|
||||
http_request.set_url(url);
|
||||
http_request.set_host(host_, port_);
|
||||
|
||||
if (!content.empty()) {
|
||||
http_request.SetContent(std::move(content), true);
|
||||
http_request.SetContentType(kAppJsonUtf8);
|
||||
}
|
||||
|
||||
http_request.Make();
|
||||
|
||||
if (!http_client_.Request(http_request)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string host_;
|
||||
std::string port_;
|
||||
|
||||
HttpClientType http_client_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_BASIC_REST_CLIENT_H_
|
@ -1,40 +0,0 @@
|
||||
#include "webcc/rest_client.h"
|
||||
|
||||
#include "webcc/http_request.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
RestClient::RestClient(const std::string& host, const std::string& port)
|
||||
: host_(host), port_(port) {
|
||||
if (port_.empty()) {
|
||||
std::size_t i = host_.find_last_of(':');
|
||||
if (i != std::string::npos) {
|
||||
port_ = host_.substr(i + 1);
|
||||
host_ = host_.substr(0, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool RestClient::Request(const std::string& method, const std::string& url,
|
||||
std::string&& content) {
|
||||
HttpRequest http_request;
|
||||
|
||||
http_request.set_method(method);
|
||||
http_request.set_url(url);
|
||||
http_request.SetHost(host_, port_);
|
||||
|
||||
if (!content.empty()) {
|
||||
http_request.SetContent(std::move(content), true);
|
||||
http_request.SetContentType(kAppJsonUtf8);
|
||||
}
|
||||
|
||||
http_request.UpdateStartLine();
|
||||
|
||||
if (!http_client_.Request(http_request)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace webcc
|
@ -0,0 +1,13 @@
|
||||
#ifndef WEBCC_REST_SSL_CLIENT_H_
|
||||
#define WEBCC_REST_SSL_CLIENT_H_
|
||||
|
||||
#include "webcc/basic_rest_client.h"
|
||||
#include "webcc/http_ssl_client.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
typedef BasicRestClient<HttpSslClient> RestSslClient;
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_REST_SSL_CLIENT_H_
|
@ -0,0 +1,6 @@
|
||||
#ifndef WEBCC_VERSION_H_
|
||||
#define WEBCC_VERSION_H_
|
||||
|
||||
#define WEBCC_VERSION "0.1.0"
|
||||
|
||||
#endif // WEBCC_VERSION_H_
|
Loading…
Reference in New Issue