Changed return types for simple calls.
the build failed Details

Also added return_entity, return_entity_vector and GenericEntity().
This commit is contained in:
tastytea 2019-02-25 13:55:24 +01:00
parent ada8f45415
commit 7bdeaeb236
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 134 additions and 41 deletions

View File

@ -24,6 +24,30 @@
using namespace Mastodon; using namespace Mastodon;
using std::string; using std::string;
return_entity::return_entity()
: entity()
{}
return_entity::return_entity(const uint8_t ec, const string &em,
const Easy::GenericEntity &ent)
: entity(ent)
{
error_code = ec;
error_message = em;
}
return_entity_vector::return_entity_vector()
: entities()
{}
return_entity_vector::return_entity_vector(const uint8_t ec, const string &em,
const vector<Easy::GenericEntity> &vec)
: entities(vec)
{
error_code = ec;
error_message = em;
}
Easy::Easy(const string &instance, const string &access_token) Easy::Easy(const string &instance, const string &access_token)
: API(instance, access_token) : API(instance, access_token)
{} {}

View File

@ -40,6 +40,10 @@ using std::chrono::system_clock;
namespace Mastodon namespace Mastodon
{ {
// Defined at the bottom
typedef struct return_entity return_entity;
typedef struct return_entity_vector return_entity_vector;
/*! /*!
* @brief Child of Mastodon::API with abstract methods. * @brief Child of Mastodon::API with abstract methods.
* *
@ -286,14 +290,14 @@ public:
* *
* @since 0.18.1 * @since 0.18.1
*/ */
const Status send_post(const Status &status, uint16_t &error); const return_entity send_post(const Status &status);
/*! /*!
* @brief Alias for send_post() * @brief Alias for send_post()
* *
* @since 0.17.0 * @since 0.17.0
*/ */
const Status send_toot(const Status &status, uint16_t &error); const return_entity send_toot(const Status &status);
/*! /*!
* @brief Gets notifications. * @brief Gets notifications.
@ -307,9 +311,9 @@ public:
* *
* @since 0.21.0 * @since 0.21.0
*/ */
const vector<Notification> get_notifications( const return_entity_vector get_notifications(const uint16_t limit = 20,
uint16_t &error, const uint16_t limit = 20, const string since_id = 0,
const string since_id = 0, const string max_id = 0); const string max_id = 0);
/*! /*!
* @brief Base class for all entities. * @brief Base class for all entities.
@ -335,6 +339,13 @@ public:
*/ */
Entity(); Entity();
/*!
* @brief Constructs an Entity object from a JSON object.
*
* @param generic The generic
*/
Entity(const Json::Value &object);
/*! /*!
* @brief Replaces the Entity with a new one from a JSON string. * @brief Replaces the Entity with a new one from a JSON string.
* *
@ -475,11 +486,60 @@ public:
mutable bool _was_set; mutable bool _was_set;
}; };
/*!
* @brief Class to hold generic entities.
*
* @since 0.100.0
*/
class GenericEntity : public Easy::Entity
{
public:
/*!
* @brief Constructs an GenericEntity object from a JSON string.
*
* @param json JSON string
*
* @since 0.100.0
*/
explicit GenericEntity(const string &json);
/*!
* @brief Constructs an empty GenericEntity object.
*
* @since 0.100.0
*/
explicit GenericEntity();
virtual bool valid() const override;
};
protected: protected:
inline static const string strtime inline static const string strtime
(const system_clock::time_point &timepoint, (const system_clock::time_point &timepoint,
const string &format, const bool &utc); const string &format, const bool &utc);
}; };
/*!
* Return type for simple calls.
* @since 0.100.0
*/
typedef struct return_entity : return_base
{
Easy::GenericEntity entity;
return_entity();
return_entity(const uint8_t ec, const string &em,
const Easy::GenericEntity &ent);
} return_entity;
typedef struct return_entity_vector : return_base
{
vector<Easy::GenericEntity> entities;
return_entity_vector();
return_entity_vector(const uint8_t ec, const string &em,
const vector<Easy::GenericEntity> &vec);
} return_entity_vector;
} }
#endif // MASTODON_EASY_CPP_HPP #endif // MASTODON_EASY_CPP_HPP

View File

@ -62,6 +62,11 @@ const Json::Value Easy::Entity::to_object() const
return _tree; return _tree;
} }
Easy::Entity::Entity(const Json::Value &object)
: _tree(object)
, _was_set(false)
{}
Easy::Entity::Entity() Easy::Entity::Entity()
: _was_set(false) : _was_set(false)
{} {}
@ -275,3 +280,16 @@ std::uint64_t Easy::Entity::stouint64(const string &str) const
return stoull(str); return stoull(str);
} }
} }
Easy::GenericEntity::GenericEntity(const string &json)
: Entity(json)
{}
Easy::GenericEntity::GenericEntity()
: Entity()
{}
bool Easy::GenericEntity::valid() const
{
return true;
}

