Breaking change: parameters is now a multimap, not a vector anymore

This commit is contained in:
tastytea 2018-01-15 20:23:21 +01:00
parent 80df5cafff
commit 5bba324385
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
7 changed files with 120 additions and 49 deletions

View File

@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.7)
include(GNUInstallDirs) include(GNUInstallDirs)
project (mastodon-cpp project (mastodon-cpp
VERSION 0.1.1 VERSION 0.1.2
LANGUAGES CXX LANGUAGES CXX
) )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fPIC") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fPIC")

View File

@ -43,6 +43,20 @@ Install with `make install`.
The HTML reference can be generated with `build_doc.sh`, if doxygen is installed. Or just look in `src/mastodon-cpp.hpp`. There are examples in `src/examples/`. The HTML reference can be generated with `build_doc.sh`, if doxygen is installed. Or just look in `src/mastodon-cpp.hpp`. There are examples in `src/examples/`.
## Most basic example
#include <iostream>
#include <string>
#include <mastodon-cpp.hpp>
int main()
{
Mastodon::API masto("social.example.com", "secret_token");
std::string answer;
masto.get(API::v1::accounts_verify_credentials, answer);
std::cout << answer << '\n';
}
## Compiling your project ## Compiling your project
After you did a `make install`, a project consisting of one file can be compiled as follows: After you did a `make install`, a project consisting of one file can be compiled as follows:

View File

