Added Easy::get_link()

This commit is contained in:
tastytea 2018-04-03 00:04:47 +02:00
parent c74f3b9491
commit fe7c20fe9e
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
4 changed files with 74 additions and 10 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.8.0
VERSION 0.8.1
LANGUAGES CXX
)

View File

@ -76,3 +76,36 @@ const std::vector<Easy::stream_event>
return vec;
}
Easy::Link::Link(const string &link_header)
: _next(0)
, _prev(0)
{
std::regex renext("max_id=([[:digit:]]*)");
std::regex reprev("since_id=([[:digit:]]*)");
std::smatch match;
if (std::regex_search(link_header, match, renext))
{
_next = std::stoull(match[1].str());
}
if (std::regex_search(link_header, match, reprev))
{
_prev = std::stoull(match[1].str());
}
}
const uint_fast64_t Easy::Link::next() const
{
return _next;
}
const uint_fast64_t Easy::Link::prev() const
{
return _prev;
}
const Easy::Link Easy::get_link() const
{
return Link(get_header("Link"));
}

View File

@ -104,6 +104,31 @@ public:
typedef std::pair<event_type, string> stream_event;
/*!
* @brief Class to hold the `Link`-header.
*
* Extracts max_id and since_id from the `Link:`-header
*/
class Link
{
public:
explicit Link(const string &link_header);
/*!
* @brief Returns max_id
*/
const uint_fast64_t next() const;
/*!
* @brief Returns since_id
*/
const uint_fast64_t prev() const;
private:
uint_fast64_t _next;
uint_fast64_t _prev;
};
/*!
* @brief Constructs a new Easy object.
*
@ -134,6 +159,11 @@ public:
static const std::vector<stream_event>
parse_stream(const std::string &streamdata);
/*!
* @brief Gets the links from the last answer
*/
const Link get_link() const;
/*!
* @brief Base class for all entities.
*/

View File

@ -22,6 +22,7 @@
#include <cstdint>
#include <map>
#include <memory>
#include <array>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
@ -332,6 +333,15 @@ public:
const string &code,
string &access_token);
/*!
* @brief Gets the header from the last answer.
*
* @param header The header to get
*
* @return The header, or "" on error.
*/
const string get_header(const string &header) const;
/*!
* @brief Make a GET request which doesn't require parameters.
*
@ -638,15 +648,6 @@ public:
const parametermap &parameters,
string &answer);
/*!
* @brief Gets the header from the last answer.
*
* @param header The header to get
*
* @return The header, or "" on error.
*/
const string get_header(const string &header) const;
private:
const string _instance;
string _access_token;