summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortastytea2020-01-04 09:12:54 +0100
committertastytea2020-01-04 09:29:52 +0100
commit0311b868aab14e03018950d20695a8dd68befe7b (patch)
treee721ecfcc74ff00544691feab31d5c33c39f2489
parent5f96861ea8dae0b4149a970dc29c31947a524a98 (diff)
downloadmastodonpp-0311b868aab14e03018950d20695a8dd68befe7b.tar
mastodonpp-0311b868aab14e03018950d20695a8dd68befe7b.tar.gz
mastodonpp-0311b868aab14e03018950d20695a8dd68befe7b.zip
Put API endpoints in class API.
-rw-r--r--include/api.hpp69
-rw-r--r--src/api.cpp43
2 files changed, 105 insertions, 7 deletions
diff --git a/include/api.hpp b/include/api.hpp
index d7c3fa5..6c45ab2 100644
--- a/include/api.hpp
+++ b/include/api.hpp
@@ -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
diff --git a/src/api.cpp b/src/api.cpp
new file mode 100644
index 0000000..30a11b9
--- /dev/null
+++ b/src/api.cpp
@@ -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