#include "mastodonpp.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
using std::exit;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::string;
using std::to_string;
using std::string_view;
using std::vector;
void handle_error(const masto::answer_type &answer);
int main(int argc, char *argv[])
{
const vector<string_view> args(argv, argv + argc);
if (args.size() <= 1)
{
cerr << "Usage: " << args[0] << " <instance hostname>\n";
return 1;
}
try
{
masto::Instance instance{args[1], {}};
masto::Instance::ObtainToken token{instance};
auto answer{token.step_1("Testclient", "read:blocks read:mutes",
"https://example.com/")};
if (!answer)
{
handle_error(answer);
}
cout << "Please visit " << answer << "\nand paste the code here: ";
string code;
cin >> code;
answer = token.step_2(code);
if (!answer)
{
handle_error(answer);
}
cout << "Your access token is: " << answer << endl;
masto::Connection connection{instance};
answer = connection.get(masto::API::v1::apps_verify_credentials);
if (!answer)
{
handle_error(answer);
}
cout << answer << endl;
}
catch (const masto::CURLException &e)
{
cerr << e.what() << endl;
}
return 0;
}
void handle_error(const masto::answer_type &answer)
{
if (answer.curl_error_code == 0)
{
cerr << "HTTP status: " << answer.http_status << endl;
}
else
{
cerr << "libcurl error " << to_string(answer.curl_error_code)
<< ": " << answer.error_message << endl;
}
exit(1);
}