Use Instance and Request classes.

This commit is contained in:
tastytea 2020-01-03 12:42:10 +01:00
parent 1d3275e429
commit 4c557aae6e
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 169 additions and 38 deletions

49
include/instance.hpp Normal file
View File

@ -0,0 +1,49 @@
/* 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/>.
*/
#ifndef MASTODONPP_INSTANCE_HPP
#define MASTODONPP_INSTANCE_HPP
#include <string>
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

View File

@ -17,8 +17,8 @@
#ifndef MASTODONPP_HPP
#define MASTODONPP_HPP
#include "instance.hpp"
#include "return_types.hpp"
#include <string>
/*!
* @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

36
include/request.hpp Normal file
View File

@ -0,0 +1,36 @@
/* 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/>.
*/
#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

31
src/instance.cpp Normal file
View File

@ -0,0 +1,31 @@
/* 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 "instance.hpp"
#include <utility>
namespace mastodonpp
{
using std::move;
Instance::Instance(string instance, string access_token)
: _instance{move(instance)}
, _access_token{move(access_token)}
{}
} // namespace mastodonpp

View File

@ -16,16 +16,9 @@
#include "mastodonpp.hpp"
#include <utility>
namespace mastodonpp
{
using std::move;
API::API(string instance, string access_token)
: _instance{move(instance)}
, _access_token{move(access_token)}
{}
} // namespace mastodonpp

26
src/request.cpp Normal file
View File

@ -0,0 +1,26 @@
/* 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 "request.hpp"
namespace mastodonpp
{
Request::Request(Instance &instance)
: _instance{instance}
{}
} // namespace mastodonpp

View File

@ -14,7 +14,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mastodonpp.hpp"
#include "instance.hpp"
#include "request.hpp"
#include <catch.hpp>
@ -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)
{