Only construct 1 API per Connection.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
tastytea 2020-01-05 15:19:00 +01:00
parent 1226f7e57b
commit 571206c9f4
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 159 additions and 176 deletions

View File

@ -24,6 +24,7 @@
namespace mastodonpp namespace mastodonpp
{ {
using std::map;
using std::string_view; using std::string_view;
using std::variant; using std::variant;
@ -240,7 +241,7 @@ public:
* *
* @since 0.1.0 * @since 0.1.0
*/ */
explicit API(const endpoint_type &endpoint); explicit API();
/*! /*!
* @brief Convert #endpoint_type to `std::string_view`. * @brief Convert #endpoint_type to `std::string_view`.
@ -248,10 +249,14 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
string_view to_string_view() const; inline string_view endpoint_to_string_view(const endpoint_type &endpoint)
const
{
return _endpoint_map.at(endpoint).data();
}
private: private:
const endpoint_type _endpoint; const map<endpoint_type,string_view> _endpoint_map;
}; };
} // namespace mastodonpp } // namespace mastodonpp

View File

@ -74,6 +74,7 @@ public:
private: private:
Instance &_instance; Instance &_instance;
const string_view _baseuri; const string_view _baseuri;
const API _api;
}; };
} // namespace mastodonpp } // namespace mastodonpp

View File

@ -16,37 +16,24 @@
#include "api.hpp" #include "api.hpp"
#include <map>
namespace mastodonpp namespace mastodonpp
{ {
using std::map; API::API()
using std::string_view; : _endpoint_map
API::API(const endpoint_type &endpoint)
: _endpoint{endpoint}
{}
string_view API::to_string_view() const
{
static const map<endpoint_type,string_view> endpoint_map
{ {
{v1::apps, "/api/v1/apps"}, {v1::apps, "/api/v1/apps"},
{v1::apps_verify_credentials, "/api/v1/apps/verify/credentials"}, {v1::apps_verify_credentials, "/api/v1/apps/verify/credentials"},
{v1::accounts, "/api/v1/accounts"}, {v1::accounts, "/api/v1/accounts"},
{v1::accounts_verify_credentials, {v1::accounts_verify_credentials, "/api/v1/accounts/verify/credentials"},
"/api/v1/accounts/verify/credentials"}, {v1::accounts_update_credentials, "/api/v1/accounts/update/credentials"},
{v1::accounts_update_credentials,
"/api/v1/accounts/update/credentials"},
{v1::accounts_id, "/api/v1/accounts/id"}, {v1::accounts_id, "/api/v1/accounts/id"},
{v1::accounts_id_statuses, "/api/v1/accounts/<ID>/statuses"}, {v1::accounts_id_statuses, "/api/v1/accounts/<ID>/statuses"},
{v1::accounts_id_followers, "/api/v1/accounts/<ID>/followers"}, {v1::accounts_id_followers, "/api/v1/accounts/<ID>/followers"},
{v1::accounts_id_following, "/api/v1/accounts/<ID>/following"}, {v1::accounts_id_following, "/api/v1/accounts/<ID>/following"},
{v1::accounts_id_lists, "/api/v1/accounts/<ID>/lists"}, {v1::accounts_id_lists, "/api/v1/accounts/<ID>/lists"},
{v1::accounts_id_identity_proofs, {v1::accounts_id_identity_proofs, "/api/v1/accounts/<ID>/identity/proofs"},
"/api/v1/accounts/<ID>/identity/proofs"},
{v1::accounts_id_follow, "/api/v1/accounts/<ID>/follow"}, {v1::accounts_id_follow, "/api/v1/accounts/<ID>/follow"},
{v1::accounts_id_unfollow, "/api/v1/accounts/<ID>/unfollow"}, {v1::accounts_id_unfollow, "/api/v1/accounts/<ID>/unfollow"},
{v1::accounts_id_block, "/api/v1/accounts/<ID>/block"}, {v1::accounts_id_block, "/api/v1/accounts/<ID>/block"},
@ -76,15 +63,13 @@ string_view API::to_string_view() const
{v1::follow_requests, "/api/v1/follow/requests"}, {v1::follow_requests, "/api/v1/follow/requests"},
{v1::follow_requests_id_authorize, {v1::follow_requests_id_authorize,
"/api/v1/follow/requests/<ID>/authorize"}, "/api/v1/follow/requests/<ID>/authorize"},
{v1::follow_requests_id_reject, {v1::follow_requests_id_reject, "/api/v1/follow/requests/<ID>/reject"},
"/api/v1/follow/requests/<ID>/reject"},
{v1::endorsements, "/api/v1/endorsements"}, {v1::endorsements, "/api/v1/endorsements"},
{v1::featured_tags, "/api/v1/featured/tags"}, {v1::featured_tags, "/api/v1/featured/tags"},
{v1::featured_tags_id, "/api/v1/featured/tags/id"}, {v1::featured_tags_id, "/api/v1/featured/tags/id"},
{v1::featured_tags_suggestions, {v1::featured_tags_suggestions, "/api/v1/featured/tags/suggestions"},
"/api/v1/featured/tags/suggestions"},
{v1::preferences, "/api/v1/preferences"}, {v1::preferences, "/api/v1/preferences"},
@ -94,10 +79,8 @@ string_view API::to_string_view() const
{v1::statuses, "/api/v1/statuses"}, {v1::statuses, "/api/v1/statuses"},
{v1::statuses_id, "/api/v1/statuses/id"}, {v1::statuses_id, "/api/v1/statuses/id"},
{v1::statuses_id_context, "/api/v1/statuses/<ID>/context"}, {v1::statuses_id_context, "/api/v1/statuses/<ID>/context"},
{v1::statuses_id_reblogged_by, {v1::statuses_id_reblogged_by, "/api/v1/statuses/<ID>/reblogged/by"},
"/api/v1/statuses/<ID>/reblogged/by"}, {v1::statuses_id_favourited_by, "/api/v1/statuses/<ID>/favourited/by"},
{v1::statuses_id_favourited_by,
"/api/v1/statuses/<ID>/favourited/by"},
{v1::statuses_id_favourite, "/api/v1/statuses/<ID>/favourite"}, {v1::statuses_id_favourite, "/api/v1/statuses/<ID>/favourite"},
{v1::statuses_id_unfavourite, "/api/v1/statuses/<ID>/unfavourite"}, {v1::statuses_id_unfavourite, "/api/v1/statuses/<ID>/unfavourite"},
{v1::statuses_id_reblog, "/api/v1/statuses/<ID>/reblog"}, {v1::statuses_id_reblog, "/api/v1/statuses/<ID>/reblog"},
@ -145,8 +128,7 @@ string_view API::to_string_view() const
{v1::notifications, "/api/v1/notifications"}, {v1::notifications, "/api/v1/notifications"},
{v1::notifications_id, "/api/v1/notifications/id"}, {v1::notifications_id, "/api/v1/notifications/id"},
{v1::notifications_clear, "/api/v1/notifications/clear"}, {v1::notifications_clear, "/api/v1/notifications/clear"},
{v1::notifications_id_dismiss, {v1::notifications_id_dismiss, "/api/v1/notifications/<ID>/dismiss"},
"/api/v1/notifications/<ID>/dismiss"},
{v1::push_subscription, "/api/v1/push/subscription"}, {v1::push_subscription, "/api/v1/push/subscription"},
@ -164,22 +146,16 @@ string_view API::to_string_view() const
{v1::admin_accounts_id, "/api/v1/admin/accounts/id"}, {v1::admin_accounts_id, "/api/v1/admin/accounts/id"},
{v1::admin_accounts_account_id_action, {v1::admin_accounts_account_id_action,
"/api/v1/admin/accounts/account/<ID>/action"}, "/api/v1/admin/accounts/account/<ID>/action"},
{v1::admin_accounts_id_approve, {v1::admin_accounts_id_approve, "/api/v1/admin/accounts/<ID>/approve"},
"/api/v1/admin/accounts/<ID>/approve"}, {v1::admin_accounts_id_reject, "/api/v1/admin/accounts/<ID>/reject"},
{v1::admin_accounts_id_reject, {v1::admin_accounts_id_enable, "/api/v1/admin/accounts/<ID>/enable"},
"/api/v1/admin/accounts/<ID>/reject"}, {v1::admin_accounts_id_unsilence, "/api/v1/admin/accounts/<ID>/unsilence"},
{v1::admin_accounts_id_enable, {v1::admin_accounts_id_unsuspend, "/api/v1/admin/accounts/<ID>/unsuspend"},
"/api/v1/admin/accounts/<ID>/enable"},
{v1::admin_accounts_id_unsilence,
"/api/v1/admin/accounts/<ID>/unsilence"},
{v1::admin_accounts_id_unsuspend,
"/api/v1/admin/accounts/<ID>/unsuspend"},
{v1::admin_reports, "/api/v1/admin/reports"}, {v1::admin_reports, "/api/v1/admin/reports"},
{v1::admin_reports_id, "/api/v1/admin/reports/id"}, {v1::admin_reports_id, "/api/v1/admin/reports/id"},
{v1::admin_reports_id_assign_to_self, {v1::admin_reports_id_assign_to_self,
"/api/v1/admin/reports/<ID>/assign/to/self"}, "/api/v1/admin/reports/<ID>/assign/to/self"},
{v1::admin_reports_id_unassign, {v1::admin_reports_id_unassign, "/api/v1/admin/reports/<ID>/unassign"},
"/api/v1/admin/reports/<ID>/unassign"},
{v1::admin_reports_id_resolve, "/api/v1/admin/reports/resolve"}, {v1::admin_reports_id_resolve, "/api/v1/admin/reports/resolve"},
{v1::admin_reports_id_reopen, "/api/v1/admin/reports/<ID>/reopen"}, {v1::admin_reports_id_reopen, "/api/v1/admin/reports/<ID>/reopen"},
@ -191,7 +167,7 @@ string_view API::to_string_view() const
{other::proofs, "/api/proofs"}, {other::proofs, "/api/proofs"},
{other::oembed, "/api/oembed"} {other::oembed, "/api/oembed"}
};
return endpoint_map.at(_endpoint).data();
} }
{}
} // namespace mastodonpp } // namespace mastodonpp

View File

@ -22,13 +22,14 @@ namespace mastodonpp
Connection::Connection(Instance &instance) Connection::Connection(Instance &instance)
: _instance{instance} : _instance{instance}
, _baseuri{instance.get_baseuri()} , _baseuri{instance.get_baseuri()}
, _api{}
{} {}
answer_type Connection::get(const API::endpoint_type &endpoint) answer_type Connection::get(const API::endpoint_type &endpoint)
{ {
return make_request( return make_request(
http_method::GET, http_method::GET,
string(_baseuri).append(API{endpoint}.to_string_view())); string(_baseuri).append(_api.endpoint_to_string_view(endpoint)));
} }
answer_type Connection::get(const string_view &endpoint) answer_type Connection::get(const string_view &endpoint)