libscrequest/src/requester.hpp

49 lines
1.3 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>
2023-11-05 00:51:47 +00:00
namespace sc {
2023-11-05 00:41:17 +00:00
2023-11-05 00:51:47 +00:00
class requester {
public:
// callback for data receiving
static size_t write_data(char* buf, size_t sz, size_t nmemb, void* user_data);
2023-11-05 00:41:17 +00:00
2023-11-05 00:51:47 +00:00
// this class is a RAII class for a curl handle
requester();
~requester();
2023-11-05 00:41:17 +00:00
2023-11-05 17:12:30 +00:00
// forbid copying
2023-11-05 00:51:47 +00:00
requester(const requester&) = delete;
requester& operator=(const requester&) = delete;
2023-11-05 17:12:30 +00:00
// allow move
requester(requester&&);
requester& operator=(requester&&);
2023-11-05 00:41:17 +00:00
2023-11-05 00:51:47 +00:00
// optional logger
void logger(sc::logger* logger) { _logger = logger; }
2023-11-05 00:41:17 +00:00
2023-11-05 00:51:47 +00:00
// perform a http get request
std::string get(const std::string& url);
2023-11-05 00:41:17 +00:00
2023-11-05 00:51:47 +00:00
// perform a http post request
std::string post(const std::string& url, const std::string& json);
2023-11-05 00:41:17 +00:00
2023-11-05 00:51:47 +00:00
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);
2023-11-05 00:51:47 +00:00
};
}
2023-11-05 00:41:17 +00:00
#endif // _requester_H_