2023-11-22 08:39:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <exception>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <curl/curl.h>
|
2023-11-24 07:11:05 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2023-11-23 08:49:27 +00:00
|
|
|
|
|
|
|
#include "models.h"
|
2023-11-29 11:36:52 +00:00
|
|
|
class CurlUrl; // forward declaration from curlu_wrapper.h
|
2023-11-22 08:39:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-11-24 07:11:05 +00:00
|
|
|
class MastodonException : public std::exception {
|
|
|
|
public:
|
|
|
|
MastodonException(long response_code_, std::string error) : response_code(response_code_), _error(std::move(error)) {}
|
|
|
|
|
|
|
|
const char* what() const noexcept {
|
|
|
|
return this->_error.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
long response_code;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _error;
|
|
|
|
};
|
|
|
|
|
2023-11-22 08:39:24 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-11-25 03:39:35 +00:00
|
|
|
std::optional<Account> get_account_by_username(std::string host, std::string username);
|
|
|
|
std::vector<Post> get_pinned_posts(std::string host, const std::string& account_id);
|
2023-11-23 08:49:27 +00:00
|
|
|
std::vector<Post> get_posts(const std::string& host, const std::string& account_id, PostSortingMethod sorting_method, std::optional<std::string> max_id);
|
2023-11-22 08:39:24 +00:00
|
|
|
|
2023-11-29 04:31:26 +00:00
|
|
|
std::optional<Post> get_post(const std::string& host, std::string id);
|
|
|
|
PostContext get_post_context(const std::string& host, std::string id);
|
2023-11-23 23:46:47 +00:00
|
|
|
|
2023-11-24 11:43:53 +00:00
|
|
|
std::vector<Post> get_tag_timeline(const std::string& host, const std::string& tag, std::optional<std::string> max_id);
|
|
|
|
|
2023-11-25 03:39:35 +00:00
|
|
|
Instance get_instance(std::string host);
|
|
|
|
blankie::html::HTMLString get_extended_description(std::string host);
|
2023-11-24 12:46:00 +00:00
|
|
|
|
2023-11-22 08:39:24 +00:00
|
|
|
private:
|
|
|
|
CURL* _get_easy();
|
2023-11-29 11:36:52 +00:00
|
|
|
nlohmann::json _send_request(std::optional<std::string> cache_key, const CurlUrl& url);
|
2023-11-22 08:39:24 +00:00
|
|
|
long _response_status_code();
|
|
|
|
|
|
|
|
std::mutex _share_locks[CURL_LOCK_DATA_LAST];
|
|
|
|
|
|
|
|
CURLSH* _share = nullptr;
|
|
|
|
pthread_key_t _easy_key;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern MastodonClient mastodon_client;
|