Finalizing and tested

dev v0.1
marcelb 11 months ago
parent dfda4cb292
commit 2a7dc27323
  1. 39
      lib/cppurl.hpp
  2. 41
      src/cppurl.cpp
  3. BIN
      test/test
  4. 6
      test/test.cpp

@ -3,6 +3,7 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <string> #include <string>
#include <map>
namespace marcelb { namespace marcelb {
@ -14,13 +15,43 @@ class Curl {
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
string readBuffer; string readBuffer;
struct curl_slist *headers = NULL;
string _useragent;
public: public:
// Curl();
string request(const string& req);
/**
* Postavi zaglavlje s ključem i vrijednošću
* Novi pozivi ne brišu stara zaglavlja, ponovljena se prepišu
*/
Curl& header(const string& key, const string& value);
/**
* Postavi zaglavlja iz mape
* Ponovan poziv prepisat će ona zaglavlja koja postoje
*/
Curl& header(const map<string, string> &_headers);
/**
* Postavi u zaglavlje User-Agent
*/
Curl& useragent(const string& useragent_);
/**
* Izvršiv HTTP GET zahtjev
* Vraća string HTTP tjela
*/
string get(const string& req);
/**
* Obriši spremljeno zaglavlje
*/
Curl& clearheader();
/**
* Obrši trenutnog User-Agent -a
*/
Curl& clearuseragent();
}; };

@ -3,17 +3,42 @@
using namespace marcelb; using namespace marcelb;
// marcelb::Curl::Curl() { static size_t marcelb::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
// } ((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
Curl& marcelb::Curl::header(const string& key, const string& value) {
headers = curl_slist_append(headers, string(key + ": " + value).c_str());
return *this;
}
Curl& marcelb::Curl::header(const map<string, string> &_headers) {
for (auto h : _headers) {
header(h.first, h.second);
}
string marcelb::Curl::request(const string& req){ return *this;
}
Curl& marcelb::Curl::useragent(const string& useragent_) {
_useragent = useragent_;
return *this;
}
string marcelb::Curl::get(const string& req){
curl = curl_easy_init(); curl = curl_easy_init();
readBuffer.clear(); readBuffer.clear();
if(curl) { if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, req.c_str()); curl_easy_setopt(curl, CURLOPT_URL, req.c_str());
if (headers != NULL) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
if (!_useragent.empty()) {
curl_easy_setopt(curl, CURLOPT_USERAGENT, _useragent.c_str());
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
@ -23,8 +48,12 @@ string marcelb::Curl::request(const string& req){
return readBuffer; return readBuffer;
} }
Curl& marcelb::Curl::clearheader() {
headers = NULL;
return *this;
}
static size_t marcelb::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { Curl& marcelb::Curl::clearuseragent() {
((std::string*)userp)->append((char*)contents, size * nmemb); _useragent.clear();
return size * nmemb; return *this;
} }

Binary file not shown.

@ -9,9 +9,9 @@ int main () {
Curl rest; Curl rest;
cout << rest.request("https://reqres.in/api/unknown/2") << endl; rest.header("Baba", "Janja").useragent("Dinio api client v1.1.0 - bitelex@bitelex.co");
cout << rest.get("http://localhost:5000/?param1=tvt&param2=2023") << endl;
cout << rest.request("https://reqres.in/api/users/2") << endl; // cout << rest.request("https://reqres.in/api/users/2") << endl;
} }
Loading…
Cancel
Save