49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#ifndef _requester_H_
|
|
#define _requester_H_
|
|
|
|
#include <string>
|
|
#include <memory>
|
|
#include <curl/curl.h>
|
|
#include <libsclogging.hpp>
|
|
|
|
namespace sc {
|
|
|
|
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
|
|
requester(const requester&) = delete;
|
|
requester& operator=(const requester&) = delete;
|
|
|
|
// allow move
|
|
requester(requester&&);
|
|
requester& operator=(requester&&);
|
|
|
|
// 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;
|
|
long _status;
|
|
|
|
void perform();
|
|
void check_status(long status);
|
|
};
|
|
|
|
}
|
|
#endif // _requester_H_
|