Add curl initialization.
This commit is contained in:
parent
6d9096193b
commit
e6690c85b4
|
@ -17,6 +17,8 @@
|
||||||
#ifndef MASTODONPP_INSTANCE_HPP
|
#ifndef MASTODONPP_INSTANCE_HPP
|
||||||
#define MASTODONPP_INSTANCE_HPP
|
#define MASTODONPP_INSTANCE_HPP
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace mastodonpp
|
namespace mastodonpp
|
||||||
|
@ -41,10 +43,30 @@ public:
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
explicit Instance(string instance, string access_token);
|
explicit Instance(string instance, string access_token);
|
||||||
|
~Instance();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const string _instance;
|
const string _instance;
|
||||||
string _access_token;
|
string _access_token;
|
||||||
|
CURL *_connection;
|
||||||
|
char _curl_buffer_error[CURL_ERROR_SIZE];
|
||||||
|
string _curl_buffer;
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief libcurl write callback function.
|
||||||
|
*
|
||||||
|
* @since 0.1.0
|
||||||
|
*/
|
||||||
|
static int writer(char *data, size_t size, size_t nmemb,
|
||||||
|
string *writerData);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Setup libcurl connection.
|
||||||
|
*
|
||||||
|
* @since 0.1.0
|
||||||
|
*/
|
||||||
|
void setup_curl();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mastodonpp
|
} // namespace mastodonpp
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
|
|
||||||
|
find_package(CURL REQUIRED)
|
||||||
|
|
||||||
# Write version in header.
|
# Write version in header.
|
||||||
configure_file ("version.hpp.in"
|
configure_file ("version.hpp.in"
|
||||||
"${PROJECT_BINARY_DIR}/version.hpp" @ONLY)
|
"${PROJECT_BINARY_DIR}/version.hpp" @ONLY)
|
||||||
|
@ -22,9 +24,8 @@ target_include_directories(${PROJECT_NAME}
|
||||||
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
|
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
|
||||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
||||||
|
|
||||||
# target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
# PRIVATE
|
PRIVATE CURL::libcurl)
|
||||||
# PUBLIC)
|
|
||||||
|
|
||||||
|
|
||||||
install(TARGETS ${PROJECT_NAME}
|
install(TARGETS ${PROJECT_NAME}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "instance.hpp"
|
#include "instance.hpp"
|
||||||
|
#include "exceptions.hpp"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -26,6 +27,53 @@ using std::move;
|
||||||
Instance::Instance(string instance, string access_token)
|
Instance::Instance(string instance, string access_token)
|
||||||
: _instance{move(instance)}
|
: _instance{move(instance)}
|
||||||
, _access_token{move(access_token)}
|
, _access_token{move(access_token)}
|
||||||
{}
|
, _connection{curl_easy_init()}
|
||||||
|
{
|
||||||
|
setup_curl();
|
||||||
|
}
|
||||||
|
Instance::~Instance()
|
||||||
|
{
|
||||||
|
curl_easy_cleanup(_connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Instance::writer(char *data, size_t size, size_t nmemb, string *writerData)
|
||||||
|
{
|
||||||
|
if(writerData == nullptr)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
writerData->append(data, size*nmemb);
|
||||||
|
|
||||||
|
return static_cast<int>(size * nmemb);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Instance::setup_curl()
|
||||||
|
{
|
||||||
|
if (_connection == nullptr)
|
||||||
|
{
|
||||||
|
throw CURLException{CURLE_FAILED_INIT, "Failed to initialize curl."};
|
||||||
|
}
|
||||||
|
|
||||||
|
CURLcode code{curl_easy_setopt(_connection, CURLOPT_ERRORBUFFER,
|
||||||
|
_curl_buffer_error)};
|
||||||
|
if (code != CURLE_OK)
|
||||||
|
{
|
||||||
|
throw CURLException{code, "Failed to set error buffer."};
|
||||||
|
}
|
||||||
|
|
||||||
|
code = curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, writer);
|
||||||
|
if (code != CURLE_OK)
|
||||||
|
{
|
||||||
|
throw CURLException{code, "Failed to set writer", _curl_buffer_error};
|
||||||
|
}
|
||||||
|
|
||||||
|
code = curl_easy_setopt(_connection, CURLOPT_WRITEDATA, &_curl_buffer);
|
||||||
|
if (code != CURLE_OK)
|
||||||
|
{
|
||||||
|
throw CURLException{code, "Failed to set write data",
|
||||||
|
_curl_buffer_error};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace mastodonpp
|
} // namespace mastodonpp
|
||||||
|
|
Loading…
Reference in New Issue
Block a user