libscrequest/src/requester.hpp

40 lines
1.1 KiB
C++
Raw Normal View History

2023-11-05 00:41:17 +00:00
#ifndef _requester_H_
#define _requester_H_
#include <string>
#include <memory>
#include <curl/curl.h>
#include <libsclogging.hpp>
class requester {
public:
// callback for data receiving
static size_t write_data(char* buf, size_t sz, size_t nmemb, void* user_data);
// this class is a RAII class for a curl handle
requester();
~requester();
// forbid copying and moving
requester(const requester&) = delete;
requester& operator=(const requester&) = delete;
requester(requester&&) = delete;
requester& operator=(requester&&) = delete;
// optional logger
void logger(sc::logger* logger) { _logger = logger; }
// perform a http get request
std::string get(const std::string& url);
// perform a http post request
std::string post(const std::string& url, const std::string& json);
private:
std::unique_ptr<CURL, void(*)(CURL*)> _h {nullptr, curl_easy_cleanup};
sc::logger* _logger {nullptr};
std::string _text;
};
#endif // _requester_H_