Implement HTTP POST in Connection.

This commit is contained in:
tastytea 2020-01-10 13:08:55 +01:00
parent f222215116
commit a6139b4390
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 57 additions and 10 deletions

View File

@ -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<string_view>{"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 &parameters);
/*!
* @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

View File

@ -32,20 +32,28 @@ Connection::Connection(Instance &instance)
}
}
string Connection::endpoint_to_uri(const endpoint_variant &endpoint) const
{
if (holds_alternative<API::endpoint_type>(endpoint))
{
return string(_baseuri)
+= API{std::get<API::endpoint_type>(endpoint)}.to_string_view();
}
return string(_baseuri) += std::get<string_view>(endpoint);
}
answer_type Connection::get(const endpoint_variant &endpoint,
const parametermap &parameters)
{
const string uri{[&]
{
if (holds_alternative<API::endpoint_type>(endpoint))
{
return string(_baseuri)
+= API{std::get<API::endpoint_type>(endpoint)}.to_string_view();
}
return string(_baseuri) += std::get<string_view>(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 &parameters)
{
return make_request(http_method::POST,
endpoint_to_uri(endpoint), parameters);
}
string Connection::get_new_stream_contents()