99
1010#include < curl/curl.h>
1111
12-
1312using namespace std ;
1413
1514size_t write_callback (void * contents,
@@ -22,43 +21,41 @@ size_t write_callback(void* contents,
2221
2322struct FetchResult {
2423 std::string body;
25-
26- FetchResult (std::string b):body(std::move(b)){
27- }
28-
24+ FetchResult () = default ;
25+ FetchResult (std::string b) : body(std::move(b)) {}
2926};
3027
3128struct FetchError {
3229 std::string message;
3330 std::string body;
3431 long httpCode = 0 ;
3532
36- FetchError (std::string msg, std::string b,long code): message(std::move(msg)), body(std::move(b)), httpCode(code)
37- {
38- }
39- };
40-
33+ FetchError () = default ;
4134
35+ FetchError (std::string msg, std::string b, long code)
36+ : message(std::move(msg)), body(std::move(b)), httpCode(code) {}
37+ };
4238
43- inline std::variant<FetchResult,FetchError> fetch (const char * url, std::unordered_map<std::string,std::string>&& headers) {
39+ inline std::variant<FetchResult, FetchError> fetch (
40+ const char * url,
41+ std::unordered_map<std::string, std::string>&& headers) {
4442 curl_global_init (CURL_GLOBAL_DEFAULT);
4543 CURL* curl = curl_easy_init ();
4644
47- auto result = std::variant<FetchResult,FetchError>();
45+ auto result = std::variant<FetchResult, FetchError>();
4846
4947 std::string response;
5048
5149 if (curl) {
52- curl_easy_setopt (curl, CURLOPT_URL,
53- url);
50+ curl_easy_setopt (curl, CURLOPT_URL, url);
5451 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_callback);
5552 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &response);
5653
57- if (headers.size ()){
54+ if (headers.size ()) {
5855 struct curl_slist * curl_headers = nullptr ;
5956
60- for (auto & it: headers){
61- std::string h = it.first + " :" + it.second ;
57+ for (auto & it : headers) {
58+ std::string h = it.first + " :" + it.second ;
6259 curl_headers = curl_slist_append (curl_headers, h.c_str ());
6360 }
6461
@@ -70,11 +67,10 @@ inline std::variant<FetchResult,FetchError> fetch(const char* url, std::unordere
7067 long httpCode = 0 ;
7168 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpCode);
7269
73-
7470 if (res == CURLE_OK && httpCode == 200 ) {
7571 result = FetchResult (response);
7672 } else {
77- result = FetchError (curl_easy_strerror (res),response,httpCode);
73+ result = FetchError (curl_easy_strerror (res), response, httpCode);
7874 }
7975
8076 curl_easy_cleanup (curl);
0 commit comments