11#include < curl/curl.h>
22#include < curl/easy.h>
3+ #include < expected>
34#include < format>
45#include < s3cpp/httpclient.h>
56#include < stdexcept>
67#include < string>
78
89// Route to its HttpMethod
9- HttpResponse HttpRequest::execute () {
10+ std::expected< HttpResponse, std::string> HttpRequest::execute () {
1011 switch (this ->http_method_ ) {
1112 case HttpMethod::Get:
1213 return client_.execute_get (*this );
1314 case HttpMethod::Head:
1415 return client_.execute_head (*this );
1516 default :
16- throw std::runtime_error ( std::format (" No matching enum Http Method" ) );
17+ return std::unexpected< std::string> (" No matching enum Http Method" );
1718 }
1819}
1920
20- HttpResponse HttpBodyRequest::execute () {
21+ std::expected< HttpResponse, std::string> HttpBodyRequest::execute () {
2122 switch (this ->http_method_ ) {
2223 case HttpMethod::Post:
2324 return client_.execute_post (*this );
@@ -26,16 +27,15 @@ HttpResponse HttpBodyRequest::execute() {
2627 case HttpMethod::Delete:
2728 return client_.execute_delete (*this );
2829 default :
29- throw std::runtime_error ( std::format (" No matching enum Http Method" ) );
30+ return std::unexpected< std::string> (" No matching enum Http Method" );
3031 }
3132}
3233
33- HttpResponse HttpClient::execute_get (HttpRequest& request) {
34+ std::expected< HttpResponse, std::string> HttpClient::execute_get (HttpRequest& request) {
3435 if (!curl_handle) {
35- throw std::runtime_error (
36- // this can happen both when cURL handle is not initialized or when it
37- // is invalidated in the HTTPClient copy constructor
38- " cURL handle is invalid" );
36+ // this can happen both when cURL handle is not initialized or when it
37+ // is invalidated in the HTTPClient copy constructor
38+ return std::unexpected<std::string>(" cURL handle is invalid" );
3939 }
4040 std::string body_buf;
4141 std::map<std::string, std::string, LowerCaseCompare> headers_buf;
@@ -74,8 +74,7 @@ HttpResponse HttpClient::execute_get(HttpRequest& request) {
7474 CURLcode code = curl_easy_perform (curl_handle);
7575 curl_slist_free_all (list);
7676 if (code != CURLE_OK) {
77- throw std::runtime_error (
78- std::format (" libcurl error: {}" , curl_easy_strerror (code)));
77+ return std::unexpected<std::string>(std::format (" libcurl error: {}" , curl_easy_strerror (code)));
7978 }
8079
8180 // get HTTP code
@@ -86,12 +85,11 @@ HttpResponse HttpClient::execute_get(HttpRequest& request) {
8685 std::move (headers_buf));
8786}
8887
89- HttpResponse HttpClient::execute_head (HttpRequest& request) {
88+ std::expected< HttpResponse, std::string> HttpClient::execute_head (HttpRequest& request) {
9089 if (!curl_handle) {
91- throw std::runtime_error (
92- // this can happen both when cURL handle is not initialized or when it
93- // is invalidated in the HTTPClient copy constructor
94- " cURL handle is invalid" );
90+ // this can happen both when cURL handle is not initialized or when it
91+ // is invalidated in the HTTPClient copy constructor
92+ return std::unexpected<std::string>(" cURL handle is invalid" );
9593 }
9694 std::map<std::string, std::string, LowerCaseCompare> headers_buf;
9795 std::string error_buf;
@@ -117,8 +115,7 @@ HttpResponse HttpClient::execute_head(HttpRequest& request) {
117115 CURLcode code = curl_easy_perform (curl_handle);
118116 curl_slist_free_all (list);
119117 if (code != CURLE_OK) {
120- throw std::runtime_error (
121- std::format (" libcurl error for request: {}" , curl_easy_strerror (code)));
118+ return std::unexpected<std::string>(std::format (" libcurl error: {}" , curl_easy_strerror (code)));
122119 }
123120
124121 // get HTTP code
@@ -128,12 +125,11 @@ HttpResponse HttpClient::execute_head(HttpRequest& request) {
128125 return HttpResponse (static_cast <int >(response_code), std::move (headers_buf));
129126}
130127
131- HttpResponse HttpClient::execute_post (HttpBodyRequest& request) {
128+ std::expected< HttpResponse, std::string> HttpClient::execute_post (HttpBodyRequest& request) {
132129 if (!curl_handle) {
133- throw std::runtime_error (
134- // this can happen both when cURL handle is not initialized or when it
135- // is invalidated in the HTTPClient copy constructor
136- " cURL handle is invalid" );
130+ // this can happen both when cURL handle is not initialized or when it
131+ // is invalidated in the HTTPClient copy constructor
132+ return std::unexpected<std::string>(" cURL handle is invalid" );
137133 }
138134 std::string body_buf;
139135 std::map<std::string, std::string, LowerCaseCompare> headers_buf;
@@ -178,8 +174,7 @@ HttpResponse HttpClient::execute_post(HttpBodyRequest& request) {
178174 CURLcode code = curl_easy_perform (curl_handle);
179175 curl_slist_free_all (list);
180176 if (code != CURLE_OK) {
181- throw std::runtime_error (
182- std::format (" libcurl error for request: {}" , curl_easy_strerror (code)));
177+ return std::unexpected<std::string>(std::format (" libcurl error: {}" , curl_easy_strerror (code)));
183178 }
184179
185180 // get HTTP code
@@ -190,12 +185,11 @@ HttpResponse HttpClient::execute_post(HttpBodyRequest& request) {
190185 std::move (headers_buf));
191186}
192187
193- HttpResponse HttpClient::execute_delete (HttpBodyRequest& request) {
188+ std::expected< HttpResponse, std::string> HttpClient::execute_delete (HttpBodyRequest& request) {
194189 if (!curl_handle) {
195- throw std::runtime_error (
196- // this can happen both when cURL handle is not initialized or when it
197- // is invalidated in the HTTPClient copy constructor
198- " cURL handle is invalid" );
190+ // this can happen both when cURL handle is not initialized or when it
191+ // is invalidated in the HTTPClient copy constructor
192+ return std::unexpected<std::string>(" cURL handle is invalid" );
199193 }
200194 std::string body_buf;
201195 std::map<std::string, std::string, LowerCaseCompare> headers_buf;
@@ -235,8 +229,7 @@ HttpResponse HttpClient::execute_delete(HttpBodyRequest& request) {
235229 CURLcode code = curl_easy_perform (curl_handle);
236230 curl_slist_free_all (list);
237231 if (code != CURLE_OK) {
238- throw std::runtime_error (
239- std::format (" libcurl error for request: {}" , curl_easy_strerror (code)));
232+ return std::unexpected<std::string>(std::format (" libcurl error: {}" , curl_easy_strerror (code)));
240233 }
241234
242235 // get HTTP code
0 commit comments