Skip to content

Commit

Permalink
Merge pull request #23 from Kicer86/generic_core_extraction
Browse files Browse the repository at this point in the history
Extract generic part as a separate entity
  • Loading branch information
Kicer86 authored Nov 26, 2023
2 parents 4eebaf8 + 5da1432 commit 9373d7b
Show file tree
Hide file tree
Showing 36 changed files with 1,184 additions and 839 deletions.
23 changes: 12 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

cmake_minimum_required(VERSION 3.16)
project(GitHubAPI)
project(cppRestAPI)

if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
Expand All @@ -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)
Expand All @@ -27,31 +30,29 @@ if(GitHubAPI_Tests)
set(GitHubAPI_CurlBackend ON)
endif()

add_library(github_api
include/github_api/iconnection.hpp
include/github_api/igithub_api.hpp
include/github_api/request.hpp
add_library(cpp_restapi
src/base_connection.cpp
src/base_connection.hpp
src/header_utils.cpp
src/header_utils.hpp
src/request.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
PRIVATE SYSTEM
${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")
Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
110 changes: 90 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,121 @@
# GitHub API for c++

This is a c++ library for accessing GitHub REST API v3.
# Rest API for c++

For connection with GitHub Qt5/6 or libcurl are needed.
It is up to the user which to use.
This is a c++ library originally written for accessing GitHub REST API v3.
Currently reorganized to be easily used with any Rest API available.

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.

##### Warning:
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

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:
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
)
```

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 <iostream>

#include <cpp_restapi/curl_connection.hpp>


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 <iostream>
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <cpp_restapi/curl_connection.hpp>
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 <QCoreApplication>
#include <QDebug>
#include <QNetworkAccessManager>

#include <github_api/github_api_qt.hpp>
#include <github_api/request.hpp>
#include <cpp_restapi/qt_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>


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<cpp_restapi::QtBackend::Connection>(manager);
cpp_restapi::GitHub::Request request(connection);

qInfo() << request.getRateLimit().c_str();
qInfo() << request.getUserInfo("Kicer86").c_str();
Expand All @@ -61,19 +124,26 @@ 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 <iostream>
#include <github_api/github_api_curl.hpp>
#include <github_api/request.hpp>
#include <cpp_restapi/curl_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>
int main(int argc, char** argv)
{
GitHub::CurlBackend::Api github;
GitHub::Request request(github.connect());
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build<cpp_restapi::CurlBackend::Connection>();
cpp_restapi::GitHub::Request request(connection);
std::cout << request.getRateLimit() << '\n';
std::cout << request.getUserInfo("Kicer86") << '\n';
Expand Down
29 changes: 27 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@ 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)
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
github_api
Qt::Network
)

target_link_libraries(deprecated_curl_example
PRIVATE
github_api
)

target_link_libraries(qt_example
PRIVATE
Expand All @@ -23,5 +38,15 @@ target_link_libraries(qt_example

target_link_libraries(curl_example
PRIVATE
github_api
cpp_restapi
)

target_link_libraries(bare_curl_connection_example
PRIVATE
cpp_restapi
)

target_link_libraries(bare_qt_connection_example
PRIVATE
cpp_restapi
)
16 changes: 16 additions & 0 deletions examples/bare_curl_connection_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include <iostream>

#include <cpp_restapi/curl_connection.hpp>


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;
}
21 changes: 21 additions & 0 deletions examples/bare_qt_connection_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#include <iostream>
#include <QCoreApplication>
#include <QNetworkAccessManager>

#include <cpp_restapi/qt_connection.hpp>


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;
}
9 changes: 5 additions & 4 deletions examples/curl_example.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

#include <iostream>

#include <github_api/github_api_curl.hpp>
#include <github_api/request.hpp>
#include <cpp_restapi/curl_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>


int main(int argc, char** argv)
{
GitHub::CurlBackend::Api github;
GitHub::Request request(github.connect());
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build<cpp_restapi::CurlBackend::Connection>();
cpp_restapi::GitHub::Request request(connection);

std::cout << request.getRateLimit() << '\n';
std::cout << request.getUserInfo("Kicer86") << '\n';
Expand Down
17 changes: 17 additions & 0 deletions examples/deprecated_curl_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include <iostream>

#include <github_api/github_api_curl.hpp>
#include <github_api/request.hpp>


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;
}
22 changes: 22 additions & 0 deletions examples/deprecated_qt_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#include <QCoreApplication>
#include <QDebug>
#include <QNetworkAccessManager>

#include <github_api/github_api_qt.hpp>
#include <github_api/request.hpp>


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;
}
Loading

0 comments on commit 9373d7b

Please sign in to comment.