@ -25,12 +25,12 @@ using std::string;
using std::cerr; using std::cerr;
const std::uint16_t API::get(const Mastodon::API::v1 &call, string &answer) const std::uint16_t API::get(const Mastodon::API::v1 &call, string &answer)
{ {
const std::vector<string> v{}; const parametermap p;
return get(call, v, answer); return get(call, p, answer);
} }
const std::uint16_t API::get(const Mastodon::API::v1 &call, const std::uint16_t API::get(const Mastodon::API::v1 &call,
const std::vector<string> &parameters, string &answer) const parametermap &parameters, string &answer)
{ {
string strcall = ""; string strcall = "";
switch (call) switch (call)
@ -82,15 +82,7 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call,
if (parameters.size() > 0) if (parameters.size() > 0)
{ {
char delim = '?'; strcall += maptostr(parameters);
for (const string p : parameters)
{
strcall += delim + p;
if (delim == '?')
{
delim = '&';
}
}
} }
return _http.request_sync(http::method::GET, strcall, answer); return _http.request_sync(http::method::GET, strcall, answer);
@ -99,15 +91,15 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call,
const std::uint16_t API::get(const Mastodon::API::v1 &call, const std::uint16_t API::get(const Mastodon::API::v1 &call,
const string &argument, string &answer) const string &argument, string &answer)
{ {
const std::vector<string> v; const parametermap p;
return get(call, argument, v, answer); return get(call, argument, p, answer);
} }
const std::uint16_t API::get(const Mastodon::API::v1 &call, const std::uint16_t API::get(const Mastodon::API::v1 &call,
const string &argument, const string &argument,
const std::vector<string> &parameters, string &answer) const parametermap &parameters, string &answer)
{ {
string strcall = ""; string strcall = "";
char delim = '?'; bool firstparam = true;
switch (call) switch (call)
{ {
@ -125,11 +117,11 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call,
break; break;
case v1::accounts_relationships: case v1::accounts_relationships:
strcall = "/api/v1/accounts/relationships?id=" + argument; strcall = "/api/v1/accounts/relationships?id=" + argument;
delim = '&'; firstparam = false;
break; break;
case v1::accounts_search: case v1::accounts_search:
strcall = "/api/v1/accounts/search?q=" + argument; strcall = "/api/v1/accounts/search?q=" + argument;
delim = '&'; firstparam = false;
break; break;
case v1::accounts_id_lists: case v1::accounts_id_lists:
strcall = "/api/v1/accounts/" + argument + "/lists"; strcall = "/api/v1/accounts/" + argument + "/lists";
@ -145,7 +137,7 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call,
break; break;
case v1::search: case v1::search:
strcall = "/api/v1/search?q=" + argument; strcall = "/api/v1/search?q=" + argument;
delim = '&'; firstparam = false;
break; break;
case v1::statuses_id: case v1::statuses_id:
strcall = "/api/v1/statuses/" + argument; strcall = "/api/v1/statuses/" + argument;
@ -176,14 +168,7 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call,
if (parameters.size() > 0) if (parameters.size() > 0)
{ {
for (const string p : parameters) strcall += maptostr(parameters, firstparam);
{
strcall += delim + p;
if (delim == '?')
{
delim = '&';
}
}
} }
return _http.request_sync(http::method::GET, strcall, answer); return _http.request_sync(http::method::GET, strcall, answer);

View File

@ -29,10 +29,10 @@ int main(int argc, char *argv[])
{ {
std::cout << "Your last toot with media attached:\n"; std::cout << "Your last toot with media attached:\n";
std::string uid = answer.substr(7, answer.find("\"", 7) - 7); std::string uid = answer.substr(7, answer.find("\"", 7) - 7);
std::vector<std::string> parameters = API::parametermap parameters =
{ {
"limit=1", { "limit", { "1" } },
"only_media=1" { "only_media", { "1" } }
}; };
ret = masto.get(API::v1::accounts_id_statuses, uid,parameters, answer); ret = masto.get(API::v1::accounts_id_statuses, uid,parameters, answer);
@ -49,10 +49,12 @@ int main(int argc, char *argv[])
std::cout << "\nYour last 2 followers:\n"; std::cout << "\nYour last 2 followers:\n";
parameters = parameters =
{ {
"limit=2", {
"exclude_types[]=favourite", "limit", { "2" }
"exclude_types[]=reblog", },
"exclude_types[]=mention" {
"exclude_types", { "favourite", "reblog", "mention" }
}
}; };
ret = masto.get(API::v1::notifications, parameters, answer); ret = masto.get(API::v1::notifications, parameters, answer);
if (ret == 0) if (ret == 0)

View File

@ -9,7 +9,7 @@
#include <sstream> #include <sstream>
#include <regex> #include <regex>
#include <cstdlib> #include <cstdlib>
#include <mastodon-cpp.hpp> #include "../mastodon-cpp.hpp"
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
@ -51,7 +51,11 @@ int main(int argc, char *argv[])
} }
// Only get toots we haven't seen yet // Only get toots we haven't seen yet
std::vector<string> params = { "limit=" + limit, "since_id=" + lastid }; API::parametermap params =
{
{ "limit", { limit } },
{ "since_id", { lastid } }
};
ret = masto.get(API::v1::timelines_tag_hashtag, hashtag, params, answer); ret = masto.get(API::v1::timelines_tag_hashtag, hashtag, params, answer);
if (ret == 0) if (ret == 0)

View File

@ -15,9 +15,14 @@
*/ */
#include <string> #include <string>
#include <map>
#include <vector>
#include "version.hpp" #include "version.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
#include <iostream>
using namespace Mastodon; using namespace Mastodon;
using std::string; using std::string;
@ -35,7 +40,43 @@ const void API::set_useragent(const std::string &useragent)
_useragent = useragent; _useragent = useragent;
} }
const std::string API::get_useragent() const const string API::get_useragent() const
{ {
return _useragent; return _useragent;
} }
const string API::maptostr(const parametermap &map, const bool &firstparam)
{
string result = "";
char delim = '?';
if (!firstparam)
{
delim = '&';
}
for (const auto &it : map)
{
if (it.second.size() == 1)
{
result += (delim + it.first + "=" + it.second.front());
if (delim == '?')
{
delim = '&';
}
}
else
{
for (const string &str : it.second)
{
result += (delim + it.first + "[]=" + str);
if (delim == '?')
{
delim = '&';
}
}
}
}
ttdebug << "Constructed parameter string: " << result << '\n';
return result;
}

View File

@ -20,6 +20,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
#include <map>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>
@ -47,6 +48,19 @@ namespace Mastodon
class API class API
{ {
public: public:
/*!
* @brief Used for passing optional parameters.
*
* Example:
* @code
* parametermap p =
* {
* {"field1", { "value1", "value2" } },
* {"field2", { "value" } }
* }
* @endcode
*/
typedef std::multimap<std::string, std::vector<std::string>> parametermap;
/*! /*!
* @brief A list of all API calls. * @brief A list of all API calls.
* *
@ -105,7 +119,7 @@ public:
* @param answer The answer from the server. Usually JSON. On error an * @param answer The answer from the server. Usually JSON. On error an
* empty string. * empty string.
* *
* @return Error code. See README.md for details. * @return @ref error "Error code".
*/ */
const std::uint16_t get(const Mastodon::API::v1 &call, std::string &answer); const std::uint16_t get(const Mastodon::API::v1 &call, std::string &answer);
@ -117,7 +131,7 @@ public:
* @param answer The answer from the server. Usually JSON. On error an * @param answer The answer from the server. Usually JSON. On error an
* empty string. * empty string.
* *
* @return Error code. See README.md for details. * @return @ref error "Error code".
*/ */
const std::uint16_t get(const Mastodon::API::v1 &call, const std::uint16_t get(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
@ -128,15 +142,15 @@ public:
* optional parameters. * optional parameters.
* *
* @param call A call defined in Mastodon::API::v1 * @param call A call defined in Mastodon::API::v1
* @param parameters A std::vector containing optional parameters in the * @param parameters A Mastodon::API::parametermap containing optional
* form `field=value` * parameters.
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return Error code. See README.md for details. * @return @ref error "Error code".
*/ */
const std::uint16_t get(const Mastodon::API::v1 &call, const std::uint16_t get(const Mastodon::API::v1 &call,
const std::vector<std::string> &parameters, const parametermap &parameters,
std::string &answer); std::string &answer);
/*! /*!
@ -145,16 +159,16 @@ public:
* *
* @param call A call defined in Mastodon::API::v1 * @param call A call defined in Mastodon::API::v1
* @param argument The non-optional argument * @param argument The non-optional argument
* @param parameters A std::vector containing optional parameters in the * @param parameters A Mastodon::API::parametermap containing optional
* form `field=value` * parameters.
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return Error code. See README.md for details. * @return @ref error "Error code".
*/ */
const std::uint16_t get(const Mastodon::API::v1 &call, const std::uint16_t get(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
const std::vector<std::string> &parameters, const parametermap &parameters,
std::string &answer); std::string &answer);
/*! /*!
@ -164,7 +178,7 @@ public:
* @param answer The answer from the server. Usually JSON. On error an * @param answer The answer from the server. Usually JSON. On error an
* empty string. * empty string.
* *
* @return Error code. See README.md for details. * @return @ref error "Error code".
*/ */
const std::uint16_t get(const std::string &call, const std::uint16_t get(const std::string &call,
std::string &answer); std::string &answer);
@ -188,6 +202,17 @@ private:
const std::string _access_token; const std::string _access_token;
std::string _useragent; std::string _useragent;
/*!
* @brief Converts map of parameters into a string.
*
* @param map Map of parameters
* @param firstparam Contains this map the first parameter?
*
* @return String of parameters
*/
const std::string maptostr(const parametermap &map,
const bool &firstparam = true);
class http class http
{ {
public: public: