66 lines
1.4 KiB
C
66 lines
1.4 KiB
C
|
#pragma once
|
||
|
|
||
|
#include <mutex>
|
||
|
#include <optional>
|
||
|
#include <exception>
|
||
|
#include <pthread.h>
|
||
|
|
||
|
#include <curl/curl.h>
|
||
|
struct Account; // forward declaration from models.h
|
||
|
|
||
|
class CurlException : public std::exception {
|
||
|
public:
|
||
|
CurlException(CURLcode code_) : code(code_) {}
|
||
|
|
||
|
const char* what() const noexcept {
|
||
|
return curl_easy_strerror(this->code);
|
||
|
}
|
||
|
|
||
|
CURLcode code;
|
||
|
};
|
||
|
|
||
|
class CurlShareException : public std::exception {
|
||
|
public:
|
||
|
CurlShareException(CURLSHcode code_) : code(code_) {}
|
||
|
|
||
|
const char* what() const noexcept {
|
||
|
return curl_share_strerror(this->code);
|
||
|
}
|
||
|
|
||
|
CURLSHcode code;
|
||
|
};
|
||
|
|
||
|
class MastodonClient {
|
||
|
public:
|
||
|
MastodonClient(const MastodonClient&&) = delete;
|
||
|
MastodonClient& operator=(const MastodonClient&&) = delete;
|
||
|
|
||
|
MastodonClient();
|
||
|
~MastodonClient();
|
||
|
|
||
|
static void init() {
|
||
|
CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
|
||
|
if (code) {
|
||
|
throw CurlException(code);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void cleanup() {
|
||
|
curl_global_cleanup();
|
||
|
}
|
||
|
|
||
|
std::optional<Account> get_account_by_username(const std::string& host, const std::string& username);
|
||
|
|
||
|
private:
|
||
|
CURL* _get_easy();
|
||
|
std::string _send_request(const std::string& url);
|
||
|
long _response_status_code();
|
||
|
|
||
|
std::mutex _share_locks[CURL_LOCK_DATA_LAST];
|
||
|
|
||
|
CURLSH* _share = nullptr;
|
||
|
pthread_key_t _easy_key;
|
||
|
};
|
||
|
|
||
|
extern MastodonClient mastodon_client;
|