From 4c557aae6eff2023aadfc8f0cd393bbbf35096d5 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 3 Jan 2020 12:42:10 +0100 Subject: Use Instance and Request classes. --- include/instance.hpp | 49 ++++++++++++++++++++++++++++++++++++++++++++ include/mastodonpp.hpp | 31 ++++------------------------ include/request.hpp | 36 ++++++++++++++++++++++++++++++++ src/instance.cpp | 31 ++++++++++++++++++++++++++++ src/mastodonpp.cpp | 7 ------- src/request.cpp | 26 +++++++++++++++++++++++ tests/test_instanciation.cpp | 27 ++++++++++++++++++++---- 7 files changed, 169 insertions(+), 38 deletions(-) create mode 100644 include/instance.hpp create mode 100644 include/request.hpp create mode 100644 src/instance.cpp create mode 100644 src/request.cpp diff --git a/include/instance.hpp b/include/instance.hpp new file mode 100644 index 0000000..c7be1cd --- /dev/null +++ b/include/instance.hpp @@ -0,0 +1,49 @@ +/* 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_INSTANCE_HPP +#define MASTODONPP_INSTANCE_HPP + +#include + +namespace mastodonpp +{ + +using std::string; + +class Instance +{ +public: + /*! + * @brief Construct a new Instance object. + * + * Holds the hostname and access token of the instance. + * + * @param instance The hostname of the instance. + * @param access_token Your access token. + * + * @since 0.1.0 + */ + explicit Instance(string instance, string access_token); + +private: + const string _instance; + string _access_token; +}; + +} // namespace mastodonpp + +#endif // MASTODONPP_INSTANCE_HPP diff --git a/include/mastodonpp.hpp b/include/mastodonpp.hpp index 080d4b2..0b7afc4 100644 --- a/include/mastodonpp.hpp +++ b/include/mastodonpp.hpp @@ -17,8 +17,8 @@ #ifndef MASTODONPP_HPP #define MASTODONPP_HPP +#include "instance.hpp" #include "return_types.hpp" -#include /*! * @mainpage mastodonpp Reference @@ -43,44 +43,21 @@ * @section Example * * @code - * mastodonpp::API masto("example.com", ""); + * mastodonpp::Instance instance{"example.com", ""}; * @endcode */ -namespace mastodonpp -{ - -using std::string; - /*! * @brief C++ wrapper for the Mastodon API. * * All text input is expected to be UTF-8. * * @since 0.1.0 - * */ - -class API +namespace mastodonpp { -public: - /*! - * @brief Construct a new API object. - * - * To register your application, leave access_token blank and call - * API::register_app1() and API::register_app2(). - * - * @param instance The hostname of your instance. - * @param access_token Your access token. - * - * @since 0.1.0 - */ - explicit API(string instance, string access_token); -private: - const string _instance; - const string _access_token; -}; + } // namespace mastodonpp diff --git a/include/request.hpp b/include/request.hpp new file mode 100644 index 0000000..97aa2ec --- /dev/null +++ b/include/request.hpp @@ -0,0 +1,36 @@ +/* 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_REQUEST_HPP +#define MASTODONPP_REQUEST_HPP + +#include "instance.hpp" + +namespace mastodonpp +{ + +class Request +{ +public: + explicit Request(Instance &instance); + +private: + Instance &_instance; +}; + +} // namespace mastodonpp + +#endif // MASTODONPP_REQUEST_HPP diff --git a/src/instance.cpp b/src/instance.cpp new file mode 100644 index 0000000..6b3443d --- /dev/null +++ b/src/instance.cpp @@ -0,0 +1,31 @@ +/* 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 "instance.hpp" + +#include + +namespace mastodonpp +{ + +using std::move; + +Instance::Instance(string instance, string access_token) + : _instance{move(instance)} + , _access_token{move(access_token)} +{} + +} // namespace mastodonpp diff --git a/src/mastodonpp.cpp b/src/mastodonpp.cpp index 7890ed6..99f2b27 100644 --- a/src/mastodonpp.cpp +++ b/src/mastodonpp.cpp @@ -16,16 +16,9 @@ #include "mastodonpp.hpp" -#include - namespace mastodonpp { -using std::move; -API::API(string instance, string access_token) - : _instance{move(instance)} - , _access_token{move(access_token)} -{} } // namespace mastodonpp diff --git a/src/request.cpp b/src/request.cpp new file mode 100644 index 0000000..26056fc --- /dev/null +++ b/src/request.cpp @@ -0,0 +1,26 @@ +/* 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 "request.hpp" + +namespace mastodonpp +{ + +Request::Request(Instance &instance) + : _instance{instance} +{} + +} // namespace mastodonpp diff --git a/tests/test_instanciation.cpp b/tests/test_instanciation.cpp index 3fbc0f3..3bd73ec 100644 --- a/tests/test_instanciation.cpp +++ b/tests/test_instanciation.cpp @@ -14,7 +14,8 @@ * along with this program. If not, see . */ -#include "mastodonpp.hpp" +#include "instance.hpp" +#include "request.hpp" #include @@ -26,15 +27,33 @@ namespace mastodonpp using std::string; -SCENARIO ("API can be instantiated.") +SCENARIO ("Instantiations.") { bool exception = false; - GIVEN ("One instanciation.") + WHEN ("Instance is instantiated.") { try { - API masto("example.com", ""); + Instance instance{"example.com", ""}; + } + catch (const std::exception &e) + { + exception = true; + } + + THEN ("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + } + + WHEN ("Request is instantiated.") + { + try + { + Instance instance{"example.com", ""}; + Request request{instance}; } catch (const std::exception &e) { -- cgit v1.2.3-54-g00ecf