From 19b868eb239b13e59850d0c0932485d3cff1dfd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Wed, 22 Nov 2023 20:19:54 +0100 Subject: [PATCH 01/24] Move IConnection to a common namespace --- include/{github_api => cpp_restapi}/iconnection.hpp | 5 ++++- include/github_api/github_api_curl.hpp | 6 +++--- include/github_api/github_api_qt.hpp | 6 +++--- include/github_api/igithub_api.hpp | 4 ++-- include/github_api/request.hpp | 7 +++---- src/base_connection.hpp | 4 ++-- src/curl_backend/github_api.cpp | 6 +++--- src/qt_backend/github_api.cpp | 8 ++++---- src/request.cpp | 3 +-- 9 files changed, 25 insertions(+), 24 deletions(-) rename include/{github_api => cpp_restapi}/iconnection.hpp (81%) diff --git a/include/github_api/iconnection.hpp b/include/cpp_restapi/iconnection.hpp similarity index 81% rename from include/github_api/iconnection.hpp rename to include/cpp_restapi/iconnection.hpp index cb4617a..209e2ad 100644 --- a/include/github_api/iconnection.hpp +++ b/include/cpp_restapi/iconnection.hpp @@ -4,8 +4,11 @@ #include -namespace GitHub +namespace cpp_restapi { + /** + * @brief Interface representing connection with rest api server + */ struct IConnection { public: diff --git a/include/github_api/github_api_curl.hpp b/include/github_api/github_api_curl.hpp index d6e99f9..3eb7b56 100644 --- a/include/github_api/github_api_curl.hpp +++ b/include/github_api/github_api_curl.hpp @@ -4,7 +4,7 @@ #include -#include "iconnection.hpp" +#include "cpp_restapi/iconnection.hpp" #include "igithub_api.hpp" #include "github_api_export.h" @@ -20,8 +20,8 @@ namespace GitHub { namespace CurlBackend Api(const Api &) = delete; ~Api(); - std::unique_ptr connect() override; - std::unique_ptr connect(const std::string& token) override; + std::unique_ptr connect() override; + std::unique_ptr connect(const std::string& token) override; std::string address() const override; Api& operator=(const Api &) = delete; diff --git a/include/github_api/github_api_qt.hpp b/include/github_api/github_api_qt.hpp index 1674fe9..188e0c9 100644 --- a/include/github_api/github_api_qt.hpp +++ b/include/github_api/github_api_qt.hpp @@ -10,7 +10,7 @@ #include -#include "iconnection.hpp" +#include "cpp_restapi/iconnection.hpp" #include "igithub_api.hpp" #include "github_api_export.h" @@ -28,8 +28,8 @@ namespace GitHub { namespace QtBackend Api(QNetworkAccessManager &, const QString& addr = "https://api.github.com"); Api(const Api &) = delete; - std::unique_ptr connect() override; - std::unique_ptr connect(const std::string& token) override; + std::unique_ptr connect() override; + std::unique_ptr connect(const std::string& token) override; std::string address() const override; Api& operator=(const Api &) = delete; diff --git a/include/github_api/igithub_api.hpp b/include/github_api/igithub_api.hpp index 01e9753..2e0385c 100644 --- a/include/github_api/igithub_api.hpp +++ b/include/github_api/igithub_api.hpp @@ -19,14 +19,14 @@ class IApi * @brief open anonymous connection with GitHub api * @return \ref GitHub::IConnection object which can be used with \ref GitHub::Request */ - virtual std::unique_ptr connect() = 0; + virtual std::unique_ptr connect() = 0; /** * @brief open authorized connection with GitHub api * @param token GitHub's authentication token. One can be generated on https://github.com/settings/tokens * @return \ref GitHub::IConnection object which can be used with \ref GitHub::Request */ - virtual std::unique_ptr connect(const std::string& token) = 0; + virtual std::unique_ptr connect(const std::string& token) = 0; /** * @return GitHub api address diff --git a/include/github_api/request.hpp b/include/github_api/request.hpp index e972e36..603babc 100644 --- a/include/github_api/request.hpp +++ b/include/github_api/request.hpp @@ -5,13 +5,12 @@ #include #include +#include #include "github_api_export.h" namespace GitHub { - struct IConnection; - /** * @brief GitHub api actions. * @@ -27,7 +26,7 @@ namespace GitHub class GITHUB_API_EXPORT Request { public: - Request(std::unique_ptr); + Request(std::unique_ptr); Request(const Request &) = delete; ~Request(); @@ -506,7 +505,7 @@ namespace GitHub std::string repoContributors(const std::string& owner, const std::string& repo); private: - std::unique_ptr m_connection; + std::unique_ptr m_connection; std::string doRequest(const std::string &); }; diff --git a/src/base_connection.hpp b/src/base_connection.hpp index b94cb13..ec11c24 100644 --- a/src/base_connection.hpp +++ b/src/base_connection.hpp @@ -2,11 +2,11 @@ #ifndef BASE_CONNECTION_HPP_INCLUDED #define BASE_CONNECTION_HPP_INCLUDED -#include +#include namespace GitHub { - class BaseConnection: public GitHub::IConnection + class BaseConnection: public cpp_restapi::IConnection { public: explicit BaseConnection(const std::string& address, const std::string& token); diff --git a/src/curl_backend/github_api.cpp b/src/curl_backend/github_api.cpp index 94ae379..8b7ac10 100644 --- a/src/curl_backend/github_api.cpp +++ b/src/curl_backend/github_api.cpp @@ -20,15 +20,15 @@ Api::~Api() } -std::unique_ptr Api::connect() +std::unique_ptr Api::connect() { return connect(std::string()); } -std::unique_ptr Api::connect(const std::string& token) +std::unique_ptr Api::connect(const std::string& token) { - return std::unique_ptr(new Connection(m_addres, token)); + return std::unique_ptr(new Connection(m_addres, token)); } diff --git a/src/qt_backend/github_api.cpp b/src/qt_backend/github_api.cpp index 00ae046..66345bf 100644 --- a/src/qt_backend/github_api.cpp +++ b/src/qt_backend/github_api.cpp @@ -12,17 +12,17 @@ Api::Api(QNetworkAccessManager& manager, const QString& addr): m_manager(manager } -std::unique_ptr Api::connect() +std::unique_ptr Api::connect() { - std::unique_ptr result(new Connection(m_manager, m_addres, "")); + std::unique_ptr result(new Connection(m_manager, m_addres, "")); return result; } -std::unique_ptr Api::connect(const std::string& token) +std::unique_ptr Api::connect(const std::string& token) { - std::unique_ptr result(new Connection(m_manager, m_addres, token.c_str())); + std::unique_ptr result(new Connection(m_manager, m_addres, token.c_str())); return result; } diff --git a/src/request.cpp b/src/request.cpp index ae1a5fa..2f2365d 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -1,6 +1,5 @@ #include -#include // Based on: @@ -11,7 +10,7 @@ namespace GitHub { - Request::Request(std::unique_ptr connection) + Request::Request(std::unique_ptr connection) : m_connection(std::move(connection)) { } From c619aacc86ea01c87605557bf6d3d1a8e8908dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Wed, 22 Nov 2023 21:32:48 +0100 Subject: [PATCH 02/24] Set c++ standard and remove hpp files from target --- CMakeLists.txt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f659ae3..b3a7809 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,9 @@ else() pkg_check_modules(JSONCPP REQUIRED jsoncpp) endif() +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + include(GenerateExportHeader) option(GitHubAPI_QtBackend "Qt backend for GitHubApi" OFF) @@ -28,13 +31,8 @@ if(GitHubAPI_Tests) endif() add_library(github_api - include/github_api/iconnection.hpp - include/github_api/igithub_api.hpp - include/github_api/request.hpp src/base_connection.cpp - src/base_connection.hpp src/header_utils.cpp - src/header_utils.hpp src/request.cpp ) From c8c4f74cd09a423b4d7f216bd764769916e7e003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Wed, 22 Nov 2023 21:33:10 +0100 Subject: [PATCH 03/24] Move header entries generation to base connection --- src/base_connection.cpp | 13 +++++++++---- src/base_connection.hpp | 6 +++++- src/curl_backend/connection.cpp | 13 +++++++------ src/qt_backend/connection.cpp | 11 ++++------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/base_connection.cpp b/src/base_connection.cpp index bff2702..1622472 100644 --- a/src/base_connection.cpp +++ b/src/base_connection.cpp @@ -66,13 +66,18 @@ std::string GitHub::BaseConnection::get(const std::string& request) } -const std::string & GitHub::BaseConnection::address() const +std::map GitHub::BaseConnection::getHeaderEntries() const { - return m_address; + std::map entries; + + if (m_token.empty() == false) + entries.emplace("Authorization", "token " + m_token); + + return entries; } -const std::string & GitHub::BaseConnection::token() const +const std::string & GitHub::BaseConnection::address() const { - return m_token; + return m_address; } diff --git a/src/base_connection.hpp b/src/base_connection.hpp index ec11c24..2e24b91 100644 --- a/src/base_connection.hpp +++ b/src/base_connection.hpp @@ -2,8 +2,11 @@ #ifndef BASE_CONNECTION_HPP_INCLUDED #define BASE_CONNECTION_HPP_INCLUDED +#include + #include + namespace GitHub { class BaseConnection: public cpp_restapi::IConnection @@ -15,8 +18,9 @@ namespace GitHub virtual std::pair fetchPage(const std::string& request) = 0; protected: + std::map getHeaderEntries() const; + const std::string& address() const; - const std::string& token() const; private: const std::string m_address; diff --git a/src/curl_backend/connection.cpp b/src/curl_backend/connection.cpp index f216d7e..8b7f916 100644 --- a/src/curl_backend/connection.cpp +++ b/src/curl_backend/connection.cpp @@ -74,15 +74,16 @@ std::pair GitHub::CurlBackend::Connection::fetchPage(c curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_links); curl_easy_setopt(curl, CURLOPT_USERAGENT, "github_api/1.0"); - const std::string& userToken = token(); - if (userToken.empty() == false) - { - authorization = std::string("Authorization: token ") + userToken; - list = curl_slist_append(list, authorization.c_str()); + const auto header_entries = getHeaderEntries(); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); + for(const auto& [k, v]: header_entries) + { + const std::string entry = k + ": " + v; + list = curl_slist_append(list, entry.c_str()); } + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); + curl_easy_perform(curl); curl_slist_free_all(list); diff --git a/src/qt_backend/connection.cpp b/src/qt_backend/connection.cpp index c8e6a83..bf3d63d 100644 --- a/src/qt_backend/connection.cpp +++ b/src/qt_backend/connection.cpp @@ -82,13 +82,10 @@ namespace GitHub { namespace QtBackend { QNetworkRequest request; - const std::string& userToken = token(); - if (userToken.empty() == false) - { - const QByteArray key("Authorization"); - const QByteArray value = QString("token %1").arg(userToken.c_str()).toLatin1(); - request.setRawHeader(key, value); - } + const auto header_entries = getHeaderEntries(); + + for(const auto& [k, v]: header_entries) + request.setRawHeader(k.c_str(), v.c_str()); request.setRawHeader("User-Agent", "github_api/1.0"); From aeb4c180436df3130c0f38036400f3b8da8f7fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Thu, 23 Nov 2023 21:07:07 +0100 Subject: [PATCH 04/24] Move connections to cpp_restapi namespace --- src/base_connection.cpp | 8 ++++---- src/base_connection.hpp | 2 +- src/curl_backend/connection.cpp | 6 +++--- src/curl_backend/connection.hpp | 2 +- src/curl_backend/github_api.cpp | 2 +- src/qt_backend/connection.cpp | 2 +- src/qt_backend/connection.hpp | 2 +- src/qt_backend/github_api.cpp | 4 ++-- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/base_connection.cpp b/src/base_connection.cpp index 1622472..f1dada7 100644 --- a/src/base_connection.cpp +++ b/src/base_connection.cpp @@ -26,7 +26,7 @@ namespace } -GitHub::BaseConnection::BaseConnection(const std::string& address, const std::string& token) +cpp_restapi::BaseConnection::BaseConnection(const std::string& address, const std::string& token) : m_address(address) , m_token(token) { @@ -34,7 +34,7 @@ GitHub::BaseConnection::BaseConnection(const std::string& address, const std::st } -std::string GitHub::BaseConnection::get(const std::string& request) +std::string cpp_restapi::BaseConnection::get(const std::string& request) { std::string nextPage = m_address + "/" + request; @@ -66,7 +66,7 @@ std::string GitHub::BaseConnection::get(const std::string& request) } -std::map GitHub::BaseConnection::getHeaderEntries() const +std::map cpp_restapi::BaseConnection::getHeaderEntries() const { std::map entries; @@ -77,7 +77,7 @@ std::map GitHub::BaseConnection::getHeaderEntries() co } -const std::string & GitHub::BaseConnection::address() const +const std::string & cpp_restapi::BaseConnection::address() const { return m_address; } diff --git a/src/base_connection.hpp b/src/base_connection.hpp index 2e24b91..5a57cc9 100644 --- a/src/base_connection.hpp +++ b/src/base_connection.hpp @@ -7,7 +7,7 @@ #include -namespace GitHub +namespace cpp_restapi { class BaseConnection: public cpp_restapi::IConnection { diff --git a/src/curl_backend/connection.cpp b/src/curl_backend/connection.cpp index 8b7f916..5985679 100644 --- a/src/curl_backend/connection.cpp +++ b/src/curl_backend/connection.cpp @@ -14,7 +14,7 @@ #include "connection.hpp" -GitHub::CurlBackend::Connection::Connection(const std::string& address, +cpp_restapi::CurlBackend::Connection::Connection(const std::string& address, const std::string& token) : BaseConnection(address, token) { @@ -22,13 +22,13 @@ GitHub::CurlBackend::Connection::Connection(const std::string& address, } -GitHub::CurlBackend::Connection::~Connection() +cpp_restapi::CurlBackend::Connection::~Connection() { } -std::pair GitHub::CurlBackend::Connection::fetchPage(const std::string& page) +std::pair cpp_restapi::CurlBackend::Connection::fetchPage(const std::string& page) { std::string result; std::string header_links; diff --git a/src/curl_backend/connection.hpp b/src/curl_backend/connection.hpp index f9edfca..7c1c97f 100644 --- a/src/curl_backend/connection.hpp +++ b/src/curl_backend/connection.hpp @@ -5,7 +5,7 @@ #include "base_connection.hpp" -namespace GitHub { namespace CurlBackend { +namespace cpp_restapi { namespace CurlBackend { class Connection: public BaseConnection { diff --git a/src/curl_backend/github_api.cpp b/src/curl_backend/github_api.cpp index 8b7ac10..d0013a3 100644 --- a/src/curl_backend/github_api.cpp +++ b/src/curl_backend/github_api.cpp @@ -28,7 +28,7 @@ std::unique_ptr Api::connect() std::unique_ptr Api::connect(const std::string& token) { - return std::unique_ptr(new Connection(m_addres, token)); + return std::unique_ptr(new cpp_restapi::CurlBackend::Connection(m_addres, token)); } diff --git a/src/qt_backend/connection.cpp b/src/qt_backend/connection.cpp index bf3d63d..5ab3924 100644 --- a/src/qt_backend/connection.cpp +++ b/src/qt_backend/connection.cpp @@ -10,7 +10,7 @@ #include -namespace GitHub { namespace QtBackend +namespace cpp_restapi { namespace QtBackend { Connection::Connection(QNetworkAccessManager& manager, const QString& address, const QString& token) : BaseConnection(address.toStdString(), token.toStdString()) diff --git a/src/qt_backend/connection.hpp b/src/qt_backend/connection.hpp index c2eaa4f..0b0deb7 100644 --- a/src/qt_backend/connection.hpp +++ b/src/qt_backend/connection.hpp @@ -11,7 +11,7 @@ class QNetworkAccessManager; -namespace GitHub { namespace QtBackend { +namespace cpp_restapi { namespace QtBackend { class Connection: public QObject, public BaseConnection { diff --git a/src/qt_backend/github_api.cpp b/src/qt_backend/github_api.cpp index 66345bf..c200769 100644 --- a/src/qt_backend/github_api.cpp +++ b/src/qt_backend/github_api.cpp @@ -14,7 +14,7 @@ Api::Api(QNetworkAccessManager& manager, const QString& addr): m_manager(manager std::unique_ptr Api::connect() { - std::unique_ptr result(new Connection(m_manager, m_addres, "")); + std::unique_ptr result(new cpp_restapi::QtBackend::Connection(m_manager, m_addres, "")); return result; } @@ -22,7 +22,7 @@ std::unique_ptr Api::connect() std::unique_ptr Api::connect(const std::string& token) { - std::unique_ptr result(new Connection(m_manager, m_addres, token.c_str())); + std::unique_ptr result(new cpp_restapi::QtBackend::Connection(m_manager, m_addres, token.c_str())); return result; } From c1a33d1264074fa1678d57c5c263f01357f55fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Thu, 23 Nov 2023 21:08:21 +0100 Subject: [PATCH 05/24] Simplify namespaces --- src/curl_backend/connection.cpp | 132 ++++++++++++++++---------------- src/qt_backend/connection.cpp | 4 +- 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/src/curl_backend/connection.cpp b/src/curl_backend/connection.cpp index 5985679..fded491 100644 --- a/src/curl_backend/connection.cpp +++ b/src/curl_backend/connection.cpp @@ -13,83 +13,85 @@ #include "connection.hpp" - -cpp_restapi::CurlBackend::Connection::Connection(const std::string& address, - const std::string& token) - : BaseConnection(address, token) -{ - -} - - -cpp_restapi::CurlBackend::Connection::~Connection() +namespace cpp_restapi::CurlBackend { + Connection::Connection(const std::string& address, + const std::string& token) + : BaseConnection(address, token) + { -} - - -std::pair cpp_restapi::CurlBackend::Connection::fetchPage(const std::string& page) -{ - std::string result; - std::string header_links; + } - CURL* curl = curl_easy_init(); - if (curl) + Connection::~Connection() { - curl_slist *list = nullptr; - std::string authorization; - - typedef size_t (*WriteCallback)(char *ptr, size_t size, size_t nmemb, void *userdata); - typedef size_t (*HeaderCallback)(char *buffer, size_t size, size_t nitems, void *userdata); - WriteCallback write_callback = [](char *ptr, size_t size, size_t nmemb, void* result_raw) - { - assert(size == 1); - - std::string& result = *static_cast(result_raw); - std::copy(ptr, ((ptr + nmemb)), std::back_inserter(result)); - - return nmemb; - }; - /** - * @brief This is used as a callback that receives - * header data, the header data is used for pagination. - * see the following link for more info - * https://curl.se/libcurl/c/CURLOPT_HEADERFUNCTION.html - */ - HeaderCallback header_callback = [](char *buffer, size_t size,size_t nitems, void *userdata) - { - std::string& header_links = *static_cast(userdata); - std::copy(buffer, buffer+nitems, std::back_inserter(header_links)); - return (size * nitems); - }; + } - // const std::string full_addr = m_address + "/" + request; - curl_easy_setopt(curl, CURLOPT_URL, page.c_str()); - curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result); - curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_links); - curl_easy_setopt(curl, CURLOPT_USERAGENT, "github_api/1.0"); + std::pair cpp_restapi::CurlBackend::Connection::fetchPage(const std::string& page) + { + std::string result; + std::string header_links; - const auto header_entries = getHeaderEntries(); + CURL* curl = curl_easy_init(); - for(const auto& [k, v]: header_entries) + if (curl) { - const std::string entry = k + ": " + v; - list = curl_slist_append(list, entry.c_str()); + curl_slist *list = nullptr; + std::string authorization; + + typedef size_t (*WriteCallback)(char *ptr, size_t size, size_t nmemb, void *userdata); + typedef size_t (*HeaderCallback)(char *buffer, size_t size, size_t nitems, void *userdata); + WriteCallback write_callback = [](char *ptr, size_t size, size_t nmemb, void* result_raw) + { + assert(size == 1); + + std::string& result = *static_cast(result_raw); + std::copy(ptr, ((ptr + nmemb)), std::back_inserter(result)); + + return nmemb; + }; + /** + * @brief This is used as a callback that receives + * header data, the header data is used for pagination. + * see the following link for more info + * https://curl.se/libcurl/c/CURLOPT_HEADERFUNCTION.html + */ + HeaderCallback header_callback = [](char *buffer, size_t size,size_t nitems, void *userdata) + { + std::string& header_links = *static_cast(userdata); + std::copy(buffer, buffer+nitems, std::back_inserter(header_links)); + + return (size * nitems); + }; + + // const std::string full_addr = m_address + "/" + request; + + curl_easy_setopt(curl, CURLOPT_URL, page.c_str()); + curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result); + curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_links); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "github_api/1.0"); + + const auto header_entries = getHeaderEntries(); + + for(const auto& [k, v]: header_entries) + { + const std::string entry = k + ": " + v; + list = curl_slist_append(list, entry.c_str()); + } + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); + + curl_easy_perform(curl); + + curl_slist_free_all(list); + curl_easy_cleanup(curl); } - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - - curl_easy_perform(curl); - - curl_slist_free_all(list); - curl_easy_cleanup(curl); + curl_global_cleanup(); + return std::make_pair(result, header_links); } - - curl_global_cleanup(); - return std::make_pair(result, header_links); } diff --git a/src/qt_backend/connection.cpp b/src/qt_backend/connection.cpp index 5ab3924..d05bf65 100644 --- a/src/qt_backend/connection.cpp +++ b/src/qt_backend/connection.cpp @@ -10,7 +10,7 @@ #include -namespace cpp_restapi { namespace QtBackend +namespace cpp_restapi::QtBackend { Connection::Connection(QNetworkAccessManager& manager, const QString& address, const QString& token) : BaseConnection(address.toStdString(), token.toStdString()) @@ -91,4 +91,4 @@ namespace cpp_restapi { namespace QtBackend return request; } -}} +} From 62e6dd6e337ce398ef775fddf00834da96d2050c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Thu, 23 Nov 2023 21:26:23 +0100 Subject: [PATCH 06/24] Format --- src/curl_backend/connection.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/curl_backend/connection.cpp b/src/curl_backend/connection.cpp index fded491..de5e343 100644 --- a/src/curl_backend/connection.cpp +++ b/src/curl_backend/connection.cpp @@ -15,8 +15,7 @@ namespace cpp_restapi::CurlBackend { - Connection::Connection(const std::string& address, - const std::string& token) + Connection::Connection(const std::string& address, const std::string& token) : BaseConnection(address, token) { From 304222b628e8fc62fe31d0d4f51948f6a768185c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 17:40:24 +0100 Subject: [PATCH 07/24] Make BaseConnection more generic --- src/base_connection.cpp | 18 ++++++++++-------- src/base_connection.hpp | 10 +++++++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/base_connection.cpp b/src/base_connection.cpp index f1dada7..c454c5e 100644 --- a/src/base_connection.cpp +++ b/src/base_connection.cpp @@ -28,7 +28,14 @@ namespace cpp_restapi::BaseConnection::BaseConnection(const std::string& address, const std::string& token) : m_address(address) - , m_token(token) +{ + if (token.empty() == false) + m_headerEntries.emplace("Authorization", "token " + token); +} + +cpp_restapi::BaseConnection::BaseConnection(const std::string& address, const std::map& headerEntries) + : m_address(address) + , m_headerEntries(headerEntries) { } @@ -66,14 +73,9 @@ std::string cpp_restapi::BaseConnection::get(const std::string& request) } -std::map cpp_restapi::BaseConnection::getHeaderEntries() const +const std::map& cpp_restapi::BaseConnection::getHeaderEntries() const { - std::map entries; - - if (m_token.empty() == false) - entries.emplace("Authorization", "token " + m_token); - - return entries; + return m_headerEntries; } diff --git a/src/base_connection.hpp b/src/base_connection.hpp index 5a57cc9..ddf2d1f 100644 --- a/src/base_connection.hpp +++ b/src/base_connection.hpp @@ -9,22 +9,26 @@ namespace cpp_restapi { + /** + * @brief base class with common parts for backend specific implementations + */ class BaseConnection: public cpp_restapi::IConnection { public: - explicit BaseConnection(const std::string& address, const std::string& token); + [[deprecated]] explicit BaseConnection(const std::string& address, const std::string& token); + explicit BaseConnection(const std::string& address, const std::map& headerEntries); std::string get(const std::string &) final; virtual std::pair fetchPage(const std::string& request) = 0; protected: - std::map getHeaderEntries() const; + const std::map& getHeaderEntries() const; const std::string& address() const; private: const std::string m_address; - const std::string m_token; + std::map m_headerEntries; }; } From b37ee1fc23efed6479c63b4d8ef9adbd03ca6748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 17:41:23 +0100 Subject: [PATCH 08/24] Extract namespace --- src/base_connection.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/base_connection.cpp b/src/base_connection.cpp index c454c5e..800c0c2 100644 --- a/src/base_connection.cpp +++ b/src/base_connection.cpp @@ -25,15 +25,17 @@ namespace } } +namespace cpp_restapi +{ -cpp_restapi::BaseConnection::BaseConnection(const std::string& address, const std::string& token) +BaseConnection::BaseConnection(const std::string& address, const std::string& token) : m_address(address) { if (token.empty() == false) m_headerEntries.emplace("Authorization", "token " + token); } -cpp_restapi::BaseConnection::BaseConnection(const std::string& address, const std::map& headerEntries) +BaseConnection::BaseConnection(const std::string& address, const std::map& headerEntries) : m_address(address) , m_headerEntries(headerEntries) { @@ -41,7 +43,7 @@ cpp_restapi::BaseConnection::BaseConnection(const std::string& address, const st } -std::string cpp_restapi::BaseConnection::get(const std::string& request) +std::string BaseConnection::get(const std::string& request) { std::string nextPage = m_address + "/" + request; @@ -73,13 +75,15 @@ std::string cpp_restapi::BaseConnection::get(const std::string& request) } -const std::map& cpp_restapi::BaseConnection::getHeaderEntries() const +const std::map& BaseConnection::getHeaderEntries() const { return m_headerEntries; } -const std::string & cpp_restapi::BaseConnection::address() const +const std::string & BaseConnection::address() const { return m_address; } + +} From ad1b3e4d16a208a1bfb2482166776b6b831e2085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 17:44:14 +0100 Subject: [PATCH 09/24] Deprecate existing connection constructors --- src/curl_backend/connection.hpp | 2 +- src/qt_backend/connection.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/curl_backend/connection.hpp b/src/curl_backend/connection.hpp index 7c1c97f..7edb0bd 100644 --- a/src/curl_backend/connection.hpp +++ b/src/curl_backend/connection.hpp @@ -10,7 +10,7 @@ namespace cpp_restapi { namespace CurlBackend { class Connection: public BaseConnection { public: - Connection(const std::string& address, const std::string& token); + [[deprecated]] Connection(const std::string& address, const std::string& token); Connection(const Connection &) = delete; ~Connection(); diff --git a/src/qt_backend/connection.hpp b/src/qt_backend/connection.hpp index 0b0deb7..77d87dd 100644 --- a/src/qt_backend/connection.hpp +++ b/src/qt_backend/connection.hpp @@ -16,7 +16,7 @@ namespace cpp_restapi { namespace QtBackend { class Connection: public QObject, public BaseConnection { public: - Connection(QNetworkAccessManager &, const QString& address, const QString& token); + [[deprecated]] Connection(QNetworkAccessManager &, const QString& address, const QString& token); Connection(const Connection &) = delete; ~Connection(); From 55b15f0f71ba2d16d9ad166a535da4f66f1efd5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 17:47:05 +0100 Subject: [PATCH 10/24] Move github specific implementation to dedicated directory --- CMakeLists.txt | 2 +- src/{ => services/github}/request.cpp | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{ => services/github}/request.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b3a7809..a17aceb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ endif() add_library(github_api src/base_connection.cpp src/header_utils.cpp - src/request.cpp + src/services/github/request.cpp ) target_include_directories(github_api diff --git a/src/request.cpp b/src/services/github/request.cpp similarity index 100% rename from src/request.cpp rename to src/services/github/request.cpp From 3e0c5437aa397395166a85b82edbc36b56a0012a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 17:50:43 +0100 Subject: [PATCH 11/24] Introducer new constructors for backend connections --- src/curl_backend/connection.hpp | 1 + src/qt_backend/connection.cpp | 7 +++++++ src/qt_backend/connection.hpp | 1 + 3 files changed, 9 insertions(+) diff --git a/src/curl_backend/connection.hpp b/src/curl_backend/connection.hpp index 7edb0bd..cb0bfab 100644 --- a/src/curl_backend/connection.hpp +++ b/src/curl_backend/connection.hpp @@ -11,6 +11,7 @@ namespace cpp_restapi { namespace CurlBackend { { public: [[deprecated]] Connection(const std::string& address, const std::string& token); + Connection(const std::string& address, const std::map& token); Connection(const Connection &) = delete; ~Connection(); diff --git a/src/qt_backend/connection.cpp b/src/qt_backend/connection.cpp index d05bf65..ecfab3c 100644 --- a/src/qt_backend/connection.cpp +++ b/src/qt_backend/connection.cpp @@ -19,6 +19,13 @@ namespace cpp_restapi::QtBackend } + Connection::Connection(QNetworkAccessManager& manager, const std::string& address, const std::map& headerEntries) + : BaseConnection(address, headerEntries) + , m_networkManager(manager) + { + + } + Connection::~Connection() { diff --git a/src/qt_backend/connection.hpp b/src/qt_backend/connection.hpp index 77d87dd..3d8fc84 100644 --- a/src/qt_backend/connection.hpp +++ b/src/qt_backend/connection.hpp @@ -17,6 +17,7 @@ namespace cpp_restapi { namespace QtBackend { { public: [[deprecated]] Connection(QNetworkAccessManager &, const QString& address, const QString& token); + Connection(QNetworkAccessManager &, const std::string& address, const std::map& headerEntries); Connection(const Connection &) = delete; ~Connection(); From 654b7e11384828855d48f5cf2b5cc333ab2db25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 18:16:57 +0100 Subject: [PATCH 12/24] Switch connection builders to new Connection api --- CMakeLists.txt | 1 + include/github_api/github_api_base.hpp | 23 ++++++++++++++++++++++ include/github_api/github_api_curl.hpp | 11 +++-------- include/github_api/github_api_qt.hpp | 13 +++++-------- include/github_api/igithub_api.hpp | 12 +++++------- src/curl_backend/connection.cpp | 4 ++-- src/curl_backend/connection.hpp | 3 +-- src/curl_backend/github_api.cpp | 22 ++++++++------------- src/qt_backend/connection.cpp | 7 ------- src/qt_backend/connection.hpp | 1 - src/qt_backend/github_api.cpp | 26 +++++++++---------------- src/services/github/github_api_base.cpp | 18 +++++++++++++++++ 12 files changed, 75 insertions(+), 66 deletions(-) create mode 100644 include/github_api/github_api_base.hpp create mode 100644 src/services/github/github_api_base.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a17aceb..2508b7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ endif() add_library(github_api src/base_connection.cpp src/header_utils.cpp + src/services/github/github_api_base.cpp src/services/github/request.cpp ) diff --git a/include/github_api/github_api_base.hpp b/include/github_api/github_api_base.hpp new file mode 100644 index 0000000..8971b0d --- /dev/null +++ b/include/github_api/github_api_base.hpp @@ -0,0 +1,23 @@ + +#ifndef GITHUB_API_BASE_HPP_INCLUDED +#define GITHUB_API_BASE_HPP_INCLUDED + +#include "github_api/igithub_api.hpp" + +namespace cpp_restapi +{ + + class GitHubBase: public GitHub::IApi + { + public: + GitHubBase(const std::string& address); + + const std::string& address() const override; + + private: + std::string m_address; + }; + +} + +#endif diff --git a/include/github_api/github_api_curl.hpp b/include/github_api/github_api_curl.hpp index 3eb7b56..e847083 100644 --- a/include/github_api/github_api_curl.hpp +++ b/include/github_api/github_api_curl.hpp @@ -5,7 +5,7 @@ #include #include "cpp_restapi/iconnection.hpp" -#include "igithub_api.hpp" +#include "github_api_base.hpp" #include "github_api_export.h" namespace GitHub { namespace CurlBackend @@ -13,21 +13,16 @@ namespace GitHub { namespace CurlBackend /** * @brief Class for establishing connection with GitHub api with Curl */ - class GITHUB_API_EXPORT Api: public IApi + class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase { public: Api(const std::string& addr = "https://api.github.com"); Api(const Api &) = delete; ~Api(); - std::unique_ptr connect() override; - std::unique_ptr connect(const std::string& token) override; - std::string address() const override; + std::unique_ptr connect(const std::string& token = "") override; Api& operator=(const Api &) = delete; - - private: - std::string m_addres; }; }} diff --git a/include/github_api/github_api_qt.hpp b/include/github_api/github_api_qt.hpp index 188e0c9..244bf56 100644 --- a/include/github_api/github_api_qt.hpp +++ b/include/github_api/github_api_qt.hpp @@ -11,34 +11,31 @@ #include #include "cpp_restapi/iconnection.hpp" -#include "igithub_api.hpp" +#include "github_api_base.hpp" #include "github_api_export.h" class QNetworkAccessManager; -namespace GitHub { namespace QtBackend +namespace GitHub::QtBackend { /** * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager */ - class GITHUB_API_EXPORT Api: public IApi + class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase { public: Api(QNetworkAccessManager &, const QString& addr = "https://api.github.com"); Api(const Api &) = delete; - std::unique_ptr connect() override; - std::unique_ptr connect(const std::string& token) override; - std::string address() const override; + std::unique_ptr connect(const std::string& token = "") override; Api& operator=(const Api &) = delete; private: QNetworkAccessManager& m_manager; - QString m_addres; }; -}} +} #endif diff --git a/include/github_api/igithub_api.hpp b/include/github_api/igithub_api.hpp index 2e0385c..a631d79 100644 --- a/include/github_api/igithub_api.hpp +++ b/include/github_api/igithub_api.hpp @@ -2,8 +2,12 @@ #ifndef GITHUBAPI_HPP #define GITHUBAPI_HPP +#include #include +#include + + namespace GitHub { @@ -15,12 +19,6 @@ class IApi public: virtual ~IApi() = default; - /** - * @brief open anonymous connection with GitHub api - * @return \ref GitHub::IConnection object which can be used with \ref GitHub::Request - */ - virtual std::unique_ptr connect() = 0; - /** * @brief open authorized connection with GitHub api * @param token GitHub's authentication token. One can be generated on https://github.com/settings/tokens @@ -31,7 +29,7 @@ class IApi /** * @return GitHub api address */ - virtual std::string address() const = 0; + virtual const std::string& address() const = 0; }; } diff --git a/src/curl_backend/connection.cpp b/src/curl_backend/connection.cpp index de5e343..eaea25b 100644 --- a/src/curl_backend/connection.cpp +++ b/src/curl_backend/connection.cpp @@ -15,8 +15,8 @@ namespace cpp_restapi::CurlBackend { - Connection::Connection(const std::string& address, const std::string& token) - : BaseConnection(address, token) + Connection::Connection(const std::string& address, const std::map& headerEntries) + : BaseConnection(address, headerEntries) { } diff --git a/src/curl_backend/connection.hpp b/src/curl_backend/connection.hpp index cb0bfab..c7acebd 100644 --- a/src/curl_backend/connection.hpp +++ b/src/curl_backend/connection.hpp @@ -10,8 +10,7 @@ namespace cpp_restapi { namespace CurlBackend { class Connection: public BaseConnection { public: - [[deprecated]] Connection(const std::string& address, const std::string& token); - Connection(const std::string& address, const std::map& token); + Connection(const std::string& address, const std::map& headerEntries); Connection(const Connection &) = delete; ~Connection(); diff --git a/src/curl_backend/github_api.cpp b/src/curl_backend/github_api.cpp index d0013a3..f13153d 100644 --- a/src/curl_backend/github_api.cpp +++ b/src/curl_backend/github_api.cpp @@ -5,10 +5,11 @@ #include "connection.hpp" -namespace GitHub { namespace CurlBackend { +namespace GitHub::CurlBackend +{ Api::Api(const std::string& addr) - : m_addres(addr) + : cpp_restapi::GitHubBase(addr) { curl_global_init(CURL_GLOBAL_DEFAULT); } @@ -20,21 +21,14 @@ Api::~Api() } -std::unique_ptr Api::connect() -{ - return connect(std::string()); -} - - std::unique_ptr Api::connect(const std::string& token) { - return std::unique_ptr(new cpp_restapi::CurlBackend::Connection(m_addres, token)); -} + std::map headerEntries; + if (token.empty() == false) + headerEntries.emplace("Authorization", "token " + token); -std::string Api::address() const -{ - return m_addres; + return std::make_unique(address(), headerEntries); } -}} +} diff --git a/src/qt_backend/connection.cpp b/src/qt_backend/connection.cpp index ecfab3c..e21ff53 100644 --- a/src/qt_backend/connection.cpp +++ b/src/qt_backend/connection.cpp @@ -12,13 +12,6 @@ namespace cpp_restapi::QtBackend { - Connection::Connection(QNetworkAccessManager& manager, const QString& address, const QString& token) - : BaseConnection(address.toStdString(), token.toStdString()) - , m_networkManager(manager) - { - - } - Connection::Connection(QNetworkAccessManager& manager, const std::string& address, const std::map& headerEntries) : BaseConnection(address, headerEntries) , m_networkManager(manager) diff --git a/src/qt_backend/connection.hpp b/src/qt_backend/connection.hpp index 3d8fc84..f11060f 100644 --- a/src/qt_backend/connection.hpp +++ b/src/qt_backend/connection.hpp @@ -16,7 +16,6 @@ namespace cpp_restapi { namespace QtBackend { class Connection: public QObject, public BaseConnection { public: - [[deprecated]] Connection(QNetworkAccessManager &, const QString& address, const QString& token); Connection(QNetworkAccessManager &, const std::string& address, const std::map& headerEntries); Connection(const Connection &) = delete; diff --git a/src/qt_backend/github_api.cpp b/src/qt_backend/github_api.cpp index c200769..7567f95 100644 --- a/src/qt_backend/github_api.cpp +++ b/src/qt_backend/github_api.cpp @@ -4,33 +4,25 @@ #include "connection.hpp" -namespace GitHub { namespace QtBackend { - -Api::Api(QNetworkAccessManager& manager, const QString& addr): m_manager(manager), m_addres(addr) +namespace GitHub::QtBackend { -} - - -std::unique_ptr Api::connect() +Api::Api(QNetworkAccessManager& manager, const QString& addr) + : cpp_restapi::GitHubBase(addr.toStdString()) + , m_manager(manager) { - std::unique_ptr result(new cpp_restapi::QtBackend::Connection(m_manager, m_addres, "")); - return result; } std::unique_ptr Api::connect(const std::string& token) { - std::unique_ptr result(new cpp_restapi::QtBackend::Connection(m_manager, m_addres, token.c_str())); - - return result; -} + std::map headerEntries; + if (token.empty() == false) + headerEntries.emplace("Authorization", "token " + token); -std::string Api::address() const -{ - return m_addres.toStdString(); + return std::make_unique(m_manager, address(), headerEntries); } -}} +} diff --git a/src/services/github/github_api_base.cpp b/src/services/github/github_api_base.cpp new file mode 100644 index 0000000..18212e6 --- /dev/null +++ b/src/services/github/github_api_base.cpp @@ -0,0 +1,18 @@ + +#include "github_api/github_api_base.hpp" + + +namespace cpp_restapi +{ + + GitHubBase::GitHubBase(const std::string& address) + : m_address(address) + { + + } + + const std::string& GitHubBase::address() const + { + return m_address; + } +} From 936932b38afe89d9b9eacb8e4a64d50eda950bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 13:01:09 +0100 Subject: [PATCH 13/24] Move GitHub's public includes to cpp_restapi subdir # Conflicts: # include/github_api/request.hpp --- .../github}/github_api_base.hpp | 6 +- .../cpp_restapi/github/github_api_curl.hpp | 30 + include/cpp_restapi/github/github_api_qt.hpp | 41 ++ .../github}/igithub_api.hpp | 2 +- include/cpp_restapi/github/request.hpp | 514 +++++++++++++++++ include/github_api/github_api_curl.hpp | 31 +- include/github_api/github_api_qt.hpp | 42 +- include/github_api/request.hpp | 515 +----------------- src/curl_backend/github_api.cpp | 4 +- src/qt_backend/github_api.cpp | 4 +- src/services/github/github_api_base.cpp | 2 +- src/services/github/request.cpp | 4 +- tests/api_tests.cpp | 7 +- 13 files changed, 607 insertions(+), 595 deletions(-) rename include/{github_api => cpp_restapi/github}/github_api_base.hpp (78%) create mode 100644 include/cpp_restapi/github/github_api_curl.hpp create mode 100644 include/cpp_restapi/github/github_api_qt.hpp rename include/{github_api => cpp_restapi/github}/igithub_api.hpp (96%) create mode 100644 include/cpp_restapi/github/request.hpp diff --git a/include/github_api/github_api_base.hpp b/include/cpp_restapi/github/github_api_base.hpp similarity index 78% rename from include/github_api/github_api_base.hpp rename to include/cpp_restapi/github/github_api_base.hpp index 8971b0d..aa44baf 100644 --- a/include/github_api/github_api_base.hpp +++ b/include/cpp_restapi/github/github_api_base.hpp @@ -2,12 +2,11 @@ #ifndef GITHUB_API_BASE_HPP_INCLUDED #define GITHUB_API_BASE_HPP_INCLUDED -#include "github_api/igithub_api.hpp" +#include "igithub_api.hpp" namespace cpp_restapi { - - class GitHubBase: public GitHub::IApi + class GitHubBase: public cpp_restapi::GitHub::IApi { public: GitHubBase(const std::string& address); @@ -17,7 +16,6 @@ namespace cpp_restapi private: std::string m_address; }; - } #endif diff --git a/include/cpp_restapi/github/github_api_curl.hpp b/include/cpp_restapi/github/github_api_curl.hpp new file mode 100644 index 0000000..10789fa --- /dev/null +++ b/include/cpp_restapi/github/github_api_curl.hpp @@ -0,0 +1,30 @@ + +#ifndef GITHUBAPI_CURL_HPP +#define GITHUBAPI_CURL_HPP + +#include + +#include "cpp_restapi/iconnection.hpp" +#include "github_api_base.hpp" +#include "github_api_export.h" + +namespace cpp_restapi::GitHub::CurlBackend +{ + /** + * @brief Class for establishing connection with GitHub api with Curl + */ + class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase + { + public: + Api(const std::string& addr = "https://api.github.com"); + Api(const Api &) = delete; + ~Api(); + + std::unique_ptr connect(const std::string& token = "") override; + + Api& operator=(const Api &) = delete; + }; + +} + +#endif diff --git a/include/cpp_restapi/github/github_api_qt.hpp b/include/cpp_restapi/github/github_api_qt.hpp new file mode 100644 index 0000000..7aa9abe --- /dev/null +++ b/include/cpp_restapi/github/github_api_qt.hpp @@ -0,0 +1,41 @@ + +#ifndef GITHUBAPI_QT_HPP +#define GITHUBAPI_QT_HPP + +// Based on: +// https://developer.github.com/guides/getting-started/ +// https://developer.github.com/v3/ + +#include + +#include + +#include "cpp_restapi/iconnection.hpp" +#include "github_api_base.hpp" +#include "github_api_export.h" + +class QNetworkAccessManager; + +namespace cpp_restapi::GitHub::QtBackend +{ + + /** + * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager + */ + class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase + { + public: + Api(QNetworkAccessManager &, const QString& addr = "https://api.github.com"); + Api(const Api &) = delete; + + std::unique_ptr connect(const std::string& token = "") override; + + Api& operator=(const Api &) = delete; + + private: + QNetworkAccessManager& m_manager; + }; + +} + +#endif diff --git a/include/github_api/igithub_api.hpp b/include/cpp_restapi/github/igithub_api.hpp similarity index 96% rename from include/github_api/igithub_api.hpp rename to include/cpp_restapi/github/igithub_api.hpp index a631d79..d240d17 100644 --- a/include/github_api/igithub_api.hpp +++ b/include/cpp_restapi/github/igithub_api.hpp @@ -8,7 +8,7 @@ #include -namespace GitHub +namespace cpp_restapi::GitHub { /** diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp new file mode 100644 index 0000000..e8b80a8 --- /dev/null +++ b/include/cpp_restapi/github/request.hpp @@ -0,0 +1,514 @@ + +#ifndef REQUEST_HPP +#define REQUEST_HPP + +#include +#include + +#include +#include "github_api_export.h" + + +namespace cpp_restapi::GitHub +{ + /** + * @brief GitHub api actions. + * + * Class contains a convenient set of actions which can be + * executed on GitHub's api. + * + * Before one can use it a connection with github needs to be established. + * Use \ref GitHub::QtBackend::Api or \ref GitHub::CurlBackend::Api + * to construct an \ref GitHub::IConnection object. + * + * All methods return a response in json format. + */ + class GITHUB_API_EXPORT Request + { + public: + Request(std::unique_ptr); + Request(const Request &) = delete; + ~Request(); + + Request& operator=(const Request &) = delete; + + /** + * @brief Request user info + * @param user GitHub user name + * @return api response in json format + * + * Request user information. Equivalent of fetching https://api.github.com/users/\ + */ + std::string getUserInfo(const std::string& user); + + /** + * @brief Request releases for repository + * @param user GitHub user name + * @param repo user's repository name + * @return api response in json format + * + * Request list of releases for repository. Equivalent of fetching https://api.github.com/repos/\/\/releases + */ + std::string getReleases(const std::string& user, const std::string& repo); + + /** + * @brief Request release details + * @param user GitHub user name + * @param repo user's repository name + * @param id release id. Id is returned as a part of \ref getReleases + * @return api response in json format + * + * Request details of release. Equivalent of fetching https://api.github.com/repos/\/\/releases/\ + */ + std::string getRelease(const std::string& user, const std::string& repo, int id); + + /** + * @brief Request api limits + * @return api response in json format + * + * Request limits for api calls. Equivalent of fetching https://api.github.com/rate_limit + */ + std::string getRateLimit(); + + /** + * @brief Request list of user repositories + * @param user GitHub user name + * @return api response in json format + * + * Request list of repositories for user. Equivalent of fetching https://api.github.com/users/\/repos + */ + std::string listUserRepo(const std::string& user); + + + // --------------- user info related api + + /** + * @brief get the authenticated user info + * + * @return std::string json std::string response + */ + std::string getAuntenticatedUser(); + + /** + * @brief Lists all users, in the order that they + * signed up on GitHub. This list includes personal + * user accounts and organization accounts. + * + * @return std::string list of gitub account + */ + std::string listUsers(); + /** + * @brief Provides publicly available information about + * someone with a GitHub account. + * + * @param username github user name + * @return std::string + */ + std::string getUser(const std::string& username); + + + // issues related api methods + /** + * @brief List issues assigned to the authenticated user + * across all visible repositories including owned + * repositories, member repositories, and organization + * repositories. + * + * @return std::string list of issues assigned to the user + */ + std::string issues(); + + /** + * @brief List issues in an organization assigned to the + * authenticated user. + * + * @param org github organization + * @return std::string + */ + std::string orgIssues(const std::string& org); + + /** + * @brief List issues in a repository. + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return std::string list of isues associated with owner + */ + std::string listRepoIssues(const std::string& owner, const std::string& repo); + + /** + * @brief + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @param issueNumber The number that identifies the issue. + * @return std::string all info associated with the issue + */ + std::string getIssue(const std::string& owner, const std::string& repo, const std::string& issueNumber); + + // pull request related api methods + /** + * @brief List pull request in a repository. + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return std::string list of pull request associated with owner + */ + std::string listPullRequest(const std::string& owner, const std::string& repo); + + /** + * @brief Lists details of a pull request by providing its number. + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @param pullNumber The number that identifies the PR. + * @return std::string, all info associated with the PR + */ + std::string getPullRequest(const std::string& owner, const std::string& repo, const std::string& pullNumber); + + /** + * @brief Lists a maximum of 250 commits for a pull request + * + * @param owner The account owner of the repository. + * @param repo the name of the repository + * @param pullNumber The number that identifies the PR. + * @return std::string, Lists up to 250 commits for a pull request + */ + std::string listPullRequestCommit(const std::string& owner, const std::string& repo, const std::string& pullNumber); + + /** + * @brief Responses include a maximum of 3000 files. + * The paginated response returns 30 files per + * page by default. + * + * @param owner The account owner of the repository. + * @param repo the name of the repository + * @param pullNumber The number that identifies the PR. + * @return std::string, list of dict with details of each committed file + */ + std::string listPullRequestfiles(const std::string& owner, const std::string& repo, const std::string& pullNumber); + + /** + * @brief Check if a pull request has been merged + * + * @param owner The account owner of the repository. + * @param repo the name of the repository + * @param pullNumber The number that identifies the PR. + * @return std::string, Status: 204 if if pull request has been merged + */ + std::string isPRmerged(const std::string& owner, const std::string& repo, const std::string& pullNumber); + + // commits related api methods + /** + * @brief List commits. + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return The response will include a verification object that + * describes the result of verifying the commit's signature. + * returns list of objects + */ + std::string listCommits(const std::string& owner, const std::string& repo); + + /** + * @brief List branches for HEAD commit + * + * @param owner The account owner of the repository. + * @param repo the name of the repository + * @param commitSha The SHA of the commit. + * @return All branches where the given commit + * SHA is the HEAD, or latest commit for the branch. + */ + std::string listBranchHeadCommit(const std::string& owner, const std::string& repo,const std::string& commitSha); + + /** + * @brief List pull requests associated with a commit + * + * @param owner The account owner of the repository. + * @param repo the name of the repository + * @param commitSha The SHA of the commit. + * @return All open,closed pull requests associated with the commit. + */ + std::string listCommitPullRequest(const std::string& owner, const std::string& repo, const std::string& commitSha); + + /** + * @brief Get a commit + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @param reference ref parameter + * @return Returns the contents of a single commit reference. + * You must have read access for the repository to use this + * endpoint. + */ + std::string getCommits(const std::string& owner, const std::string& repo, const std::string& reference); + + // metrics related api methods + /** + * @brief Get the weekly commit activity + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return a weekly aggregate of the number of additions and + * deletions pushed to a repository. + */ + std::string getWeeklyCommit(const std::string& owner, const std::string& repo); + + /** + * @brief Get the last year of commit activity + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return std::string Returns the last year of commit activity grouped by week. + * The days array is a group of commits per day, starting on Sunday + */ + std::string getLastYearCommit(const std::string& owner, const std::string& repo); + /** + * @brief Get all contributor commit activity + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return std::string Returns the total number of commits authored by the + * contributor. In addition, the response includes a Weekly + * Hash (weeks array) with the following information: + w - Start of the week, given as a Unix timestamp. + a - Number of additions + d - Number of deletions + c - Number of commits + */ + std::string getcontributorsActivity(const std::string& owner, const std::string& repo); + + /** + * @brief Get the weekly commit count + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return The total commit counts for the owner and total commit counts in + * all. All is everyone combined, including the owner in the last 52 + * weeks. If you'd like to get the commit counts for non-owners, you + * can subtract owner from all. + */ + std::string getCommitCount(const std::string& owner, const std::string& repo); + + /** + * @brief Get the hourly commit count for each day + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return std::string Returns array containing the day number, hour number, + * and number of commits: + * For example, [2, 14, 25] indicates that there + * were 25 total commits, during the 2:00pm hour on Tuesdays + */ + std::string getHourlyCommitCount(const std::string& owner, const std::string& repo); + + /** + * @brief Get community profile metrics + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return all community profile metrics, including an overall health + * score, repository description, the presence of documentation + * detected code of conduct, detected license,and the presence of + * ISSUE_TEMPLATE, PULL_REQUEST_TEMPLATE,README, and CONTRIBUTING + * files + */ + std::string getCommunityProfileMetrics(const std::string& owner, const std::string& repo); + + /** + * @brief Get repository clones + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return return Get the total number of clones and breakdown per day + * or week for the last 14 days. Timestamps are aligned to UTC + * midnight of the beginning of the day or week. Week begins on + * Monday. + */ + std::string getRepoClones(const std::string& owner, const std::string& repo); + + /** + * @brief Get top referral paths + * + * @param owner The account owner of the repository. + * @param repo the name of the repository. + * @return return lists of the top 10 popular contents over the last + * 14 days. + */ + std::string getRefferalPaths(const std::string& owner, const std::string& repo); + + /** + * @brief Get top referral sources + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return std::string Get the top 10 referrers over the last 14 days. + */ + std::string getTopreferralSource(const std::string& owner, const std::string& repo); + + /** + * @brief Get page views. Get the total number of views and breakdown + * per day or week for the last 14 days. + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return std::string Get the top 10 referrers over the last 14 days. + */ + std::string getPageViews(const std::string& owner, const std::string& repo); + + // event related apis + /** + * @brief List public events for a network of + * repositories + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return public event associated with repo + */ + std::string listNetworkRepoEvent(const std::string& owner, const std::string& repo); + + /** + * @brief List public organization events + * + * @param org The organization name. + * @return public event associated with repo + */ + std::string listOrgEvent(const std::string& org); + + /** + * @brief List repository events + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return public event associated with repo + */ + std::string listRepoEvent(const std::string& owner, const std::string& repo); + + /** + * @brief List events for the authenticated user + * + * @param username The handle for the GitHub user account + * @return If you are authenticated as the given user, you will see your + * private events. Otherwise, you'll only see public events. + */ + std::string listUserEvent(const std::string& username); + + // staring related api methods + + /** + * @brief Lists the people that have starred the repository. + * + * @param owner The account owner of the repository. + * @param repo The name of the repository. + * @return returns Lists of people that have starred the repository. + */ + std::string listStargazers(const std::string& owner, const std::string& repo); + + /** + * @brief List repositories starred by a user + * + * @param username The handle for the GitHub user account + * @return returns a list of repositories a user has starred + */ + std::string listUserStarredRepo(const std::string& username); + + // watching related api + + /** + * @brief Lists the people watching the specified repository. + * + * @param owner The account owner of the repository + * @param repo The name of the repository + * @return list of github account watching the repository + */ + std::string listRepoWatchers(const std::string& owner, const std::string& repo); + + /** + * @brief Get a repository subscription. + * + * @param owner The account owner of the repository + * @param repo The name of the repository + * @return list of users subscribe to the repository + */ + std::string getRepoSubscription(const std::string& owner, const std::string& repo); + + /** + * @brief List repositories watched by a user + * + * @param username The handle for the GitHub user account + * @return Lists repositories a user is watching. + */ + std::string listUserWatchedRepos(const std::string& username); + + /** + * @brief List repository collaborators + * + * @param owner The account owner of the repository + * @param repo The name of the repository + * @return list of collaborators includes outside collaborators, + * organization members that are direct collaborators, + * organization members with access through team memberships, + * organization members with access through default + * organization permissions, and organization owners + */ + std::string listRepoCollaborators(const std::string& owner, const std::string& repo); + + /** + * @brief List organization repositories + * + * @param org The organization name + * @return Lists repositories for the specified organization. + */ + std::string getOrgRepo(const std::string& org); + + /** + * @brief Get a repository + * + * @param owner The account owner of the repository + * @param repo The name of the repository + * @return The parent and source objects are present + * when the repository is a fork. parent is + * the repository this repository was forked from, + * source is the ultimate source for the network. + */ + std::string getRepository(const std::string& owner, const std::string& repo); + + /** + * @brief List repositories for the authenticated user + * + * @return Lists repositories that the authenticated user + * has explicit permission (:read, :write, or :admin) + * to access. + */ + std::string listAuthUserRepo(); + + /** + * @brief List repository languages + * + * @param owner The account owner of the repository + * @param repo The name of the repository + * @return Lists languages for the specified repository. + * The value shown for each language is the number + * of bytes of code written in that language. + */ + std::string getRepoLang(const std::string& owner, const std::string& repo); + + /** + * @brief List repository contributors + * + * @param owner The account owner of the repository + * @param repo The name of the repository + * @return Lists contributors to the specified repository + * and sorts them by the number of commits per + * contributor in descending order + */ + std::string repoContributors(const std::string& owner, const std::string& repo); + + private: + std::unique_ptr m_connection; + + std::string doRequest(const std::string &); + }; +} + +#endif // REQUEST_H diff --git a/include/github_api/github_api_curl.hpp b/include/github_api/github_api_curl.hpp index e847083..bfa81be 100644 --- a/include/github_api/github_api_curl.hpp +++ b/include/github_api/github_api_curl.hpp @@ -1,30 +1,5 @@ -#ifndef GITHUBAPI_CURL_HPP -#define GITHUBAPI_CURL_HPP +#warning This file is deprecated. Please include instead +#include -#include - -#include "cpp_restapi/iconnection.hpp" -#include "github_api_base.hpp" -#include "github_api_export.h" - -namespace GitHub { namespace CurlBackend -{ - /** - * @brief Class for establishing connection with GitHub api with Curl - */ - class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase - { - public: - Api(const std::string& addr = "https://api.github.com"); - Api(const Api &) = delete; - ~Api(); - - std::unique_ptr connect(const std::string& token = "") override; - - Api& operator=(const Api &) = delete; - }; - -}} - -#endif +namespace GitHub = cpp_restapi::GitHub; diff --git a/include/github_api/github_api_qt.hpp b/include/github_api/github_api_qt.hpp index 244bf56..f9c101f 100644 --- a/include/github_api/github_api_qt.hpp +++ b/include/github_api/github_api_qt.hpp @@ -1,41 +1,5 @@ -#ifndef GITHUBAPI_QT_HPP -#define GITHUBAPI_QT_HPP +#warning This file is deprecated. Please include instead +#include -// Based on: -// https://developer.github.com/guides/getting-started/ -// https://developer.github.com/v3/ - -#include - -#include - -#include "cpp_restapi/iconnection.hpp" -#include "github_api_base.hpp" -#include "github_api_export.h" - -class QNetworkAccessManager; - -namespace GitHub::QtBackend -{ - - /** - * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager - */ - class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase - { - public: - Api(QNetworkAccessManager &, const QString& addr = "https://api.github.com"); - Api(const Api &) = delete; - - std::unique_ptr connect(const std::string& token = "") override; - - Api& operator=(const Api &) = delete; - - private: - QNetworkAccessManager& m_manager; - }; - -} - -#endif +namespace GitHub = cpp_restapi::GitHub; diff --git a/include/github_api/request.hpp b/include/github_api/request.hpp index 603babc..b3520a4 100644 --- a/include/github_api/request.hpp +++ b/include/github_api/request.hpp @@ -1,514 +1,3 @@ -#ifndef REQUEST_HPP -#define REQUEST_HPP - -#include -#include - -#include -#include "github_api_export.h" - - -namespace GitHub -{ - /** - * @brief GitHub api actions. - * - * Class contains a convenient set of actions which can be - * executed on GitHub's api. - * - * Before one can use it a connection with github needs to be established. - * Use \ref GitHub::QtBackend::Api or \ref GitHub::CurlBackend::Api - * to construct an \ref GitHub::IConnection object. - * - * All methods return a response in json format. - */ - class GITHUB_API_EXPORT Request - { - public: - Request(std::unique_ptr); - Request(const Request &) = delete; - ~Request(); - - Request& operator=(const Request &) = delete; - - /** - * @brief Request user info - * @param user GitHub user name - * @return api response in json format - * - * Request user information. Equivalent of fetching https://api.github.com/users/\ - */ - std::string getUserInfo(const std::string& user); - - /** - * @brief Request releases for repository - * @param user GitHub user name - * @param repo user's repository name - * @return api response in json format - * - * Request list of releases for repository. Equivalent of fetching https://api.github.com/repos/\/\/releases - */ - std::string getReleases(const std::string& user, const std::string& repo); - - /** - * @brief Request release details - * @param user GitHub user name - * @param repo user's repository name - * @param id release id. Id is returned as a part of \ref getReleases - * @return api response in json format - * - * Request details of release. Equivalent of fetching https://api.github.com/repos/\/\/releases/\ - */ - std::string getRelease(const std::string& user, const std::string& repo, int id); - - /** - * @brief Request api limits - * @return api response in json format - * - * Request limits for api calls. Equivalent of fetching https://api.github.com/rate_limit - */ - std::string getRateLimit(); - - /** - * @brief Request list of user repositories - * @param user GitHub user name - * @return api response in json format - * - * Request list of repositories for user. Equivalent of fetching https://api.github.com/users/\/repos - */ - std::string listUserRepo(const std::string& user); - - - // --------------- user info related api - - /** - * @brief get the authenticated user info - * - * @return std::string json std::string response - */ - std::string getAuntenticatedUser(); - - /** - * @brief Lists all users, in the order that they - * signed up on GitHub. This list includes personal - * user accounts and organization accounts. - * - * @return std::string list of gitub account - */ - std::string listUsers(); - /** - * @brief Provides publicly available information about - * someone with a GitHub account. - * - * @param username github user name - * @return std::string - */ - std::string getUser(const std::string& username); - - - // issues related api methods - /** - * @brief List issues assigned to the authenticated user - * across all visible repositories including owned - * repositories, member repositories, and organization - * repositories. - * - * @return std::string list of issues assigned to the user - */ - std::string issues(); - - /** - * @brief List issues in an organization assigned to the - * authenticated user. - * - * @param org github organization - * @return std::string - */ - std::string orgIssues(const std::string& org); - - /** - * @brief List issues in a repository. - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return std::string list of isues associated with owner - */ - std::string listRepoIssues(const std::string& owner, const std::string& repo); - - /** - * @brief - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @param issueNumber The number that identifies the issue. - * @return std::string all info associated with the issue - */ - std::string getIssue(const std::string& owner, const std::string& repo, const std::string& issueNumber); - - // pull request related api methods - /** - * @brief List pull request in a repository. - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return std::string list of pull request associated with owner - */ - std::string listPullRequest(const std::string& owner, const std::string& repo); - - /** - * @brief Lists details of a pull request by providing its number. - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @param pullNumber The number that identifies the PR. - * @return std::string, all info associated with the PR - */ - std::string getPullRequest(const std::string& owner, const std::string& repo, const std::string& pullNumber); - - /** - * @brief Lists a maximum of 250 commits for a pull request - * - * @param owner The account owner of the repository. - * @param repo the name of the repository - * @param pullNumber The number that identifies the PR. - * @return std::string, Lists up to 250 commits for a pull request - */ - std::string listPullRequestCommit(const std::string& owner, const std::string& repo, const std::string& pullNumber); - - /** - * @brief Responses include a maximum of 3000 files. - * The paginated response returns 30 files per - * page by default. - * - * @param owner The account owner of the repository. - * @param repo the name of the repository - * @param pullNumber The number that identifies the PR. - * @return std::string, list of dict with details of each committed file - */ - std::string listPullRequestfiles(const std::string& owner, const std::string& repo, const std::string& pullNumber); - - /** - * @brief Check if a pull request has been merged - * - * @param owner The account owner of the repository. - * @param repo the name of the repository - * @param pullNumber The number that identifies the PR. - * @return std::string, Status: 204 if if pull request has been merged - */ - std::string isPRmerged(const std::string& owner, const std::string& repo, const std::string& pullNumber); - - // commits related api methods - /** - * @brief List commits. - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return The response will include a verification object that - * describes the result of verifying the commit's signature. - * returns list of objects - */ - std::string listCommits(const std::string& owner, const std::string& repo); - - /** - * @brief List branches for HEAD commit - * - * @param owner The account owner of the repository. - * @param repo the name of the repository - * @param commitSha The SHA of the commit. - * @return All branches where the given commit - * SHA is the HEAD, or latest commit for the branch. - */ - std::string listBranchHeadCommit(const std::string& owner, const std::string& repo,const std::string& commitSha); - - /** - * @brief List pull requests associated with a commit - * - * @param owner The account owner of the repository. - * @param repo the name of the repository - * @param commitSha The SHA of the commit. - * @return All open,closed pull requests associated with the commit. - */ - std::string listCommitPullRequest(const std::string& owner, const std::string& repo, const std::string& commitSha); - - /** - * @brief Get a commit - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @param reference ref parameter - * @return Returns the contents of a single commit reference. - * You must have read access for the repository to use this - * endpoint. - */ - std::string getCommits(const std::string& owner, const std::string& repo, const std::string& reference); - - // metrics related api methods - /** - * @brief Get the weekly commit activity - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return a weekly aggregate of the number of additions and - * deletions pushed to a repository. - */ - std::string getWeeklyCommit(const std::string& owner, const std::string& repo); - - /** - * @brief Get the last year of commit activity - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return std::string Returns the last year of commit activity grouped by week. - * The days array is a group of commits per day, starting on Sunday - */ - std::string getLastYearCommit(const std::string& owner, const std::string& repo); - /** - * @brief Get all contributor commit activity - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return std::string Returns the total number of commits authored by the - * contributor. In addition, the response includes a Weekly - * Hash (weeks array) with the following information: - w - Start of the week, given as a Unix timestamp. - a - Number of additions - d - Number of deletions - c - Number of commits - */ - std::string getcontributorsActivity(const std::string& owner, const std::string& repo); - - /** - * @brief Get the weekly commit count - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return The total commit counts for the owner and total commit counts in - * all. All is everyone combined, including the owner in the last 52 - * weeks. If you'd like to get the commit counts for non-owners, you - * can subtract owner from all. - */ - std::string getCommitCount(const std::string& owner, const std::string& repo); - - /** - * @brief Get the hourly commit count for each day - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return std::string Returns array containing the day number, hour number, - * and number of commits: - * For example, [2, 14, 25] indicates that there - * were 25 total commits, during the 2:00pm hour on Tuesdays - */ - std::string getHourlyCommitCount(const std::string& owner, const std::string& repo); - - /** - * @brief Get community profile metrics - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return all community profile metrics, including an overall health - * score, repository description, the presence of documentation - * detected code of conduct, detected license,and the presence of - * ISSUE_TEMPLATE, PULL_REQUEST_TEMPLATE,README, and CONTRIBUTING - * files - */ - std::string getCommunityProfileMetrics(const std::string& owner, const std::string& repo); - - /** - * @brief Get repository clones - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return return Get the total number of clones and breakdown per day - * or week for the last 14 days. Timestamps are aligned to UTC - * midnight of the beginning of the day or week. Week begins on - * Monday. - */ - std::string getRepoClones(const std::string& owner, const std::string& repo); - - /** - * @brief Get top referral paths - * - * @param owner The account owner of the repository. - * @param repo the name of the repository. - * @return return lists of the top 10 popular contents over the last - * 14 days. - */ - std::string getRefferalPaths(const std::string& owner, const std::string& repo); - - /** - * @brief Get top referral sources - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return std::string Get the top 10 referrers over the last 14 days. - */ - std::string getTopreferralSource(const std::string& owner, const std::string& repo); - - /** - * @brief Get page views. Get the total number of views and breakdown - * per day or week for the last 14 days. - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return std::string Get the top 10 referrers over the last 14 days. - */ - std::string getPageViews(const std::string& owner, const std::string& repo); - - // event related apis - /** - * @brief List public events for a network of - * repositories - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return public event associated with repo - */ - std::string listNetworkRepoEvent(const std::string& owner, const std::string& repo); - - /** - * @brief List public organization events - * - * @param org The organization name. - * @return public event associated with repo - */ - std::string listOrgEvent(const std::string& org); - - /** - * @brief List repository events - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return public event associated with repo - */ - std::string listRepoEvent(const std::string& owner, const std::string& repo); - - /** - * @brief List events for the authenticated user - * - * @param username The handle for the GitHub user account - * @return If you are authenticated as the given user, you will see your - * private events. Otherwise, you'll only see public events. - */ - std::string listUserEvent(const std::string& username); - - // staring related api methods - - /** - * @brief Lists the people that have starred the repository. - * - * @param owner The account owner of the repository. - * @param repo The name of the repository. - * @return returns Lists of people that have starred the repository. - */ - std::string listStargazers(const std::string& owner, const std::string& repo); - - /** - * @brief List repositories starred by a user - * - * @param username The handle for the GitHub user account - * @return returns a list of repositories a user has starred - */ - std::string listUserStarredRepo(const std::string& username); - - // watching related api - - /** - * @brief Lists the people watching the specified repository. - * - * @param owner The account owner of the repository - * @param repo The name of the repository - * @return list of github account watching the repository - */ - std::string listRepoWatchers(const std::string& owner, const std::string& repo); - - /** - * @brief Get a repository subscription. - * - * @param owner The account owner of the repository - * @param repo The name of the repository - * @return list of users subscribe to the repository - */ - std::string getRepoSubscription(const std::string& owner, const std::string& repo); - - /** - * @brief List repositories watched by a user - * - * @param username The handle for the GitHub user account - * @return Lists repositories a user is watching. - */ - std::string listUserWatchedRepos(const std::string& username); - - /** - * @brief List repository collaborators - * - * @param owner The account owner of the repository - * @param repo The name of the repository - * @return list of collaborators includes outside collaborators, - * organization members that are direct collaborators, - * organization members with access through team memberships, - * organization members with access through default - * organization permissions, and organization owners - */ - std::string listRepoCollaborators(const std::string& owner, const std::string& repo); - - /** - * @brief List organization repositories - * - * @param org The organization name - * @return Lists repositories for the specified organization. - */ - std::string getOrgRepo(const std::string& org); - - /** - * @brief Get a repository - * - * @param owner The account owner of the repository - * @param repo The name of the repository - * @return The parent and source objects are present - * when the repository is a fork. parent is - * the repository this repository was forked from, - * source is the ultimate source for the network. - */ - std::string getRepository(const std::string& owner, const std::string& repo); - - /** - * @brief List repositories for the authenticated user - * - * @return Lists repositories that the authenticated user - * has explicit permission (:read, :write, or :admin) - * to access. - */ - std::string listAuthUserRepo(); - - /** - * @brief List repository languages - * - * @param owner The account owner of the repository - * @param repo The name of the repository - * @return Lists languages for the specified repository. - * The value shown for each language is the number - * of bytes of code written in that language. - */ - std::string getRepoLang(const std::string& owner, const std::string& repo); - - /** - * @brief List repository contributors - * - * @param owner The account owner of the repository - * @param repo The name of the repository - * @return Lists contributors to the specified repository - * and sorts them by the number of commits per - * contributor in descending order - */ - std::string repoContributors(const std::string& owner, const std::string& repo); - - private: - std::unique_ptr m_connection; - - std::string doRequest(const std::string &); - }; -} - -#endif // REQUEST_H +#warning This file is deprecated. Please include instead +#include diff --git a/src/curl_backend/github_api.cpp b/src/curl_backend/github_api.cpp index f13153d..7348584 100644 --- a/src/curl_backend/github_api.cpp +++ b/src/curl_backend/github_api.cpp @@ -1,11 +1,11 @@ #include -#include +#include #include "connection.hpp" -namespace GitHub::CurlBackend +namespace cpp_restapi::GitHub::CurlBackend { Api::Api(const std::string& addr) diff --git a/src/qt_backend/github_api.cpp b/src/qt_backend/github_api.cpp index 7567f95..d6725ae 100644 --- a/src/qt_backend/github_api.cpp +++ b/src/qt_backend/github_api.cpp @@ -1,10 +1,10 @@ -#include +#include #include "connection.hpp" -namespace GitHub::QtBackend +namespace cpp_restapi::GitHub::QtBackend { Api::Api(QNetworkAccessManager& manager, const QString& addr) diff --git a/src/services/github/github_api_base.cpp b/src/services/github/github_api_base.cpp index 18212e6..3f80044 100644 --- a/src/services/github/github_api_base.cpp +++ b/src/services/github/github_api_base.cpp @@ -1,5 +1,5 @@ -#include "github_api/github_api_base.hpp" +#include "cpp_restapi/github/github_api_base.hpp" namespace cpp_restapi diff --git a/src/services/github/request.cpp b/src/services/github/request.cpp index 2f2365d..73ecb36 100644 --- a/src/services/github/request.cpp +++ b/src/services/github/request.cpp @@ -1,5 +1,5 @@ -#include +#include // Based on: @@ -7,7 +7,7 @@ // https://developer.github.com/v3/ -namespace GitHub +namespace cpp_restapi::GitHub { Request::Request(std::unique_ptr connection) diff --git a/tests/api_tests.cpp b/tests/api_tests.cpp index 0809a7b..a043468 100644 --- a/tests/api_tests.cpp +++ b/tests/api_tests.cpp @@ -4,9 +4,9 @@ #include #include -#include "github_api/github_api_curl.hpp" -#include "github_api/github_api_qt.hpp" -#include "github_api/request.hpp" +#include "cpp_restapi/github/github_api_curl.hpp" +#include "cpp_restapi/github/github_api_qt.hpp" +#include "cpp_restapi/github/request.hpp" #include "github_server_mock.hpp" @@ -15,6 +15,7 @@ using testing::_; using testing::Return; using testing::NiceMock; +using namespace cpp_restapi; namespace { From 7241e1e7885b58cfe3eea36d47f4e2932bf097ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 18:49:09 +0100 Subject: [PATCH 14/24] Mark connection builders as deprecated --- include/cpp_restapi/github/github_api_base.hpp | 2 +- include/cpp_restapi/github/github_api_curl.hpp | 2 +- include/cpp_restapi/github/github_api_qt.hpp | 2 +- include/cpp_restapi/github/igithub_api.hpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/cpp_restapi/github/github_api_base.hpp b/include/cpp_restapi/github/github_api_base.hpp index aa44baf..7a75453 100644 --- a/include/cpp_restapi/github/github_api_base.hpp +++ b/include/cpp_restapi/github/github_api_base.hpp @@ -6,7 +6,7 @@ namespace cpp_restapi { - class GitHubBase: public cpp_restapi::GitHub::IApi + class [[deprecated]] GitHubBase: public cpp_restapi::GitHub::IApi { public: GitHubBase(const std::string& address); diff --git a/include/cpp_restapi/github/github_api_curl.hpp b/include/cpp_restapi/github/github_api_curl.hpp index 10789fa..4c3179a 100644 --- a/include/cpp_restapi/github/github_api_curl.hpp +++ b/include/cpp_restapi/github/github_api_curl.hpp @@ -13,7 +13,7 @@ namespace cpp_restapi::GitHub::CurlBackend /** * @brief Class for establishing connection with GitHub api with Curl */ - class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase + class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { public: Api(const std::string& addr = "https://api.github.com"); diff --git a/include/cpp_restapi/github/github_api_qt.hpp b/include/cpp_restapi/github/github_api_qt.hpp index 7aa9abe..cf53e76 100644 --- a/include/cpp_restapi/github/github_api_qt.hpp +++ b/include/cpp_restapi/github/github_api_qt.hpp @@ -22,7 +22,7 @@ namespace cpp_restapi::GitHub::QtBackend /** * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager */ - class GITHUB_API_EXPORT Api: public cpp_restapi::GitHubBase + class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { public: Api(QNetworkAccessManager &, const QString& addr = "https://api.github.com"); diff --git a/include/cpp_restapi/github/igithub_api.hpp b/include/cpp_restapi/github/igithub_api.hpp index d240d17..1edeb89 100644 --- a/include/cpp_restapi/github/igithub_api.hpp +++ b/include/cpp_restapi/github/igithub_api.hpp @@ -14,7 +14,7 @@ namespace cpp_restapi::GitHub /** * @brief common interface for all api backends */ -class IApi +class [[deprecated]] IApi { public: virtual ~IApi() = default; From 16f65d50f348dbea85afb71ecd09a1af71babe95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 19:21:19 +0100 Subject: [PATCH 15/24] Introduce ConnectionBuilder for GitHub --- .../cpp_restapi/github/connection_builder.hpp | 46 +++++++++++++++++ .../cpp_restapi/github/github_api_base.hpp | 2 +- .../cpp_restapi/github/github_api_curl.hpp | 2 +- include/cpp_restapi/github/github_api_qt.hpp | 2 +- include/cpp_restapi/github/igithub_api.hpp | 4 +- tests/CMakeLists.txt | 1 + tests/api_tests.cpp | 50 ++++++++++++++++++- 7 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 include/cpp_restapi/github/connection_builder.hpp diff --git a/include/cpp_restapi/github/connection_builder.hpp b/include/cpp_restapi/github/connection_builder.hpp new file mode 100644 index 0000000..f9e6ebe --- /dev/null +++ b/include/cpp_restapi/github/connection_builder.hpp @@ -0,0 +1,46 @@ + +#ifndef CONNECTION_BUILDER_HPP_INCLUDED +#define CONNECTION_BUILDER_HPP_INCLUDED + +#include + + +namespace cpp_restapi::GitHub +{ + class ConnectionBuilder + { + public: + ConnectionBuilder() + : m_address("https://api.github.com") + { + } + + ConnectionBuilder(const ConnectionBuilder &) = delete; + + ConnectionBuilder& setAddress(const std::string& address) + { + m_address = address; + + return *this; + } + + ConnectionBuilder& setToken(const std::string& token) + { + m_headerEntries.emplace("Authorization", "token " + token); + + return *this; + } + + template + std::unique_ptr build(Args&&... args) + { + return std::make_unique(std::forward(args)..., m_address, m_headerEntries); + } + + private: + std::map m_headerEntries; + std::string m_address; + }; +} + +#endif diff --git a/include/cpp_restapi/github/github_api_base.hpp b/include/cpp_restapi/github/github_api_base.hpp index 7a75453..6e68499 100644 --- a/include/cpp_restapi/github/github_api_base.hpp +++ b/include/cpp_restapi/github/github_api_base.hpp @@ -6,7 +6,7 @@ namespace cpp_restapi { - class [[deprecated]] GitHubBase: public cpp_restapi::GitHub::IApi + class [[deprecated("Use GitHubConnectionBuilder")]] GitHubBase: public cpp_restapi::GitHub::IApi { public: GitHubBase(const std::string& address); diff --git a/include/cpp_restapi/github/github_api_curl.hpp b/include/cpp_restapi/github/github_api_curl.hpp index 4c3179a..bffeee9 100644 --- a/include/cpp_restapi/github/github_api_curl.hpp +++ b/include/cpp_restapi/github/github_api_curl.hpp @@ -11,7 +11,7 @@ namespace cpp_restapi::GitHub::CurlBackend { /** - * @brief Class for establishing connection with GitHub api with Curl + * @brief Class for establishing connection with GitHub api with Curl. Deprecated, use GitHubConnectionBuilder instead */ class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { diff --git a/include/cpp_restapi/github/github_api_qt.hpp b/include/cpp_restapi/github/github_api_qt.hpp index cf53e76..e642240 100644 --- a/include/cpp_restapi/github/github_api_qt.hpp +++ b/include/cpp_restapi/github/github_api_qt.hpp @@ -20,7 +20,7 @@ namespace cpp_restapi::GitHub::QtBackend { /** - * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager + * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager. Deprecated, use GitHubConnectionBuilder instead */ class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { diff --git a/include/cpp_restapi/github/igithub_api.hpp b/include/cpp_restapi/github/igithub_api.hpp index 1edeb89..5f25486 100644 --- a/include/cpp_restapi/github/igithub_api.hpp +++ b/include/cpp_restapi/github/igithub_api.hpp @@ -12,9 +12,9 @@ namespace cpp_restapi::GitHub { /** - * @brief common interface for all api backends + * @brief common interface for all api backends. Deprecated, use GitHubConnectionBuilder instead */ -class [[deprecated]] IApi +class [[deprecated("Use GitHubConnectionBuilder")]] IApi { public: virtual ~IApi() = default; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f9bfefb..3cb0692 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,6 +29,7 @@ target_link_libraries(api_tests target_include_directories(api_tests PRIVATE + ${PROJECT_SOURCE_DIR}/src ${httpmockserver_SOURCE_DIR}/include ) diff --git a/tests/api_tests.cpp b/tests/api_tests.cpp index a043468..2d411ac 100644 --- a/tests/api_tests.cpp +++ b/tests/api_tests.cpp @@ -4,8 +4,11 @@ #include #include +#include "curl_backend/connection.hpp" +#include "qt_backend/connection.hpp" #include "cpp_restapi/github/github_api_curl.hpp" #include "cpp_restapi/github/github_api_qt.hpp" +#include "cpp_restapi/github/connection_builder.hpp" #include "cpp_restapi/github/request.hpp" #include "github_server_mock.hpp" @@ -24,19 +27,34 @@ namespace template T buildApi(); + template + std::unique_ptr buildNewApi(); + template<> GitHub::CurlBackend::Api buildApi() { return GitHub::CurlBackend::Api(std::string("http://localhost:") + std::to_string(port)); } - template<> GitHub::QtBackend::Api buildApi() { static QNetworkAccessManager networkmanager; return GitHub::QtBackend::Api(networkmanager, QString("http://localhost:%1").arg(port)); } + + template<> + std::unique_ptr buildNewApi() + { + return GitHub::ConnectionBuilder().setAddress(std::string("http://localhost:") + std::to_string(port)).build(); + } + + template<> + std::unique_ptrbuildNewApi() + { + static QNetworkAccessManager networkmanager; + return GitHub::ConnectionBuilder().setAddress(std::string("http://localhost:") + std::to_string(port)).build(networkmanager); + } } @@ -53,6 +71,21 @@ class ApiTest: public testing::Test NiceMock server; }; +template +struct BackendTraits; + +template<> +struct BackendTraits +{ + using Connection = CurlBackend::Connection; +}; + +template<> +struct BackendTraits +{ + using Connection = QtBackend::Connection; +}; + using Backends = testing::Types; TYPED_TEST_SUITE(ApiTest, Backends); @@ -72,6 +105,21 @@ TYPED_TEST(ApiTest, fetchRegularUser) } +TYPED_TEST(ApiTest, newInterface) +{ + GithubServerMock::Response response(200, R"({"login":"userName1234","id":1234"})"); + ON_CALL(this->server, responseHandler).WillByDefault(Return(response)); + + using Connection = typename BackendTraits::Connection; + auto connection = buildNewApi(); + + GitHub::Request request(std::move(connection)); + const auto info = request.getUserInfo("userName1234"); + + EXPECT_EQ(info, "{\"id\":1234,\"login\":\"userName1234\"}\n"); +} + + TYPED_TEST(ApiTest, pagination) { auto api = buildApi(); From 97bcb1ffe7887655fbc82bfb8e319fb975f56f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Nov 2023 19:51:23 +0100 Subject: [PATCH 16/24] Make connection classes public --- {src => include/cpp_restapi}/base_connection.hpp | 0 .../connection.hpp => include/cpp_restapi/curl_connection.hpp | 3 ++- include/cpp_restapi/github/connection_builder.hpp | 3 +++ .../connection.hpp => include/cpp_restapi/qt_connection.hpp | 3 ++- src/base_connection.cpp | 2 +- src/curl_backend/CMakeLists.txt | 1 - src/curl_backend/connection.cpp | 3 ++- src/curl_backend/github_api.cpp | 2 +- src/qt_backend/CMakeLists.txt | 1 - src/qt_backend/connection.cpp | 2 +- src/qt_backend/github_api.cpp | 3 +-- tests/CMakeLists.txt | 1 - tests/api_tests.cpp | 4 ++-- 13 files changed, 15 insertions(+), 13 deletions(-) rename {src => include/cpp_restapi}/base_connection.hpp (100%) rename src/curl_backend/connection.hpp => include/cpp_restapi/curl_connection.hpp (85%) rename src/qt_backend/connection.hpp => include/cpp_restapi/qt_connection.hpp (87%) diff --git a/src/base_connection.hpp b/include/cpp_restapi/base_connection.hpp similarity index 100% rename from src/base_connection.hpp rename to include/cpp_restapi/base_connection.hpp diff --git a/src/curl_backend/connection.hpp b/include/cpp_restapi/curl_connection.hpp similarity index 85% rename from src/curl_backend/connection.hpp rename to include/cpp_restapi/curl_connection.hpp index c7acebd..7b98e02 100644 --- a/src/curl_backend/connection.hpp +++ b/include/cpp_restapi/curl_connection.hpp @@ -3,11 +3,12 @@ #define CONNECTION_QT_HPP #include "base_connection.hpp" +#include "github_api_export.h" namespace cpp_restapi { namespace CurlBackend { - class Connection: public BaseConnection + class GITHUB_API_EXPORT Connection: public BaseConnection { public: Connection(const std::string& address, const std::map& headerEntries); diff --git a/include/cpp_restapi/github/connection_builder.hpp b/include/cpp_restapi/github/connection_builder.hpp index f9e6ebe..b894466 100644 --- a/include/cpp_restapi/github/connection_builder.hpp +++ b/include/cpp_restapi/github/connection_builder.hpp @@ -2,6 +2,9 @@ #ifndef CONNECTION_BUILDER_HPP_INCLUDED #define CONNECTION_BUILDER_HPP_INCLUDED +#include +#include + #include diff --git a/src/qt_backend/connection.hpp b/include/cpp_restapi/qt_connection.hpp similarity index 87% rename from src/qt_backend/connection.hpp rename to include/cpp_restapi/qt_connection.hpp index f11060f..8cb88ff 100644 --- a/src/qt_backend/connection.hpp +++ b/include/cpp_restapi/qt_connection.hpp @@ -7,13 +7,14 @@ #include #include "base_connection.hpp" +#include "github_api_export.h" class QNetworkAccessManager; namespace cpp_restapi { namespace QtBackend { - class Connection: public QObject, public BaseConnection + class GITHUB_API_EXPORT Connection: public QObject, public BaseConnection { public: Connection(QNetworkAccessManager &, const std::string& address, const std::map& headerEntries); diff --git a/src/base_connection.cpp b/src/base_connection.cpp index 800c0c2..0c11712 100644 --- a/src/base_connection.cpp +++ b/src/base_connection.cpp @@ -3,7 +3,7 @@ #include #include -#include "base_connection.hpp" +#include #include "header_utils.hpp" diff --git a/src/curl_backend/CMakeLists.txt b/src/curl_backend/CMakeLists.txt index efab3f4..e2b4c12 100644 --- a/src/curl_backend/CMakeLists.txt +++ b/src/curl_backend/CMakeLists.txt @@ -5,7 +5,6 @@ target_sources(github_api PRIVATE ${PROJECT_SOURCE_DIR}/include/github_api/github_api_curl.hpp connection.cpp - connection.hpp github_api.cpp ) diff --git a/src/curl_backend/connection.cpp b/src/curl_backend/connection.cpp index eaea25b..f70ade5 100644 --- a/src/curl_backend/connection.cpp +++ b/src/curl_backend/connection.cpp @@ -11,7 +11,8 @@ #include #include -#include "connection.hpp" +#include + namespace cpp_restapi::CurlBackend { diff --git a/src/curl_backend/github_api.cpp b/src/curl_backend/github_api.cpp index 7348584..c45b48f 100644 --- a/src/curl_backend/github_api.cpp +++ b/src/curl_backend/github_api.cpp @@ -2,7 +2,7 @@ #include #include -#include "connection.hpp" +#include namespace cpp_restapi::GitHub::CurlBackend diff --git a/src/qt_backend/CMakeLists.txt b/src/qt_backend/CMakeLists.txt index badf5e9..f4f9e40 100644 --- a/src/qt_backend/CMakeLists.txt +++ b/src/qt_backend/CMakeLists.txt @@ -8,7 +8,6 @@ endif() target_sources(github_api PRIVATE ${PROJECT_SOURCE_DIR}/include/github_api/github_api_qt.hpp connection.cpp - connection.hpp github_api.cpp ) diff --git a/src/qt_backend/connection.cpp b/src/qt_backend/connection.cpp index e21ff53..a03ae62 100644 --- a/src/qt_backend/connection.cpp +++ b/src/qt_backend/connection.cpp @@ -1,5 +1,5 @@ -#include "connection.hpp" +#include #include #include diff --git a/src/qt_backend/github_api.cpp b/src/qt_backend/github_api.cpp index d6725ae..1fd6002 100644 --- a/src/qt_backend/github_api.cpp +++ b/src/qt_backend/github_api.cpp @@ -1,7 +1,6 @@ #include - -#include "connection.hpp" +#include namespace cpp_restapi::GitHub::QtBackend diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3cb0692..f9bfefb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,7 +29,6 @@ target_link_libraries(api_tests target_include_directories(api_tests PRIVATE - ${PROJECT_SOURCE_DIR}/src ${httpmockserver_SOURCE_DIR}/include ) diff --git a/tests/api_tests.cpp b/tests/api_tests.cpp index 2d411ac..d2e5435 100644 --- a/tests/api_tests.cpp +++ b/tests/api_tests.cpp @@ -4,8 +4,8 @@ #include #include -#include "curl_backend/connection.hpp" -#include "qt_backend/connection.hpp" +#include "cpp_restapi/curl_connection.hpp" +#include "cpp_restapi/qt_connection.hpp" #include "cpp_restapi/github/github_api_curl.hpp" #include "cpp_restapi/github/github_api_qt.hpp" #include "cpp_restapi/github/connection_builder.hpp" From 412524861cd3cc94faffe95a983481f425c4e706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 13:28:51 +0100 Subject: [PATCH 17/24] Add new examples # Conflicts: # examples/CMakeLists.txt --- examples/CMakeLists.txt | 13 +++++++++++++ examples/curl_example.cpp | 9 +++++---- examples/deprecated_curl_example.cpp | 17 +++++++++++++++++ examples/deprecated_qt_example.cpp | 22 ++++++++++++++++++++++ examples/qt_example.cpp | 9 +++++---- 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 examples/deprecated_curl_example.cpp create mode 100644 examples/deprecated_qt_example.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 16fb490..23ca556 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -12,9 +12,22 @@ set(GitHubAPI_QtBackend ON) set(GitHubAPI_CurlBackend ON) add_subdirectory(.. github_api_root) #include directory with github_api +add_executable(deprecated_qt_example deprecated_qt_example.cpp) +add_executable(deprecated_curl_example deprecated_curl_example.cpp) add_executable(qt_example qt_example.cpp) add_executable(curl_example curl_example.cpp) +target_link_libraries(deprecated_qt_example + PRIVATE + github_api + Qt::Network +) + +target_link_libraries(deprecated_curl_example + PRIVATE + github_api +) + target_link_libraries(qt_example PRIVATE github_api diff --git a/examples/curl_example.cpp b/examples/curl_example.cpp index fa3bd90..541ce9b 100644 --- a/examples/curl_example.cpp +++ b/examples/curl_example.cpp @@ -1,14 +1,15 @@ #include -#include -#include +#include +#include +#include int main(int argc, char** argv) { - GitHub::CurlBackend::Api github; - GitHub::Request request(github.connect()); + auto connection = cpp_restapi::GitHub::ConnectionBuilder().build(); + cpp_restapi::GitHub::Request request(std::move(connection)); std::cout << request.getRateLimit() << '\n'; std::cout << request.getUserInfo("Kicer86") << '\n'; diff --git a/examples/deprecated_curl_example.cpp b/examples/deprecated_curl_example.cpp new file mode 100644 index 0000000..fa3bd90 --- /dev/null +++ b/examples/deprecated_curl_example.cpp @@ -0,0 +1,17 @@ + +#include + +#include +#include + + +int main(int argc, char** argv) +{ + GitHub::CurlBackend::Api github; + GitHub::Request request(github.connect()); + + std::cout << request.getRateLimit() << '\n'; + std::cout << request.getUserInfo("Kicer86") << '\n'; + + return 0; +} diff --git a/examples/deprecated_qt_example.cpp b/examples/deprecated_qt_example.cpp new file mode 100644 index 0000000..dd35f87 --- /dev/null +++ b/examples/deprecated_qt_example.cpp @@ -0,0 +1,22 @@ + +#include +#include +#include + +#include +#include + + +int main(int argc, char** argv) +{ + QCoreApplication qapp(argc, argv); + QNetworkAccessManager manager; + + GitHub::QtBackend::Api github(manager); + GitHub::Request request(github.connect()); + + qInfo() << request.getRateLimit().c_str(); + qInfo() << request.getUserInfo("Kicer86").c_str(); + + return 0; +} diff --git a/examples/qt_example.cpp b/examples/qt_example.cpp index dd35f87..a53c64f 100644 --- a/examples/qt_example.cpp +++ b/examples/qt_example.cpp @@ -3,8 +3,9 @@ #include #include -#include -#include +#include +#include +#include int main(int argc, char** argv) @@ -12,8 +13,8 @@ int main(int argc, char** argv) QCoreApplication qapp(argc, argv); QNetworkAccessManager manager; - GitHub::QtBackend::Api github(manager); - GitHub::Request request(github.connect()); + auto connection = cpp_restapi::GitHub::ConnectionBuilder().build(manager); + cpp_restapi::GitHub::Request request(std::move(connection)); qInfo() << request.getRateLimit().c_str(); qInfo() << request.getUserInfo("Kicer86").c_str(); From 92fdc3aa64d7d4edb42c11e1f5d7845259d0753b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 11:33:24 +0100 Subject: [PATCH 18/24] Update doxygen documentation --- .../cpp_restapi/github/connection_builder.hpp | 20 +++++++++++++++++++ .../cpp_restapi/github/github_api_curl.hpp | 2 +- include/cpp_restapi/github/github_api_qt.hpp | 2 +- include/cpp_restapi/github/igithub_api.hpp | 4 ++-- include/cpp_restapi/github/request.hpp | 8 ++++---- 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/include/cpp_restapi/github/connection_builder.hpp b/include/cpp_restapi/github/connection_builder.hpp index b894466..dab7319 100644 --- a/include/cpp_restapi/github/connection_builder.hpp +++ b/include/cpp_restapi/github/connection_builder.hpp @@ -10,6 +10,10 @@ namespace cpp_restapi::GitHub { + /** + * @brief Connection build for GitHub api + */ + class ConnectionBuilder { public: @@ -20,6 +24,13 @@ namespace cpp_restapi::GitHub ConnectionBuilder(const ConnectionBuilder &) = delete; + /** + * @brief change github api address. + * @param address api address + * + * Default value is https://api.github.com + * Api address should not be change in normal conditions. It is used for testing. + */ ConnectionBuilder& setAddress(const std::string& address) { m_address = address; @@ -27,6 +38,10 @@ namespace cpp_restapi::GitHub return *this; } + /** + * @brief set token used for authorization + * @param token token to be used for authorization + */ ConnectionBuilder& setToken(const std::string& token) { m_headerEntries.emplace("Authorization", "token " + token); @@ -34,6 +49,11 @@ namespace cpp_restapi::GitHub return *this; } + /** + * @brief build @ref cpp_restapi::IConnection object + * @tparam CT connection type (Qt or Curl backend). @ref cpp_restapi::CurlBackend::Connection or @ref cpp_restapi::QtBackend::Connection + * @param args backend specific arguments to be passed to connection. + */ template std::unique_ptr build(Args&&... args) { diff --git a/include/cpp_restapi/github/github_api_curl.hpp b/include/cpp_restapi/github/github_api_curl.hpp index bffeee9..7c93196 100644 --- a/include/cpp_restapi/github/github_api_curl.hpp +++ b/include/cpp_restapi/github/github_api_curl.hpp @@ -11,7 +11,7 @@ namespace cpp_restapi::GitHub::CurlBackend { /** - * @brief Class for establishing connection with GitHub api with Curl. Deprecated, use GitHubConnectionBuilder instead + * @brief Class for establishing connection with GitHub api with Curl. Deprecated, use @ref cpp_restapi::GitHub::ConnectionBuilder instead */ class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { diff --git a/include/cpp_restapi/github/github_api_qt.hpp b/include/cpp_restapi/github/github_api_qt.hpp index e642240..b500b54 100644 --- a/include/cpp_restapi/github/github_api_qt.hpp +++ b/include/cpp_restapi/github/github_api_qt.hpp @@ -20,7 +20,7 @@ namespace cpp_restapi::GitHub::QtBackend { /** - * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager. Deprecated, use GitHubConnectionBuilder instead + * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager. Deprecated, use @ref cpp_restapi::GitHub::ConnectionBuilder instead */ class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { diff --git a/include/cpp_restapi/github/igithub_api.hpp b/include/cpp_restapi/github/igithub_api.hpp index 5f25486..a4a5ad2 100644 --- a/include/cpp_restapi/github/igithub_api.hpp +++ b/include/cpp_restapi/github/igithub_api.hpp @@ -12,7 +12,7 @@ namespace cpp_restapi::GitHub { /** - * @brief common interface for all api backends. Deprecated, use GitHubConnectionBuilder instead + * @brief common interface for all api backends. Deprecated, use @ref cpp_restapi::GitHub::ConnectionBuilder instead */ class [[deprecated("Use GitHubConnectionBuilder")]] IApi { @@ -22,7 +22,7 @@ class [[deprecated("Use GitHubConnectionBuilder")]] IApi /** * @brief open authorized connection with GitHub api * @param token GitHub's authentication token. One can be generated on https://github.com/settings/tokens - * @return \ref GitHub::IConnection object which can be used with \ref GitHub::Request + * @return \ref cpp_restapi::IConnection object which can be used with \ref cpp_restapi::GitHub::Request */ virtual std::unique_ptr connect(const std::string& token) = 0; diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp index e8b80a8..6a14804 100644 --- a/include/cpp_restapi/github/request.hpp +++ b/include/cpp_restapi/github/request.hpp @@ -18,8 +18,8 @@ namespace cpp_restapi::GitHub * executed on GitHub's api. * * Before one can use it a connection with github needs to be established. - * Use \ref GitHub::QtBackend::Api or \ref GitHub::CurlBackend::Api - * to construct an \ref GitHub::IConnection object. + * Use \ref cpp_restapi::GitHub::ConnectionBuilder + * to construct a \ref cpp_restapi::IConnection object. * * All methods return a response in json format. */ @@ -79,7 +79,6 @@ namespace cpp_restapi::GitHub */ std::string listUserRepo(const std::string& user); - // --------------- user info related api /** @@ -97,6 +96,7 @@ namespace cpp_restapi::GitHub * @return std::string list of gitub account */ std::string listUsers(); + /** * @brief Provides publicly available information about * someone with a GitHub account. @@ -106,7 +106,6 @@ namespace cpp_restapi::GitHub */ std::string getUser(const std::string& username); - // issues related api methods /** * @brief List issues assigned to the authenticated user @@ -263,6 +262,7 @@ namespace cpp_restapi::GitHub * The days array is a group of commits per day, starting on Sunday */ std::string getLastYearCommit(const std::string& owner, const std::string& repo); + /** * @brief Get all contributor commit activity * From f425b405846c451e19c2bd9af59c9ab9063522b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 11:49:00 +0100 Subject: [PATCH 19/24] Switch to shared_ptrs for IConnection --- examples/curl_example.cpp | 2 +- examples/qt_example.cpp | 2 +- include/cpp_restapi/github/connection_builder.hpp | 4 ++-- include/cpp_restapi/github/request.hpp | 4 ++-- src/services/github/request.cpp | 2 +- tests/api_tests.cpp | 8 ++++---- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/curl_example.cpp b/examples/curl_example.cpp index 541ce9b..7857211 100644 --- a/examples/curl_example.cpp +++ b/examples/curl_example.cpp @@ -9,7 +9,7 @@ int main(int argc, char** argv) { auto connection = cpp_restapi::GitHub::ConnectionBuilder().build(); - cpp_restapi::GitHub::Request request(std::move(connection)); + cpp_restapi::GitHub::Request request(connection); std::cout << request.getRateLimit() << '\n'; std::cout << request.getUserInfo("Kicer86") << '\n'; diff --git a/examples/qt_example.cpp b/examples/qt_example.cpp index a53c64f..0c67ae5 100644 --- a/examples/qt_example.cpp +++ b/examples/qt_example.cpp @@ -14,7 +14,7 @@ int main(int argc, char** argv) QNetworkAccessManager manager; auto connection = cpp_restapi::GitHub::ConnectionBuilder().build(manager); - cpp_restapi::GitHub::Request request(std::move(connection)); + cpp_restapi::GitHub::Request request(connection); qInfo() << request.getRateLimit().c_str(); qInfo() << request.getUserInfo("Kicer86").c_str(); diff --git a/include/cpp_restapi/github/connection_builder.hpp b/include/cpp_restapi/github/connection_builder.hpp index dab7319..7e8d8b0 100644 --- a/include/cpp_restapi/github/connection_builder.hpp +++ b/include/cpp_restapi/github/connection_builder.hpp @@ -55,9 +55,9 @@ namespace cpp_restapi::GitHub * @param args backend specific arguments to be passed to connection. */ template - std::unique_ptr build(Args&&... args) + std::shared_ptr build(Args&&... args) { - return std::make_unique(std::forward(args)..., m_address, m_headerEntries); + return std::make_shared(std::forward(args)..., m_address, m_headerEntries); } private: diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp index 6a14804..601778f 100644 --- a/include/cpp_restapi/github/request.hpp +++ b/include/cpp_restapi/github/request.hpp @@ -26,7 +26,7 @@ namespace cpp_restapi::GitHub class GITHUB_API_EXPORT Request { public: - Request(std::unique_ptr); + Request(std::shared_ptr); Request(const Request &) = delete; ~Request(); @@ -505,7 +505,7 @@ namespace cpp_restapi::GitHub std::string repoContributors(const std::string& owner, const std::string& repo); private: - std::unique_ptr m_connection; + std::shared_ptr m_connection; std::string doRequest(const std::string &); }; diff --git a/src/services/github/request.cpp b/src/services/github/request.cpp index 73ecb36..d064eb8 100644 --- a/src/services/github/request.cpp +++ b/src/services/github/request.cpp @@ -10,7 +10,7 @@ namespace cpp_restapi::GitHub { - Request::Request(std::unique_ptr connection) + Request::Request(std::shared_ptr connection) : m_connection(std::move(connection)) { } diff --git a/tests/api_tests.cpp b/tests/api_tests.cpp index d2e5435..fa22f87 100644 --- a/tests/api_tests.cpp +++ b/tests/api_tests.cpp @@ -28,7 +28,7 @@ namespace T buildApi(); template - std::unique_ptr buildNewApi(); + std::shared_ptr buildNewApi(); template<> GitHub::CurlBackend::Api buildApi() @@ -44,13 +44,13 @@ namespace } template<> - std::unique_ptr buildNewApi() + std::shared_ptr buildNewApi() { return GitHub::ConnectionBuilder().setAddress(std::string("http://localhost:") + std::to_string(port)).build(); } template<> - std::unique_ptrbuildNewApi() + std::shared_ptrbuildNewApi() { static QNetworkAccessManager networkmanager; return GitHub::ConnectionBuilder().setAddress(std::string("http://localhost:") + std::to_string(port)).build(networkmanager); @@ -113,7 +113,7 @@ TYPED_TEST(ApiTest, newInterface) using Connection = typename BackendTraits::Connection; auto connection = buildNewApi(); - GitHub::Request request(std::move(connection)); + GitHub::Request request(connection); const auto info = request.getUserInfo("userName1234"); EXPECT_EQ(info, "{\"id\":1234,\"login\":\"userName1234\"}\n"); From bf832e229d810d99a9a1fd4f829ef63d858c071b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 11:55:45 +0100 Subject: [PATCH 20/24] Update README --- README.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a400116..63cdbe7 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,17 @@ # GitHub API for c++ -This is a c++ library for accessing GitHub REST API v3. +This is a c++ library originally written for accessing GitHub REST API v3. +Currently reorganized to be easily used with any API available. -For connection with GitHub Qt5/6 or libcurl are needed. -It is up to the user which to use. - -Currently offered functionality is limited but very easy to extend. +It supports two backends for establishing connections with remote API servers: +Qt5/6 and Curl. ## How to use it This is a CMake based project and is meant to be included as a subproject. Simply embed github_api's sources in your project, -choose which http backend you prefer and include github_api project in your CMakeLists.txt like this: +choose which http backend you prefer (both can be used simoultanously) and include github_api project in your CMakeLists.txt like this: ```cmake set(GitHubAPI_QtBackend ON) # use this line if you prefer Qt backend @@ -42,8 +41,9 @@ Set GitHubAPI_UseQt6 CMake variable to TRUE to use Qt6. #include #include -#include -#include +#include +#include +#include int main(int argc, char** argv) @@ -51,8 +51,8 @@ int main(int argc, char** argv) QCoreApplication qapp(argc, argv); QNetworkAccessManager manager; - GitHub::QtBackend::Api github(manager); - GitHub::Request request(github.connect()); + auto connection = cpp_restapi::GitHub::ConnectionBuilder().build(manager); + cpp_restapi::GitHub::Request request(connection); qInfo() << request.getRateLimit().c_str(); qInfo() << request.getUserInfo("Kicer86").c_str(); @@ -66,14 +66,15 @@ int main(int argc, char** argv) ```c++ #include -#include -#include +#include +#include +#include int main(int argc, char** argv) { - GitHub::CurlBackend::Api github; - GitHub::Request request(github.connect()); + auto connection = cpp_restapi::GitHub::ConnectionBuilder().build(); + cpp_restapi::GitHub::Request request(connection); std::cout << request.getRateLimit() << '\n'; std::cout << request.getUserInfo("Kicer86") << '\n'; From 091caa21e0b3e1585240936a1948a114a9f6d920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 12:33:43 +0100 Subject: [PATCH 21/24] Rename project --- CMakeLists.txt | 12 ++++++----- Doxyfile | 2 +- README.md | 20 ++++++++++++------- examples/CMakeLists.txt | 2 +- include/cpp_restapi/curl_connection.hpp | 4 ++-- .../cpp_restapi/github/github_api_curl.hpp | 4 ++-- include/cpp_restapi/github/github_api_qt.hpp | 4 ++-- include/cpp_restapi/github/request.hpp | 4 ++-- include/cpp_restapi/qt_connection.hpp | 4 ++-- src/curl_backend/CMakeLists.txt | 6 +++--- src/qt_backend/CMakeLists.txt | 6 +++--- 11 files changed, 38 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2508b7d..9e0d15f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.16) -project(GitHubAPI) +project(cppRestAPI) if(POLICY CMP0135) cmake_policy(SET CMP0135 NEW) @@ -30,14 +30,14 @@ if(GitHubAPI_Tests) set(GitHubAPI_CurlBackend ON) endif() -add_library(github_api +add_library(cpp_restapi src/base_connection.cpp src/header_utils.cpp src/services/github/github_api_base.cpp src/services/github/request.cpp ) -target_include_directories(github_api +target_include_directories(cpp_restapi PUBLIC ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/include @@ -45,12 +45,14 @@ target_include_directories(github_api ${JSONCPP_INCLUDE_DIRS} ) -target_link_libraries(github_api +target_link_libraries(cpp_restapi PRIVATE ${JSONCPP_LIBRARIES} ) -generate_export_header(github_api) +generate_export_header(cpp_restapi) + +add_library(github_api ALIAS cpp_restapi) if(NOT GitHubAPI_QtBackend AND NOT GitHubAPI_CurlBackend) message(FATAL_ERROR "No backend was chosen. Set either GitHubAPI_QtBackend or GitHubAPI_CurlBackend variable to ON") diff --git a/Doxyfile b/Doxyfile index b2c44ef..430720f 100644 --- a/Doxyfile +++ b/Doxyfile @@ -42,7 +42,7 @@ DOXYFILE_ENCODING = UTF-8 # title of most generated pages and in a few other places. # The default value is: My Project. -PROJECT_NAME = GitHubApi +PROJECT_NAME = cpp Rest API # The PROJECT_NUMBER tag can be used to enter a project or revision number. This # could be handy for archiving the generated documentation or if some version diff --git a/README.md b/README.md index 63cdbe7..25f2e04 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,36 @@ -# GitHub API for c++ + +# Rest API for c++ This is a c++ library originally written for accessing GitHub REST API v3. -Currently reorganized to be easily used with any API available. +Currently reorganized to be easily used with any Rest API available. It supports two backends for establishing connections with remote API servers: Qt5/6 and Curl. +##### Warning: +As the library was not yet fully renamed (to provide backward compatibility) from being GitHub only API (github_api) +to more general one (cpp_restapi), github_api and cpp_restapi names may occur interchangeably. +Do not use classes marked as deprecated in new projects. + ## How to use it This is a CMake based project and is meant to be included as a subproject. -Simply embed github_api's sources in your project, -choose which http backend you prefer (both can be used simoultanously) and include github_api project in your CMakeLists.txt like this: +Simply embed cpp_restapi's sources in your project, +choose which http backend you prefer (both can be used simoultanously) and include cpp_restapi project in your CMakeLists.txt like this: ```cmake set(GitHubAPI_QtBackend ON) # use this line if you prefer Qt backend set(GitHubAPI_CurlBackend ON) # use this line if you prefer Curl backend -add_subdirectory(github_api) +add_subdirectory(cpp_restapi) ``` -Then you can link your application against github_api: +Then you can link your application against cpp_restapi: ```cmake target_link_libraries(app PRIVATE - github_api + cpp_restapi ) ``` diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 23ca556..4b929ed 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -36,5 +36,5 @@ target_link_libraries(qt_example target_link_libraries(curl_example PRIVATE - github_api + cpp_restapi ) diff --git a/include/cpp_restapi/curl_connection.hpp b/include/cpp_restapi/curl_connection.hpp index 7b98e02..57c63ed 100644 --- a/include/cpp_restapi/curl_connection.hpp +++ b/include/cpp_restapi/curl_connection.hpp @@ -3,12 +3,12 @@ #define CONNECTION_QT_HPP #include "base_connection.hpp" -#include "github_api_export.h" +#include "cpp_restapi_export.h" namespace cpp_restapi { namespace CurlBackend { - class GITHUB_API_EXPORT Connection: public BaseConnection + class CPP_RESTAPI_EXPORT Connection: public BaseConnection { public: Connection(const std::string& address, const std::map& headerEntries); diff --git a/include/cpp_restapi/github/github_api_curl.hpp b/include/cpp_restapi/github/github_api_curl.hpp index 7c93196..1fa39c4 100644 --- a/include/cpp_restapi/github/github_api_curl.hpp +++ b/include/cpp_restapi/github/github_api_curl.hpp @@ -6,14 +6,14 @@ #include "cpp_restapi/iconnection.hpp" #include "github_api_base.hpp" -#include "github_api_export.h" +#include "cpp_restapi_export.h" namespace cpp_restapi::GitHub::CurlBackend { /** * @brief Class for establishing connection with GitHub api with Curl. Deprecated, use @ref cpp_restapi::GitHub::ConnectionBuilder instead */ - class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase + class CPP_RESTAPI_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { public: Api(const std::string& addr = "https://api.github.com"); diff --git a/include/cpp_restapi/github/github_api_qt.hpp b/include/cpp_restapi/github/github_api_qt.hpp index b500b54..de2fc3c 100644 --- a/include/cpp_restapi/github/github_api_qt.hpp +++ b/include/cpp_restapi/github/github_api_qt.hpp @@ -12,7 +12,7 @@ #include "cpp_restapi/iconnection.hpp" #include "github_api_base.hpp" -#include "github_api_export.h" +#include "cpp_restapi_export.h" class QNetworkAccessManager; @@ -22,7 +22,7 @@ namespace cpp_restapi::GitHub::QtBackend /** * @brief Class for establishing connection with GitHub api with Qt's QNetworkAccessManager. Deprecated, use @ref cpp_restapi::GitHub::ConnectionBuilder instead */ - class GITHUB_API_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase + class CPP_RESTAPI_DEPRECATED_EXPORT Api: public cpp_restapi::GitHubBase { public: Api(QNetworkAccessManager &, const QString& addr = "https://api.github.com"); diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp index 601778f..d1921c5 100644 --- a/include/cpp_restapi/github/request.hpp +++ b/include/cpp_restapi/github/request.hpp @@ -6,7 +6,7 @@ #include #include -#include "github_api_export.h" +#include "cpp_restapi_export.h" namespace cpp_restapi::GitHub @@ -23,7 +23,7 @@ namespace cpp_restapi::GitHub * * All methods return a response in json format. */ - class GITHUB_API_EXPORT Request + class CPP_RESTAPI_EXPORT Request { public: Request(std::shared_ptr); diff --git a/include/cpp_restapi/qt_connection.hpp b/include/cpp_restapi/qt_connection.hpp index 8cb88ff..88df6df 100644 --- a/include/cpp_restapi/qt_connection.hpp +++ b/include/cpp_restapi/qt_connection.hpp @@ -7,14 +7,14 @@ #include #include "base_connection.hpp" -#include "github_api_export.h" +#include "cpp_restapi_export.h" class QNetworkAccessManager; namespace cpp_restapi { namespace QtBackend { - class GITHUB_API_EXPORT Connection: public QObject, public BaseConnection + class CPP_RESTAPI_EXPORT Connection: public QObject, public BaseConnection { public: Connection(QNetworkAccessManager &, const std::string& address, const std::map& headerEntries); diff --git a/src/curl_backend/CMakeLists.txt b/src/curl_backend/CMakeLists.txt index e2b4c12..6cd1bb6 100644 --- a/src/curl_backend/CMakeLists.txt +++ b/src/curl_backend/CMakeLists.txt @@ -1,19 +1,19 @@ find_package(CURL REQUIRED) -target_sources(github_api +target_sources(cpp_restapi PRIVATE ${PROJECT_SOURCE_DIR}/include/github_api/github_api_curl.hpp connection.cpp github_api.cpp ) -target_link_libraries(github_api +target_link_libraries(cpp_restapi PUBLIC CURL::libcurl ) -target_include_directories(github_api +target_include_directories(cpp_restapi PRIVATE ${PROJECT_SOURCE_DIR}/src ) diff --git a/src/qt_backend/CMakeLists.txt b/src/qt_backend/CMakeLists.txt index f4f9e40..ab319bb 100644 --- a/src/qt_backend/CMakeLists.txt +++ b/src/qt_backend/CMakeLists.txt @@ -5,19 +5,19 @@ else() find_package(Qt5 5.15 REQUIRED COMPONENTS Core Network) endif() -target_sources(github_api PRIVATE +target_sources(cpp_restapi PRIVATE ${PROJECT_SOURCE_DIR}/include/github_api/github_api_qt.hpp connection.cpp github_api.cpp ) -target_link_libraries(github_api +target_link_libraries(cpp_restapi PUBLIC Qt::Core Qt::Network ) -target_include_directories(github_api +target_include_directories(cpp_restapi PRIVATE ${PROJECT_SOURCE_DIR}/src ) From 801a8f2e115f2b7056601795830b9b80f8fd6ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 22:49:07 +0100 Subject: [PATCH 22/24] Add header guard --- tests/github_server_mock.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/github_server_mock.hpp b/tests/github_server_mock.hpp index dff4f63..229ae99 100644 --- a/tests/github_server_mock.hpp +++ b/tests/github_server_mock.hpp @@ -1,4 +1,8 @@ +#ifndef GITHUB_SERVER_MOCK_HPP_INCLUDED +#define GITHUB_SERVER_MOCK_HPP_INCLUDED + + #include #include @@ -18,3 +22,5 @@ class GithubServerMock: public httpmock::MockServer { (override) ); }; + +#endif From ebfed96b447b7339bbe244301036f9e936e5002d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Nov 2023 22:49:19 +0100 Subject: [PATCH 23/24] Rename github_api_root to cpp_restapi_root --- examples/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4b929ed..235c1a8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -10,7 +10,7 @@ endif() set(GitHubAPI_UseQt6 ${Qt6_FOUND}) set(GitHubAPI_QtBackend ON) set(GitHubAPI_CurlBackend ON) -add_subdirectory(.. github_api_root) #include directory with github_api +add_subdirectory(.. cpp_restapi_root) #include directory with github_api add_executable(deprecated_qt_example deprecated_qt_example.cpp) add_executable(deprecated_curl_example deprecated_curl_example.cpp) From 5da1432a489f3280d6c5858ab99e8678f8539f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sun, 26 Nov 2023 10:58:09 +0100 Subject: [PATCH 24/24] Provide more examples and improve documentation --- README.md | 71 +++++++++++++++++++++-- examples/CMakeLists.txt | 12 ++++ examples/bare_curl_connection_example.cpp | 16 +++++ examples/bare_qt_connection_example.cpp | 21 +++++++ 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 examples/bare_curl_connection_example.cpp create mode 100644 examples/bare_qt_connection_example.cpp diff --git a/README.md b/README.md index 25f2e04..4823e58 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ It supports two backends for establishing connections with remote API servers: Qt5/6 and Curl. ##### Warning: -As the library was not yet fully renamed (to provide backward compatibility) from being GitHub only API (github_api) -to more general one (cpp_restapi), github_api and cpp_restapi names may occur interchangeably. +The library is being renamed from GitHub_API to cpp_RestAPI. +At this moment, to provide backward compatibility, old interfaces are still available but are about to be removed. Do not use classes marked as deprecated in new projects. ## How to use it @@ -36,11 +36,68 @@ target_link_libraries(app and that's all. +##### Note: +Depending on your choice of backend you may need to install libcurl and/or Qt libraries. + Qt backend can be compiled with Qt5 (default) or Qt6. Set GitHubAPI_UseQt6 CMake variable to TRUE to use Qt6. -## Qt example +## Examples + +## Simplest usage + +```c++ +#include + +#include + + +int main(int argc, char** argv) +{ + // Access The Star Wars API + cpp_restapi::CurlBackend::Connection connection("https://swapi.dev/api", {}); + + std::cout << connection.get("people/1") << '\n'; + std::cout << connection.get("starships/12/") << '\n'; + + return 0; +} +``` + +This example accesses The Star Wars API using curl backend +As you can see it is enought to instantiate `cpp_restapi::CurlBackend::Connection` object providing API url and after that request can be made. + +Qt version: +```c++ +#include +#include +#include + +#include + + +int main(int argc, char** argv) +{ + QCoreApplication qapp(argc, argv); + QNetworkAccessManager manager; + + // Access The Star Wars API + cpp_restapi::QtBackend::Connection connection(manager, "https://swapi.dev/api", {}); + + std::cout << connection.get("people/1") << '\n'; + std::cout << connection.get("starships/12/") << '\n'; + + return 0; +} +``` + +### Dedicated GitHub helpers + +For accessing GitHub API it is possible to use exactly the same apporach as presented above. +However, for conveniance, there are also additional helpers available: + +#### Qt example ```c++ #include @@ -67,7 +124,13 @@ int main(int argc, char** argv) } ``` -## libcurl example +Here connection is being build with `ConnectionBuilder`. Builder provides methods for setting additional connection parameters (passed as a second argument to `Connection` after API url). +It also sets the API url automatically. +Refer documentation of `ConnectionBuilder` for more details. + +Additionaly there is also `cpp_restapi::GitHub::Request` class available which comes with accessors to most common API requests. + +#### libcurl example ```c++ #include diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 235c1a8..c90c5c2 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -16,6 +16,8 @@ add_executable(deprecated_qt_example deprecated_qt_example.cpp) add_executable(deprecated_curl_example deprecated_curl_example.cpp) add_executable(qt_example qt_example.cpp) add_executable(curl_example curl_example.cpp) +add_executable(bare_curl_connection_example bare_curl_connection_example.cpp) +add_executable(bare_qt_connection_example bare_qt_connection_example.cpp) target_link_libraries(deprecated_qt_example PRIVATE @@ -38,3 +40,13 @@ target_link_libraries(curl_example PRIVATE cpp_restapi ) + +target_link_libraries(bare_curl_connection_example + PRIVATE + cpp_restapi +) + +target_link_libraries(bare_qt_connection_example + PRIVATE + cpp_restapi +) diff --git a/examples/bare_curl_connection_example.cpp b/examples/bare_curl_connection_example.cpp new file mode 100644 index 0000000..784caad --- /dev/null +++ b/examples/bare_curl_connection_example.cpp @@ -0,0 +1,16 @@ + +#include + +#include + + +int main(int argc, char** argv) +{ + // Access The Star Wars API + cpp_restapi::CurlBackend::Connection connection("https://swapi.dev/api", {}); + + std::cout << connection.get("people/1") << '\n'; + std::cout << connection.get("starships/12/") << '\n'; + + return 0; +} diff --git a/examples/bare_qt_connection_example.cpp b/examples/bare_qt_connection_example.cpp new file mode 100644 index 0000000..22bd7c1 --- /dev/null +++ b/examples/bare_qt_connection_example.cpp @@ -0,0 +1,21 @@ + +#include +#include +#include + +#include + + +int main(int argc, char** argv) +{ + QCoreApplication qapp(argc, argv); + QNetworkAccessManager manager; + + // Access The Star Wars API + cpp_restapi::QtBackend::Connection connection(manager, "https://swapi.dev/api", {}); + + std::cout << connection.get("people/1") << '\n'; + std::cout << connection.get("starships/12/") << '\n'; + + return 0; +}