From a8e342202d7f429446a7193fc86ccbc0d0438820 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 14 Jan 2020 20:44:08 +0100 Subject: Move types into types.hpp and add answer_type::next() and prev(). --- include/answer.hpp | 118 ---------------------------- include/connection.hpp | 2 +- include/curl_wrapper.hpp | 40 +--------- include/instance.hpp | 2 +- include/mastodonpp.hpp | 2 +- include/types.hpp | 197 +++++++++++++++++++++++++++++++++++++++++++++++ src/answer.cpp | 63 --------------- src/instance.cpp | 1 - src/types.cpp | 100 ++++++++++++++++++++++++ 9 files changed, 301 insertions(+), 224 deletions(-) delete mode 100644 include/answer.hpp create mode 100644 include/types.hpp delete mode 100644 src/answer.cpp create mode 100644 src/types.cpp diff --git a/include/answer.hpp b/include/answer.hpp deleted file mode 100644 index b820a70..0000000 --- a/include/answer.hpp +++ /dev/null @@ -1,118 +0,0 @@ -/* This file is part of mastodonpp. - * Copyright © 2020 tastytea - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef MASTODONPP_ANSWER_HPP -#define MASTODONPP_ANSWER_HPP - -#include -#include -#include -#include - -namespace mastodonpp -{ - -using std::uint8_t; -using std::uint16_t; -using std::ostream; -using std::string; -using std::string_view; - -/*! - * @brief Return type for Request%s. - * - * @since 0.1.0 - * - * @headerfile answer.hpp mastodonpp/answer.hpp - * - */ -struct answer_type -{ - /*! - * @brief The error code returned by libcurl. - * - * For more information consult - * [libcurl-errors(3)](https://curl.haxx.se/libcurl/c/libcurl-errors.html). - * - * @since 0.1.0 - */ - uint8_t curl_error_code{0}; - - /*! - * @brief The error message. - * - * @since 0.1.0 - */ - string error_message; - - /*! - * @brief HTTP status code. - * - * @since 0.1.0 - */ - uint16_t http_status{0}; - - /*! - * @brief The headers of the response from the server. - * - * @since 0.1.0 - */ - string headers; - - /*! - * @brief The response from the server, usually JSON. - * - * @since 0.1.0 - */ - string body; - - /*! - * @brief Returns true if #curl_error_code is 0 and #http_status is 200, - * false otherwise. - * - * @since 0.1.0 - */ - explicit operator bool() const; - - /*! - * @brief Returns #body as `std::string_view`. - * - * @since 0.1.0 - */ - explicit operator string_view() const; - - /*! - * @brief Returns #body as `std::ostream`. - * - * @since 0.1.0 - */ - friend ostream &operator <<(ostream &out, const answer_type &answer); - - /*! - * @brief Returns the value of a header field. - * - * Is only valid for as long as the answer_type is in scope. - * - * @param field Case insensitive, only ASCII. - * - * @since 0.1.0 - */ - string_view get_header(string_view field) const; -}; - -} // namespace mastodonpp - -#endif // MASTODONPP_ANSWER_HPP 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 #include 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 #include #include #include -#include -#include -#include 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. @@ -54,36 +46,6 @@ enum class http_method DELETE }; -/*! - * @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 “\@file:” followed by the file - * name as value. - * - * Example: - * @code - * parametermap parameters - * { - * {"poll[expires_in]", "86400"}, - * {"poll[options]", vector{"Yes", "No", "Maybe"}}, - * {"status", "How is the weather?"} - * }; - * @endcode - * - * @since 0.1.0 - */ -using parametermap = - map>>; - -/*! - * @brief A single parameter of a parametermap. - * - * @since 0.1.0 - */ -using parameterpair = - pair>>; - /*! * @brief Handles the details of network connections. * 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 #include 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/types.hpp b/include/types.hpp new file mode 100644 index 0000000..bfd5e42 --- /dev/null +++ b/include/types.hpp @@ -0,0 +1,197 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +// Types that are used in more than one file. + +#ifndef MASTODONPP_TYPES_HPP +#define MASTODONPP_TYPES_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +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 “\@file:” followed by the file + * name as value. + * + * Example: + * @code + * parametermap parameters + * { + * {"poll[expires_in]", "86400"}, + * {"poll[options]", vector{"Yes", "No", "Maybe"}}, + * {"status", "How is the weather?"} + * }; + * @endcode + * + * @since 0.1.0 + */ +using parametermap = + map>>; + +/*! + * @brief A single parameter of a parametermap. + * + * @since 0.1.0 + */ +using parameterpair = + pair>>; + +/*! + * @brief Return type for Request%s. + * + * @since 0.1.0 + * + * @headerfile answer.hpp mastodonpp/answer.hpp + * + */ +struct answer_type +{ + /*! + * @brief The error code returned by libcurl. + * + * For more information consult + * [libcurl-errors(3)](https://curl.haxx.se/libcurl/c/libcurl-errors.html). + * + * @since 0.1.0 + */ + uint8_t curl_error_code{0}; + + /*! + * @brief The error message. + * + * @since 0.1.0 + */ + string error_message; + + /*! + * @brief HTTP status code. + * + * @since 0.1.0 + */ + uint16_t http_status{0}; + + /*! + * @brief The headers of the response from the server. + * + * @since 0.1.0 + */ + string headers; + + /*! + * @brief The response from the server, usually JSON. + * + * @since 0.1.0 + */ + string body; + + /*! + * @brief Returns true if #curl_error_code is 0 and #http_status is 200, + * false otherwise. + * + * @since 0.1.0 + */ + explicit operator bool() const; + + /*! + * @brief Returns #body as `std::string_view`. + * + * @since 0.1.0 + */ + explicit operator string_view() const; + + /*! + * @brief Returns #body as `std::ostream`. + * + * @since 0.1.0 + */ + friend ostream &operator <<(ostream &out, const answer_type &answer); + + /*! + * @brief Returns the value of a header field. + * + * Is only valid for as long as the answer_type is in scope. + * + * @param field Case insensitive, only ASCII. + * + * @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_TYPES_HPP diff --git a/src/answer.cpp b/src/answer.cpp deleted file mode 100644 index 0db9176..0000000 --- a/src/answer.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* This file is part of mastodonpp. - * Copyright © 2020 tastytea - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#include "answer.hpp" - -#include -#include - -namespace mastodonpp -{ - -using std::search; -using std::tolower; - -answer_type::operator bool() const -{ - return (curl_error_code == 0 && http_status == 200); -} - -answer_type::operator string_view() const -{ - return body; -} - -std::ostream &operator <<(std::ostream &out, const answer_type &answer) -{ - out << answer.body; - return out; -} - -string_view answer_type::get_header(const string_view field) const -{ - const string_view searchstring{string(field) += ':'}; - auto it{search(headers.begin(), headers.end(), - searchstring.begin(), searchstring.end(), - [](unsigned char a, unsigned char b) - { return tolower(a) == tolower(b); })}; - - if (it != headers.end()) - { - auto pos{static_cast(it - headers.begin())}; - pos = headers.find(':', pos) + 2; - const auto endpos{headers.find('\n', pos)}; - return string_view(&headers[pos], endpos - pos); - } - - return {}; -} - -} // namespace mastodonpp 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 . */ -#include "answer.hpp" #include "instance.hpp" #include "log.hpp" diff --git a/src/types.cpp b/src/types.cpp new file mode 100644 index 0000000..8c4575c --- /dev/null +++ b/src/types.cpp @@ -0,0 +1,100 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "log.hpp" +#include "types.hpp" + +#include +#include + +namespace mastodonpp +{ + +using std::search; +using std::tolower; + +answer_type::operator bool() const +{ + return (curl_error_code == 0 && http_status == 200); +} + +answer_type::operator string_view() const +{ + return body; +} + +std::ostream &operator <<(std::ostream &out, const answer_type &answer) +{ + out << answer.body; + return out; +} + +string_view answer_type::get_header(const string_view field) const +{ + const string_view searchstring{string(field) += ':'}; + auto it{search(headers.begin(), headers.end(), + searchstring.begin(), searchstring.end(), + [](unsigned char a, unsigned char b) + { return tolower(a) == tolower(b); })}; + + if (it != headers.end()) + { + auto pos{static_cast(it - headers.begin())}; + pos = headers.find(':', pos) + 2; + const auto endpos{headers.find('\n', pos)}; + return string_view(&headers[pos], endpos - pos); + } + + 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 -- cgit v1.2.3-54-g00ecf