Fixed deletion of parameters.

This commit is contained in:
tastytea 2019-04-14 19:57:39 +02:00
parent 64d4c65668
commit 7d245e0df1
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 39 additions and 8 deletions

View File

@ -15,6 +15,7 @@
*/ */
#include <iostream> #include <iostream>
#include <algorithm>
#include "debug.hpp" #include "debug.hpp"
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
@ -224,10 +225,8 @@ const return_call API::get(const Mastodon::API::v1 &call,
if (params.size() > 0) if (params.size() > 0)
{ {
// Delete the params that are already in strcall // Delete the parameters that are already in strcall
parameters newparams = params; const parameters newparams = delete_params(params, { "id", "tag" });
newparams.erase(newparams.find("id"));
newparams.erase(newparams.find("tag"));
strcall += maptostr(newparams); strcall += maptostr(newparams);
} }
@ -263,10 +262,8 @@ const return_call API::get(const Mastodon::API::v2 &call,
if (params.size() > 0) if (params.size() > 0)
{ {
// Delete the params that are already in strcall // Delete the parameterss that are already in strcall
parameters newparams = params; const parameters newparams = delete_params(params, { "id", "tag" });
newparams.erase(newparams.find("id"));
newparams.erase(newparams.find("tag"));
strcall += maptostr(newparams); strcall += maptostr(newparams);
} }

View File

@ -578,3 +578,24 @@ void API::get_proxy(string &proxy, string &userpw) const
} }
} }
} }
const parameters API::delete_params(const parameters &params,
const vector<string> &keys)
{
// Iterate through params. For each item in keys (k), compare to key of
// current parameter (p). Return false if parameter is to be deleted. Copy
// to new list of parameters (newparams) if true is returned.
parameters newparams(params.size());
const auto it =
std::copy_if(params.begin(), params.end(), newparams.begin(),
[&keys](const param &p)
{
return std::any_of(keys.begin(), keys.end(),
[&p](const string &k)
{
return (k != p.key);
});
});
newparams.resize(std::distance(newparams.begin(), it));
return newparams;
}

View File

@ -687,6 +687,19 @@ namespace Mastodon
* @return Form data as curlpp::Forms * @return Form data as curlpp::Forms
*/ */
const curlpp::Forms maptoformdata(const parameters &map); const curlpp::Forms maptoformdata(const parameters &map);
/*!
* @brief Delete Mastodon::param from Mastodon::parameters.
*
* @param params Old vector of parameters.
* @param key keys of Mastodon::param to delete.
*
* @return New vector of parameters.
*
* @since 0.102.0
*/
const parameters delete_params(const parameters &params,
const vector<string> &keys);
}; };
} }