From 49e910ecd8d7215531ba2cc01162e090c1846260 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 3 Jan 2020 10:24:31 +0100 Subject: [PATCH] Add return type for requests. --- include/mastodonpp.hpp | 1 + include/return_types.hpp | 46 ++++++++++++++++++++++++++++++++++++++++ src/return_types.cpp | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 include/return_types.hpp create mode 100644 src/return_types.cpp diff --git a/include/mastodonpp.hpp b/include/mastodonpp.hpp index 713712d..606e61a 100644 --- a/include/mastodonpp.hpp +++ b/include/mastodonpp.hpp @@ -17,6 +17,7 @@ #ifndef MASTODONPP_HPP #define MASTODONPP_HPP +#include "return_types.hpp" #include namespace mastodonpp diff --git a/include/return_types.hpp b/include/return_types.hpp new file mode 100644 index 0000000..e9d7db8 --- /dev/null +++ b/include/return_types.hpp @@ -0,0 +1,46 @@ +/* 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_RETURN_TYPES_HPP +#define MASTODONPP_RETURN_TYPES_HPP + +#include +#include +#include + +namespace mastodonpp +{ + +using std::uint8_t; +using std::uint16_t; +using std::string; +using std::string_view; + +struct answer +{ + uint8_t error_code; + string error_message; + uint16_t http_status; + string body; + + explicit operator bool() const; + explicit operator string_view() const; + friend std::ostream &operator <<(std::ostream &out, const answer &answer); +}; + +} // namespace mastodonpp + +#endif // MASTODONPP_RETURN_TYPES_HPP diff --git a/src/return_types.cpp b/src/return_types.cpp new file mode 100644 index 0000000..6367f7c --- /dev/null +++ b/src/return_types.cpp @@ -0,0 +1,38 @@ +/* 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 "return_types.hpp" + +namespace mastodonpp +{ + +answer::operator bool() const +{ + return (error_code == 0); +} + +answer::operator string_view() const +{ + return body; +} + +std::ostream &operator <<(std::ostream &out, const answer &answer) +{ + out << answer.body; + return out; +} + +} // namespace mastodonpp