diff --git a/lib/cppurl.hpp b/lib/cppurl.hpp index 6717581..438eb54 100644 --- a/lib/cppurl.hpp +++ b/lib/cppurl.hpp @@ -17,6 +17,7 @@ class Curl { string readBuffer; struct curl_slist *headers = NULL; string _useragent; + long _timeout = 0; public: @@ -37,6 +38,12 @@ class Curl { */ Curl& useragent(const string& useragent_); + /** + * Postavi vrijeme isteka zahtjeva + */ + + Curl& timeout(const long _timeout); + /** * Izvršiv HTTP GET zahtjev * Vraća string HTTP tjela diff --git a/src/cppurl.cpp b/src/cppurl.cpp index a4b6c91..d899c4a 100644 --- a/src/cppurl.cpp +++ b/src/cppurl.cpp @@ -26,6 +26,11 @@ Curl& marcelb::Curl::useragent(const string& useragent_) { return *this; } +Curl& marcelb::Curl::timeout(const long timeout_) { + _timeout = timeout_; +} + + string marcelb::Curl::get(const string& req){ curl = curl_easy_init(); @@ -39,6 +44,9 @@ string marcelb::Curl::get(const string& req){ if (!_useragent.empty()) { curl_easy_setopt(curl, CURLOPT_USERAGENT, _useragent.c_str()); } + if (_timeout > 0) { + curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS , _timeout); + } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); res = curl_easy_perform(curl); diff --git a/test/test b/test/test index 52392b6..3271eba 100755 Binary files a/test/test and b/test/test differ diff --git a/test/test.cpp b/test/test.cpp index e48c09d..8dd92eb 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,4 +1,8 @@ #include +#include +#include +#include +#include #include "../lib/cppurl.hpp" @@ -7,11 +11,55 @@ using namespace marcelb; int main () { - Curl rest; + // Curl rest; + // string header_value = "jebiga"; + // rest.header("API", header_value); + // cout << rest.get("https://reqres.in/api/users/2") << endl; - rest.header("Baba", "Janja").useragent("Dinio api client v1.1.0 - bitelex@bitelex.co"); - cout << rest.get("http://localhost:5000/?param1=tvt¶m2=2023") << endl; - // cout << rest.request("https://reqres.in/api/users/2") << endl; + // vector thr; + // for (uint i=0; i<4; i++) { + // thr.push_back(thread([](){ + // Curl rest; + // string header_value = "jebiga"; + // rest.header("API", header_value); + // cout << rest.get("https://reqres.in/api/users/2") << endl; + // })); + // } + + // for (uint i=0; i> debx_responses; + + for (uint i=0; i<4; i++) { + debx_responses.push_back(async(launch::async, [&](){ + Curl rest; + rest.timeout(600); + return rest.get("https://reqres.iin/api/users/2"); + })); + } + + for (uint i=0; i<4; i++) { + cout << debx_responses[i].get() << endl << endl; + } + } \ No newline at end of file