diff options
author | tastytea | 2020-01-14 20:44:08 +0100 |
---|---|---|
committer | tastytea | 2020-01-14 20:44:08 +0100 |
commit | a8e342202d7f429446a7193fc86ccbc0d0438820 (patch) | |
tree | 64dac8952eefefee783446b5ec0a9fb42626e31a | |
parent | bd490e788cbe17c86777e353f815d1a573163ba5 (diff) | |
download | mastodonpp-a8e342202d7f429446a7193fc86ccbc0d0438820.tar mastodonpp-a8e342202d7f429446a7193fc86ccbc0d0438820.tar.gz mastodonpp-a8e342202d7f429446a7193fc86ccbc0d0438820.zip |
Move types into types.hpp and add answer_type::next() and prev().
-rw-r--r-- | include/connection.hpp | 2 | ||||
-rw-r--r-- | include/curl_wrapper.hpp | 40 | ||||
-rw-r--r-- | include/instance.hpp | 2 | ||||
-rw-r--r-- | include/mastodonpp.hpp | 2 | ||||
-rw-r--r-- | include/types.hpp (renamed from include/answer.hpp) | 85 | ||||
-rw-r--r-- | src/instance.cpp | 1 | ||||
-rw-r--r-- | src/types.cpp (renamed from src/answer.cpp) | 39 |
7 files changed, 124 insertions, 47 deletions
diff --git a/include/connection.hpp b/include/connection.hpp index 27119e4..bf37219 100644 --- a/include/connection.hpp +++ b/include/connection.hpp @@ -17,10 +17,10 @@ #ifndef MASTODONPP_CONNECTION_HPP #define MASTODONPP_CONNECTION_HPP -#include "answer.hpp" #include "api.hpp" #include "curl_wrapper.hpp" #include "instance.hpp" +#include "types.hpp" #include <string> #include <string_view> diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index 30fa1ce..890b36e 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -17,28 +17,20 @@ #ifndef MASTODONPP_CURL_WRAPPER_HPP #define MASTODONPP_CURL_WRAPPER_HPP -#include "answer.hpp" +#include "types.hpp" #include "curl/curl.h" -#include <map> #include <mutex> #include <string> #include <string_view> -#include <utility> -#include <variant> -#include <vector> namespace mastodonpp { -using std::map; using std::mutex; using std::string; using std::string_view; -using std::pair; -using std::variant; -using std::vector; /*! * @brief The HTTP method. @@ -55,36 +47,6 @@ enum class http_method }; /*! - * @brief `std::map` of parameters for %API calls. - * - * Note that arrays always have to be specified as vectors, even if they have - * only 1 element. To send a file, use “<tt>\@file:</tt>” followed by the file - * name as value. - * - * Example: - * @code - * parametermap parameters - * { - * {"poll[expires_in]", "86400"}, - * {"poll[options]", vector<string_view>{"Yes", "No", "Maybe"}}, - * {"status", "How is the weather?"} - * }; - * @endcode - * - * @since 0.1.0 - */ -using parametermap = - map<string_view, variant<string_view, vector<string_view>>>; - -/*! - * @brief A single parameter of a parametermap. - * - * @since 0.1.0 - */ -using parameterpair = - pair<string_view, variant<string_view, vector<string_view>>>; - -/*! * @brief Handles the details of network connections. * * You don't need to use this. diff --git a/include/instance.hpp b/include/instance.hpp index df62646..a7feb78 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -18,7 +18,7 @@ #define MASTODONPP_INSTANCE_HPP #include "curl_wrapper.hpp" -#include "answer.hpp" +#include "types.hpp" #include <cstdint> #include <string> diff --git a/include/mastodonpp.hpp b/include/mastodonpp.hpp index ca396d7..65c1af7 100644 --- a/include/mastodonpp.hpp +++ b/include/mastodonpp.hpp @@ -17,11 +17,11 @@ #ifndef MASTODONPP_HPP #define MASTODONPP_HPP -#include "answer.hpp" #include "api.hpp" #include "connection.hpp" #include "exceptions.hpp" #include "instance.hpp" +#include "types.hpp" /*! * @headerfile mastodonpp.hpp mastodonpp/mastodonpp.hpp diff --git a/include/answer.hpp b/include/types.hpp index b820a70..bfd5e42 100644 --- a/include/answer.hpp +++ b/include/types.hpp @@ -14,22 +14,62 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef MASTODONPP_ANSWER_HPP -#define MASTODONPP_ANSWER_HPP +// Types that are used in more than one file. + +#ifndef MASTODONPP_TYPES_HPP +#define MASTODONPP_TYPES_HPP #include <cstdint> +#include <map> #include <ostream> #include <string> #include <string_view> +#include <utility> +#include <variant> +#include <vector> namespace mastodonpp { using std::uint8_t; using std::uint16_t; +using std::map; using std::ostream; using std::string; using std::string_view; +using std::pair; +using std::variant; +using std::vector; + +/*! + * @brief `std::map` of parameters for %API calls. + * + * Note that arrays always have to be specified as vectors, even if they have + * only 1 element. To send a file, use “<tt>\@file:</tt>” followed by the file + * name as value. + * + * Example: + * @code + * parametermap parameters + * { + * {"poll[expires_in]", "86400"}, + * {"poll[options]", vector<string_view>{"Yes", "No", "Maybe"}}, + * {"status", "How is the weather?"} + * }; + * @endcode + * + * @since 0.1.0 + */ +using parametermap = + map<string_view, variant<string_view, vector<string_view>>>; + +/*! + * @brief A single parameter of a parametermap. + * + * @since 0.1.0 + */ +using parameterpair = + pair<string_view, variant<string_view, vector<string_view>>>; /*! * @brief Return type for Request%s. @@ -110,9 +150,48 @@ struct answer_type * * @since 0.1.0 */ + [[nodiscard]] string_view get_header(string_view field) const; + + /*! + * @brief Returns the parameters needed for the next entries. + * + * Parses the `Link` header. + * + * @since 0.3.0 + */ + [[nodiscard]] + inline parametermap next() const + { + return parse_pagination(true); + } + + /*! + * @brief Returns the parameters needed for the previous entries. + * + * + * Parses the `Link` header. + * + * @since 0.3.0 + */ + [[nodiscard]] + inline parametermap prev() const + { + return parse_pagination(false); + } + +private: + /*! + * @brief Returns the parameters needed for the next or previous entries. + * + * + * Parses the `Link` header. + * + * @since 0.3.0 + */ + parametermap parse_pagination(bool next) const; }; } // namespace mastodonpp -#endif // MASTODONPP_ANSWER_HPP +#endif // MASTODONPP_TYPES_HPP diff --git a/src/instance.cpp b/src/instance.cpp index cd99645..5e9126d 100644 --- a/src/instance.cpp +++ b/src/instance.cpp @@ -14,7 +14,6 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "answer.hpp" #include "instance.hpp" #include "log.hpp" diff --git a/src/answer.cpp b/src/types.cpp index 0db9176..8c4575c 100644 --- a/src/answer.cpp +++ b/src/types.cpp @@ -14,7 +14,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "answer.hpp" +#include "log.hpp" +#include "types.hpp" #include <algorithm> #include <cctype> @@ -60,4 +61,40 @@ string_view answer_type::get_header(const string_view field) const return {}; } +parametermap answer_type::parse_pagination(const bool next) const +{ + const string_view link{get_header("Link")}; + if (link.empty()) + { + return {}; + } + + const auto direction{next ? R"(rel="next")" : R"(rel="prev")"}; + auto endpos{link.find(direction)}; + endpos = link.rfind('>', endpos); + auto startpos{link.rfind('?', endpos) + 1}; + const string_view paramstr{link.substr(startpos, endpos - startpos)}; + debuglog << "Found parameters in Link header: " << paramstr << '\n'; + + startpos = 0; + parametermap parameters; + while ((endpos = paramstr.find('=', startpos)) != string_view::npos) + { + parameterpair param; + param.first = paramstr.substr(startpos, endpos - startpos); + startpos = endpos + 1; + endpos = paramstr.find('&', startpos); + param.second = paramstr.substr(startpos, endpos - startpos); + parameters.insert(param); + + if (endpos == string_view::npos) + { + break; + } + startpos = endpos + 1; + } + + return parameters; +} + } // namespace mastodonpp |