Skip to content

Commit d7db4cb

Browse files
committed
Add HttpRequest fluent builder
1 parent 6abb7cd commit d7db4cb

3 files changed

Lines changed: 14 additions & 15 deletions

File tree

src/s3cpp/httpclient.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#include <stdexcept>
66
#include <string>
77

8-
HttpResponse HttpRequest::execute() {
9-
return client_.execute(*this);
10-
}
8+
HttpResponse HttpRequest::execute() { return client_.execute(*this); }
119

1210
HttpResponse HttpClient::execute(HttpRequest &request) {
1311
if (!curl_handle) {

src/s3cpp/httpclient.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class HttpResponse {
1414
public:
1515
HttpResponse(int c, std::string b) : code_(c), body_(std::move(b)) {};
1616

17-
[[nodiscard]] int status() const { return code_; }
17+
int status() const { return code_; }
1818
const std::string &body() const { return body_; }
1919

2020
bool is_ok() const { return code_ >= 200 && code_ < 300; }
@@ -33,7 +33,7 @@ class HttpRequest {
3333
HttpRequest(HttpClient &client, std::string URL)
3434
: client_(client), URL_(std::move(URL)), timeout_(0) {};
3535

36-
[[nodiscard]] HttpRequest &header(const std::string &header,
36+
[[nodiscard]] HttpRequest &header(const std::string &&header,
3737
const std::string &value) {
3838
headers_[header] = value;
3939
return *this;
@@ -43,16 +43,16 @@ class HttpRequest {
4343
return *this;
4444
}
4545

46-
HttpResponse execute();
46+
HttpResponse execute();
4747

48-
std::string getURL() {return URL_;}
49-
int getTimeout() {return timeout_;}
48+
const std::string& getURL() const { return URL_; }
49+
const int getTimeout() const { return timeout_; }
5050

5151
private:
5252
HttpClient &client_;
5353
std::string URL_;
5454
std::unordered_map<std::string, std::string> headers_;
55-
int timeout_;
55+
int timeout_; // TODO(cristian): `std::chrono` ?
5656
};
5757

5858
// HttpClient should only focus on handling the cURL handle

test/httpclient_test.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ TEST(HTTP, HTTPBodyNonEmpty) {
5656
}
5757

5858
TEST(HTTP, HTTPHandleTimeout) {
59-
HttpClient client{};
59+
HttpClient client{};
6060
try {
61-
auto response = client.get("https://postman-echo.com/delay/10")
62-
.timeout(1)
63-
.execute();
64-
FAIL () << "Client not handling 1s timeout on a 10s delayed request";
61+
auto response =
62+
client.get("https://postman-echo.com/delay/10").timeout(1).execute();
63+
FAIL() << "Client not handling 1s timeout on a 10s delayed request";
6564
} catch (const std::exception &e) {
66-
SUCCEED(); // libcurl error for request: Timeout was reached
65+
SUCCEED(); // libcurl error for request: Timeout was reached
6766
}
6867
}
68+
69+
// TODO(cristian): Headers extensive tests

0 commit comments

Comments
 (0)