75 lines
1.8 KiB
C++
75 lines
1.8 KiB
C++
#ifndef CURL_DL_H_
|
|
#define CURL_DL_H_
|
|
|
|
#include <chrono>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "json.hpp"
|
|
|
|
class CURL_DL
|
|
{
|
|
public:
|
|
class RateLimit
|
|
{
|
|
friend class CURL_DL;
|
|
private:
|
|
std::string m_name;
|
|
uint32_t m_ms;
|
|
|
|
public:
|
|
RateLimit(std::string name = "", uint32_t ms = 0)
|
|
: m_name(name), m_ms(ms)
|
|
{}
|
|
};
|
|
|
|
private:
|
|
CURL *m_handle;
|
|
|
|
std::map<std::string, std::chrono::time_point<std::chrono::steady_clock>> m_limits;
|
|
|
|
char m_error[CURL_ERROR_SIZE];
|
|
|
|
void open_curl();
|
|
|
|
void check_rate_limit(const RateLimit& limit);
|
|
void save_rate_limit(const RateLimit& limit);
|
|
|
|
CURL_DL();
|
|
CURL_DL(const CURL_DL&) = delete;
|
|
CURL_DL(CURL_DL&&) = delete;
|
|
|
|
public:
|
|
~CURL_DL();
|
|
static CURL_DL& get_handle();
|
|
|
|
bool download(const std::string& url, std::ostream* out,
|
|
RateLimit limit = RateLimit());
|
|
bool download(const std::string& url, std::ostream* out,
|
|
const std::vector<std::string> *headers, RateLimit limit = RateLimit());
|
|
bool download(const std::string& url, std::ostream* out,
|
|
const std::vector<std::string> *headers,
|
|
std::map<std::string, std::string> *out_headers,
|
|
RateLimit limit = RateLimit());
|
|
bool download(const std::string& url, std::ostream* out,
|
|
const std::vector<std::string> *headers,
|
|
std::map<std::string, std::string> *out_headers,
|
|
const std::map<std::string, std::string> *params,
|
|
RateLimit limit = RateLimit());
|
|
bool post_json(const std::string& url, nlohmann::json& j, std::ostream* out,
|
|
RateLimit limit = RateLimit());
|
|
bool post_json(const std::string& url, nlohmann::json& j, std::ostream* out,
|
|
const std::vector<std::string> *headers, RateLimit limit = RateLimit());
|
|
|
|
std::string get_error() const;
|
|
|
|
static std::string url_encode(const std::string& input);
|
|
static std::string calc_redir(std::string url, const std::string& location);
|
|
};
|
|
|
|
#endif // CURL_DL_H_
|