diff --git a/include/connection.hpp b/include/connection.hpp index 6885931..91a06d7 100644 --- a/include/connection.hpp +++ b/include/connection.hpp @@ -124,6 +124,43 @@ public: return get(endpoint, {}); } + /*! + * @brief Make a HTTP POST call with parameters. + * + * Example: + * @code + * auto answer{connection.post( + * mastodonpp::API::v1::statuses, + * { + * {"status", "How is the wheather?"}, + * {"poll[options]", vector{"Nice", "not nice"}}, + * {"poll[expires_in]", to_string(poll_seconds)} + * })}; + * @endcode + * + * @param endpoint Endpoint as API::endpoint_type or `std::string_view`. + * @param parameters A map of parameters. + * + * + * @since 0.1.0 + */ + [[nodiscard]] + answer_type post(const endpoint_variant &endpoint, + const parametermap ¶meters); + + /*! + * @brief Make a HTTP POST call. + * + * @param endpoint Endpoint as API::endpoint_type or `std::string_view`. + * + * @since 0.1.0 + */ + [[nodiscard]] + inline answer_type post(const endpoint_variant &endpoint) + { + return post(endpoint, {}); + } + /*! * @brief Copy new stream contents and delete the “original”. * @@ -147,6 +184,8 @@ public: private: Instance &_instance; const string_view _baseuri; + + string endpoint_to_uri(const endpoint_variant &endpoint) const; }; } // namespace mastodonpp diff --git a/src/connection.cpp b/src/connection.cpp index 662c333..498384d 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -32,20 +32,28 @@ Connection::Connection(Instance &instance) } } +string Connection::endpoint_to_uri(const endpoint_variant &endpoint) const +{ + if (holds_alternative(endpoint)) + { + return string(_baseuri) + += API{std::get(endpoint)}.to_string_view(); + } + return string(_baseuri) += std::get(endpoint); +} + answer_type Connection::get(const endpoint_variant &endpoint, const parametermap ¶meters) { - const string uri{[&] - { - if (holds_alternative(endpoint)) - { - return string(_baseuri) - += API{std::get(endpoint)}.to_string_view(); - } - return string(_baseuri) += std::get(endpoint); - }()}; + return make_request(http_method::GET, + endpoint_to_uri(endpoint), parameters); +} - return make_request(http_method::GET, uri, parameters); +answer_type Connection::post(const endpoint_variant &endpoint, + const parametermap ¶meters) +{ + return make_request(http_method::POST, + endpoint_to_uri(endpoint), parameters); } string Connection::get_new_stream_contents()