Added Easy::Account::fields()

Returns the metadata fields introduced in Mastodon 2.4.0.
This commit is contained in:
tastytea 2018-06-12 03:05:12 +02:00
parent 14e164d7b4
commit b3dfec4204
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
5 changed files with 81 additions and 1 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.16.0
VERSION 0.16.1
LANGUAGES CXX
)

View File

@ -0,0 +1,45 @@
/* This file is part of mastodon-cpp.
* Get fields from own account.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
#endif
using std::cout;
using Mastodon::API;
using Mastodon::Easy;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Easy masto(argv[1], argv[2]);
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
cout << "Return code: " << ret << '\n';
Easy::Account account(answer);
std::vector<Easy::Account::fields_pair> fields(account.fields());
for (const auto &field : fields)
{
cout << "Name: " << field.first << "\nValue: " << field.second << "\n\n";
}
return 0;
}

View File

@ -58,6 +58,24 @@ const string Account::display_name() const
return get_string("display_name");
}
const std::vector<Account::fields_pair> Account::fields() const
{
const Json::Value &node = get("fields");
if (node.isArray())
{
std::vector<Account::fields_pair> vec;
for (const Json::Value &value : node)
{
vec.push_back(Account::fields_pair(value["name"].asString(),
value["value"].asString()));
}
return vec;
}
return {};
}
const std::uint_fast64_t Account::followers_count() const
{
return get_uint64("followers_count");

View File

@ -20,6 +20,8 @@
#include <string>
#include <cstdint>
#include <chrono>
#include <vector>
#include <utility>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
@ -42,6 +44,13 @@ namespace Mastodon
class Easy::Account : public Easy::Entity
{
public:
/*!
* @brief Describes a field. Format: name, value
*
* @since 0.16.1
*/
using fields_pair = std::pair<const string, const string>;
/*!
* @brief Constructs an Account object from a JSON string.
*
@ -87,6 +96,13 @@ namespace Mastodon
*/
const string display_name() const;
/*!
* @brief Returns metadata fields
*
* @since 0.16.1
*/
const std::vector<fields_pair> fields() const;
/*!
* @brief Returns number of followers
*/

View File

@ -46,6 +46,7 @@ using std::string;
* @example example13_easy_stream.cpp
* @example example14_easy_treeview.cpp
* @example example15_proxy.cpp
* @example example16_account_fields.cpp
*/
namespace Mastodon