Changed return types for simple calls.
the build failed
Details
the build failed
Details
Also added return_entity, return_entity_vector and GenericEntity().
This commit is contained in:
parent
ada8f45415
commit
7bdeaeb236
|
@ -24,6 +24,30 @@
|
|||
using namespace Mastodon;
|
||||
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)
|
||||
: API(instance, access_token)
|
||||
{}
|
||||
|
|
|
@ -40,6 +40,10 @@ using std::chrono::system_clock;
|
|||
|
||||
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.
|
||||
*
|
||||
|
@ -286,14 +290,14 @@ public:
|
|||
*
|
||||
* @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()
|
||||
*
|
||||
* @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.
|
||||
|
@ -307,9 +311,9 @@ public:
|
|||
*
|
||||
* @since 0.21.0
|
||||
*/
|
||||
const vector<Notification> get_notifications(
|
||||
uint16_t &error, const uint16_t limit = 20,
|
||||
const string since_id = 0, const string max_id = 0);
|
||||
const return_entity_vector get_notifications(const uint16_t limit = 20,
|
||||
const string since_id = 0,
|
||||
const string max_id = 0);
|
||||
|
||||
/*!
|
||||
* @brief Base class for all entities.
|
||||
|
@ -335,6 +339,13 @@ public:
|
|||
*/
|
||||
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.
|
||||
*
|
||||
|
@ -475,11 +486,60 @@ public:
|
|||
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:
|
||||
inline static const string strtime
|
||||
(const system_clock::time_point &timepoint,
|
||||
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
|
||||
|
|
|
@ -62,6 +62,11 @@ const Json::Value Easy::Entity::to_object() const
|
|||
return _tree;
|
||||
}
|
||||
|
||||
Easy::Entity::Entity(const Json::Value &object)
|
||||
: _tree(object)
|
||||
, _was_set(false)
|
||||
{}
|
||||
|
||||
Easy::Entity::Entity()
|
||||
: _was_set(false)
|
||||
{}
|
||||
|
@ -275,3 +280,16 @@ std::uint64_t Easy::Entity::stouint64(const string &str) const
|
|||
return stoull(str);
|
||||
}
|
||||
}
|
||||
|
||||
Easy::GenericEntity::GenericEntity(const string &json)
|
||||
: Entity(json)
|
||||
{}
|
||||
|
||||
Easy::GenericEntity::GenericEntity()
|
||||
: Entity()
|
||||
{}
|
||||
|
||||
bool Easy::GenericEntity::valid() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -23,16 +23,14 @@
|
|||
|
||||
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;
|
||||
string answer;
|
||||
error = 0;
|
||||
|
||||
if (!status.content().empty())
|
||||
{
|
||||
|
@ -41,8 +39,7 @@ const Easy::Status Easy::send_post(const Status &status, uint16_t &error)
|
|||
else
|
||||
{
|
||||
ttdebug << "ERROR: Easy::Status::content can not be empty.\n";
|
||||
error = 11;
|
||||
return Status();
|
||||
return {22, "Easy::Status::content can not be empty", GenericEntity()};
|
||||
}
|
||||
|
||||
if (!status.in_reply_to_id().empty())
|
||||
|
@ -97,8 +94,8 @@ const Easy::Status Easy::send_post(const Status &status, uint16_t &error)
|
|||
else
|
||||
{
|
||||
ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
|
||||
error = 11;
|
||||
return Status();
|
||||
return { 22, "Easy::Attachment::file can not be empty",
|
||||
GenericEntity() };
|
||||
}
|
||||
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]) }});
|
||||
}
|
||||
|
||||
error = post(API::v1::media, param_att, answer);
|
||||
if (error == 0)
|
||||
return_call ret = post(API::v1::media, param_att);
|
||||
if (ret.error_code == 0)
|
||||
{
|
||||
Attachment attachment(answer);
|
||||
Attachment attachment(ret.answer);
|
||||
media_ids.push_back(attachment.id());
|
||||
}
|
||||
else
|
||||
{
|
||||
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 });
|
||||
}
|
||||
|
||||
error = post(API::v1::statuses, parameters, answer);
|
||||
if (error == 0)
|
||||
{
|
||||
return Status(answer);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Status();
|
||||
}
|
||||
return_call ret = post(API::v1::statuses, parameters);
|
||||
return { ret.error_code, ret.error_message, GenericEntity(ret.answer) };
|
||||
}
|
||||
|
||||
const vector<Easy::Notification> Easy::get_notifications(
|
||||
uint16_t &error, const uint16_t limit,
|
||||
const string since_id, const string max_id)
|
||||
const return_entity_vector Easy::get_notifications(const uint16_t limit,
|
||||
const string since_id,
|
||||
const string max_id)
|
||||
{
|
||||
API::parametermap parameters;
|
||||
string answer;
|
||||
error = 0;
|
||||
|
||||
parameters.insert({ "limit", { std::to_string(limit) } });
|
||||
if (!since_id.empty())
|
||||
|
@ -156,24 +145,24 @@ const vector<Easy::Notification> Easy::get_notifications(
|
|||
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);
|
||||
vector<Notification> notifications;
|
||||
const vector<string> &answer_v = json_array_to_vector(ret.answer);
|
||||
vector<GenericEntity> notifications;
|
||||
notifications.resize(answer_v.size());
|
||||
|
||||
// Transform vector of strings to vector of Notification.
|
||||
std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
|
||||
[](const string s)
|
||||
{ return Notification(s); });
|
||||
{ return GenericEntity(s); });
|
||||
|
||||
return notifications;
|
||||
return { ret.error_code, ret.error_message, notifications };
|
||||
}
|
||||
else
|
||||
{
|
||||
ttdebug << "ERROR: Could not get notifications.\n";
|
||||
return { Notification() };
|
||||
return { ret.error_code, ret.error_message, {} };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,8 @@ using std::string;
|
|||
namespace Mastodon
|
||||
{
|
||||
/*!
|
||||
* Base return type.
|
||||
* Base return type.
|
||||
* @since 0.100.0
|
||||
*/
|
||||
typedef struct return_base
|
||||
{
|
||||
|
@ -51,7 +52,8 @@ namespace Mastodon
|
|||
} return_base;
|
||||
|
||||
/*!
|
||||
* Return type for API calls.
|
||||
* Return type for API calls.
|
||||
* @since 0.100.0
|
||||
*/
|
||||
typedef struct return_call : return_base
|
||||
{
|
||||
|
|
Reference in New Issue