You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.7 KiB
C
82 lines
1.7 KiB
C
![]()
7 years ago
|
#ifndef WEBCC_REST_CLIENT_H_
|
||
|
#define WEBCC_REST_CLIENT_H_
|
||
|
|
||
![]()
7 years ago
|
#include <cassert>
|
||
![]()
7 years ago
|
#include <string>
|
||
|
|
||
|
#include "webcc/globals.h"
|
||
![]()
7 years ago
|
#include "webcc/http_response.h"
|
||
![]()
7 years ago
|
|
||
|
namespace webcc {
|
||
|
|
||
|
class RestClient {
|
||
![]()
7 years ago
|
public:
|
||
![]()
7 years ago
|
RestClient(const std::string& host, const std::string& port);
|
||
|
|
||
|
~RestClient() = default;
|
||
|
|
||
|
DELETE_COPY_AND_ASSIGN(RestClient);
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
void set_timeout_seconds(int timeout_seconds) {
|
||
|
timeout_seconds_ = timeout_seconds;
|
||
|
}
|
||
|
|
||
|
HttpResponsePtr response() const { return response_; }
|
||
![]()
7 years ago
|
|
||
|
int response_status() const {
|
||
|
assert(response_);
|
||
|
return response_->status();
|
||
|
}
|
||
|
|
||
|
const std::string& response_content() const {
|
||
|
assert(response_);
|
||
|
return response_->content();
|
||
|
}
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
bool timed_out() const { return timed_out_; }
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
Error error() const { return error_; }
|
||
|
|
||
|
inline bool Get(const std::string& url) {
|
||
![]()
7 years ago
|
return Request(kHttpGet, url, "");
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
inline bool Post(const std::string& url, const std::string& content) {
|
||
![]()
7 years ago
|
return Request(kHttpPost, url, content);
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
inline bool Put(const std::string& url, const std::string& content) {
|
||
![]()
7 years ago
|
return Request(kHttpPut, url, content);
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
inline bool Patch(const std::string& url, const std::string& content) {
|
||
![]()
7 years ago
|
return Request(kHttpPatch, url, content);
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
inline bool Delete(const std::string& url) {
|
||
![]()
7 years ago
|
return Request(kHttpDelete, url, "");
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
private:
|
||
![]()
7 years ago
|
bool Request(const std::string& method,
|
||
|
const std::string& url,
|
||
![]()
7 years ago
|
const std::string& content);
|
||
![]()
7 years ago
|
|
||
|
std::string host_;
|
||
|
std::string port_;
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
// Timeout in seconds; only effective when > 0.
|
||
![]()
7 years ago
|
int timeout_seconds_;
|
||
![]()
7 years ago
|
|
||
|
HttpResponsePtr response_;
|
||
|
|
||
![]()
7 years ago
|
// If the error was caused by timeout or not.
|
||
![]()
7 years ago
|
bool timed_out_;
|
||
|
|
||
|
Error error_;
|
||
![]()
7 years ago
|
};
|
||
|
|
||
|
} // namespace webcc
|
||
|
|
||
|
#endif // WEBCC_REST_CLIENT_H_
|