View File

@ -23,16 +23,14 @@
using namespace Mastodon; using namespace Mastodon;
const Easy::Status Easy::send_toot(const Status &status, uint16_t &error) const return_entity Easy::send_toot(const Status &status)
{ {
return send_post(status, error); return send_post(status);
} }
const Easy::Status Easy::send_post(const Status &status, uint16_t &error) const return_entity Easy::send_post(const Status &status)
{ {
API::parametermap parameters; API::parametermap parameters;
string answer;
error = 0;
if (!status.content().empty()) if (!status.content().empty())
{ {
@ -41,8 +39,7 @@ const Easy::Status Easy::send_post(const Status &status, uint16_t &error)
else else
{ {
ttdebug << "ERROR: Easy::Status::content can not be empty.\n"; ttdebug << "ERROR: Easy::Status::content can not be empty.\n";
error = 11; return {22, "Easy::Status::content can not be empty", GenericEntity()};
return Status();
} }
if (!status.in_reply_to_id().empty()) if (!status.in_reply_to_id().empty())
@ -97,8 +94,8 @@ const Easy::Status Easy::send_post(const Status &status, uint16_t &error)
else else
{ {
ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n"; ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
error = 11; return { 22, "Easy::Attachment::file can not be empty",
return Status(); GenericEntity() };
} }
if (!att.description().empty()) if (!att.description().empty())
{ {
@ -111,40 +108,32 @@ const Easy::Status Easy::send_post(const Status &status, uint16_t &error)
std::to_string(att.focus()[1]) }}); std::to_string(att.focus()[1]) }});
} }
error = post(API::v1::media, param_att, answer); return_call ret = post(API::v1::media, param_att);
if (error == 0) if (ret.error_code == 0)
{ {
Attachment attachment(answer); Attachment attachment(ret.answer);
media_ids.push_back(attachment.id()); media_ids.push_back(attachment.id());
} }
else else
{ {
ttdebug << "ERROR: Could not upload file.\n"; ttdebug << "ERROR: Could not upload file.\n";
return Status(); return { ret.error_code, ret.error_message,
GenericEntity(ret.answer) };
} }
} }
parameters.insert({ "media_ids", media_ids }); parameters.insert({ "media_ids", media_ids });
} }
error = post(API::v1::statuses, parameters, answer); return_call ret = post(API::v1::statuses, parameters);
if (error == 0) return { ret.error_code, ret.error_message, GenericEntity(ret.answer) };
{
return Status(answer);
}
else
{
return Status();
}
} }
const vector<Easy::Notification> Easy::get_notifications( const return_entity_vector Easy::get_notifications(const uint16_t limit,
uint16_t &error, const uint16_t limit, const string since_id,
const string since_id, const string max_id) const string max_id)
{ {
API::parametermap parameters; API::parametermap parameters;
string answer;
error = 0;
parameters.insert({ "limit", { std::to_string(limit) } }); parameters.insert({ "limit", { std::to_string(limit) } });
if (!since_id.empty()) if (!since_id.empty())
@ -156,24 +145,24 @@ const vector<Easy::Notification> Easy::get_notifications(
parameters.insert({ "max_id", { max_id } }); parameters.insert({ "max_id", { max_id } });
} }
error = API::get(Mastodon::API::v1::notifications, parameters, answer); return_call ret = API::get(API::v1::notifications, parameters);
if (error == 0) if (ret.error_code == 0)
{ {
const vector<string> &answer_v = json_array_to_vector(answer); const vector<string> &answer_v = json_array_to_vector(ret.answer);
vector<Notification> notifications; vector<GenericEntity> notifications;
notifications.resize(answer_v.size()); notifications.resize(answer_v.size());
// Transform vector of strings to vector of Notification. // Transform vector of strings to vector of Notification.
std::transform(answer_v.begin(), answer_v.end(), notifications.begin(), std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
[](const string s) [](const string s)
{ return Notification(s); }); { return GenericEntity(s); });
return notifications; return { ret.error_code, ret.error_message, notifications };
} }
else else
{ {
ttdebug << "ERROR: Could not get notifications.\n"; ttdebug << "ERROR: Could not get notifications.\n";
return { Notification() }; return { ret.error_code, ret.error_message, {} };
} }
} }

View File

@ -39,7 +39,8 @@ using std::string;
namespace Mastodon namespace Mastodon
{ {
/*! /*!
* Base return type. * Base return type.
* @since 0.100.0
*/ */
typedef struct return_base typedef struct return_base
{ {
@ -51,7 +52,8 @@ namespace Mastodon
} return_base; } return_base;
/*! /*!
* Return type for API calls. * Return type for API calls.
* @since 0.100.0
*/ */
typedef struct return_call : return_base typedef struct return_call : return_base
{ {