Move types into types.hpp and add answer_type::next() and prev().

This commit is contained in:
tastytea 2020-01-14 20:44:08 +01:00
parent bd490e788c
commit a8e342202d
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 124 additions and 47 deletions

View File

@ -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>

View File

@ -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.
@ -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 <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.
*

View File

@ -18,7 +18,7 @@
#define MASTODONPP_INSTANCE_HPP
#include "curl_wrapper.hpp"
#include "answer.hpp"
#include "types.hpp"
#include <cstdint>
#include <string>

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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