Put API endpoints in class API.
This commit is contained in:
parent
5f96861ea8
commit
0311b868aa
@ -17,21 +17,76 @@
|
||||
#ifndef MASTODONPP_API_HPP
|
||||
#define MASTODONPP_API_HPP
|
||||
|
||||
namespace mastodonpp::API
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace mastodonpp
|
||||
{
|
||||
|
||||
using std::string;
|
||||
using std::variant;
|
||||
|
||||
/*!
|
||||
* @brief A list of all v1 API calls.
|
||||
*
|
||||
* The original `/` are substituted by `_`.
|
||||
* @brief Holds API endpoints.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
enum class v1
|
||||
class API
|
||||
{
|
||||
instance
|
||||
public:
|
||||
/*!
|
||||
* @brief An enumeration of all v1 API endpoints.
|
||||
*
|
||||
* The original `/` are substituted with `_`.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
enum class v1
|
||||
{
|
||||
instance
|
||||
};
|
||||
|
||||
/*!
|
||||
* @brief An enumeration of all v2 API endpoints.
|
||||
*
|
||||
* The original `/` are substituted with `_`.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
enum class v2
|
||||
{
|
||||
search
|
||||
};
|
||||
|
||||
/*!
|
||||
* @brief Type for endpoints. Either API::v1 or API::v2.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
using endpoint_type = variant<v1,v2>;
|
||||
|
||||
/*!
|
||||
* @brief Constructs an API object. You should never need this.
|
||||
*
|
||||
* This constructor exists to hide away the class members, which are used
|
||||
* internally.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
explicit API(endpoint_type &endpoint);
|
||||
|
||||
/*!
|
||||
* @brief Convert #endpoint_type to string.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
string to_string() const;
|
||||
|
||||
private:
|
||||
const endpoint_type _endpoint;
|
||||
};
|
||||
|
||||
} // namespace mastodonpp::API
|
||||
} // namespace mastodonpp
|
||||
|
||||
#endif // MASTODONPP_API_HPP
|
||||
|
43
src/api.cpp
Normal file
43
src/api.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/* This file is part of mastodonpp.
|
||||
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "api.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace mastodonpp
|
||||
{
|
||||
|
||||
using std::map;
|
||||
using std::string_view;
|
||||
using std::move;
|
||||
|
||||
API::API(endpoint_type &endpoint)
|
||||
: _endpoint{move(endpoint)}
|
||||
{}
|
||||
|
||||
string API::to_string() const
|
||||
{
|
||||
static const map<endpoint_type,string_view> endpoint_map
|
||||
{
|
||||
{v1::instance, "/api/v1/instance"},
|
||||
{v2::search, "/api/v2/search"}
|
||||
};
|
||||
return endpoint_map.at(_endpoint).data();
|
||||
}
|
||||
} // namespace mastodonpp
|
Loading…
x
Reference in New Issue
Block a user