diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..517e9ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +example diff --git a/LICENSE b/LICENSE index 137069b..c06f6b3 100644 --- a/LICENSE +++ b/LICENSE @@ -58,7 +58,7 @@ APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright [yyyy] [name of copyright owner] +Copyright [2023] [Marcel Bandić - marcelb96@yahoo.com] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/dependency b/dependency new file mode 100644 index 0000000..18ae19f --- /dev/null +++ b/dependency @@ -0,0 +1 @@ +libcurl4-openssl-dev \ No newline at end of file diff --git a/lib/cppurl.hpp b/lib/cppurl.hpp new file mode 100644 index 0000000..a81da5f --- /dev/null +++ b/lib/cppurl.hpp @@ -0,0 +1,30 @@ +#ifndef _CPPURL_ +#define _CPPURL_ + +#include +#include + +namespace marcelb { + +using namespace std; + +static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp); + +class Curl { + CURL *curl; + CURLcode res; + string readBuffer; + + + + public: + // Curl(); + string request(const string& req); + + +}; + + +} + +#endif diff --git a/src/cppurl.cpp b/src/cppurl.cpp new file mode 100644 index 0000000..7e784cc --- /dev/null +++ b/src/cppurl.cpp @@ -0,0 +1,30 @@ + +#include "../lib/cppurl.hpp" + +using namespace marcelb; + +// marcelb::Curl::Curl() { +// } + + +string marcelb::Curl::request(const string& req){ + curl = curl_easy_init(); + + readBuffer.clear(); + + if(curl) { + curl_easy_setopt(curl, CURLOPT_URL, req.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + } + + return readBuffer; +} + + +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; +} \ No newline at end of file diff --git a/test/compile.sh b/test/compile.sh new file mode 100644 index 0000000..3df2b75 --- /dev/null +++ b/test/compile.sh @@ -0,0 +1 @@ +g++ test.cpp ../src/*.cpp -o test -lcurl \ No newline at end of file diff --git a/test/test b/test/test new file mode 100755 index 0000000..d0b68c1 Binary files /dev/null and b/test/test differ diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..41f058d --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,17 @@ +#include + +#include "../lib/cppurl.hpp" + +using namespace std; +using namespace marcelb; + +int main () { + + Curl rest; + + cout << rest.request("https://reqres.in/api/unknown/2") << endl; + + cout << rest.request("https://reqres.in/api/users/2") << endl; + + +} \ No newline at end of file