Skip to content

Commit

Permalink
Merge pull request #46 from Kicer86/docs_update
Browse files Browse the repository at this point in the history
Update documentation and examples
  • Loading branch information
Kicer86 authored Jul 13, 2024
2 parents a7d787f + b7422b3 commit 1e265ed
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 44 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/linux-build-qt5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,5 @@ jobs:
with:
build-dir: ${{ runner.workspace }}/build
build-type: Release
configure-options: -DCppRestAPI_Tests=ON -DCppRestAPI_UseQt6=OFF
configure-options: -DCppRestAPI_Tests=ON -DCppRestAPI_UseQt6=OFF -DCppRestAPI_Examples=ON
run-test: true

- name: Build Examples
uses: ashutoshvarma/action-cmake-build@master
with:
source-dir: ${{ github.workspace }}/examples
build-dir: ${{ runner.workspace }}/build-examples
build-type: Release
configure-options: -DCppRestAPI_UseQt6=OFF
10 changes: 1 addition & 9 deletions .github/workflows/linux-build-qt6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,5 @@ jobs:
with:
build-dir: ${{ runner.workspace }}/build
build-type: Release
configure-options: -DCppRestAPI_Tests=ON -DCppRestAPI_UseQt6=ON
configure-options: -DCppRestAPI_Tests=ON -DCppRestAPI_UseQt6=ON -DCppRestAPI_Examples=ON
run-test: true

- name: Build Examples
uses: ashutoshvarma/action-cmake-build@master
with:
source-dir: ${{ github.workspace }}/examples
build-dir: ${{ runner.workspace }}/build-examples
build-type: Release
configure-options: -DCppRestAPI_UseQt6=ON
14 changes: 10 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ include(GenerateExportHeader)

option(CppRestAPI_QtBackend "Qt backend for CppRestAPI" OFF)
option(CppRestAPI_CurlBackend "libcurl backend for CppRestAPI" OFF)
option(CppRestAPI_HttplibBackend "cpp-httplib backend for CppRestAPI" OFF)
option(CppRestAPI_CppHttplibBackend "cpp-httplib backend for CppRestAPI" OFF)
option(CppRestAPI_Tests "Enable CppRestAPI tests. Forces all backends to be ON" OFF)
option(CppRestAPI_Examples "Build CppRestAPI examples. Forces all backends to be ON" OFF)

if(CppRestAPI_Tests)
if(CppRestAPI_Tests OR CppRestAPI_Examples)
set(CppRestAPI_QtBackend ON)
set(CppRestAPI_CurlBackend ON)
set(CppRestAPI_HttplibBackend ON)
set(CppRestAPI_CppHttplibBackend ON)
endif()

add_library(cpp_restapi
Expand Down Expand Up @@ -65,11 +66,16 @@ if(CppRestAPI_CurlBackend)
add_subdirectory(src/curl_backend)
endif()

if(CppRestAPI_HttplibBackend)
if(CppRestAPI_CppHttplibBackend)
add_subdirectory(src/cpp-httplib_backend)
endif()

if(CppRestAPI_Tests)
enable_testing()
add_subdirectory(tests)
endif()

if(CppRestAPI_Examples)
add_subdirectory(examples)
endif()

58 changes: 47 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
This is a c++ library originally written for accessing GitHub REST API v3.
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:
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.
It supports three backends for establishing connections with remote API servers:
Qt5/6, Curl and cpp-httplib.

# Submodules

Expand All @@ -26,11 +21,12 @@ Visit https://learn.microsoft.com/vcpkg/about/privacy for more details:
This is a CMake based project and is meant to be included as a subproject.

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:
choose which http backend you prefer (all can be used simoultanously) and include `cpp_restapi` project in your `CMakeLists.txt` like this:

```cmake
set(CppRestAPI_QtBackend ON) # use this line if you prefer Qt backend
set(CppRestAPI_CurlBackend ON) # use this line if you prefer Curl backend
set(CppRestAPI_QtBackend ON) # use this line if you prefer Qt backend
set(CppRestAPI_CurlBackend ON) # use this line if you prefer Curl backend
set(CppRestAPI_CppHttplibBackend ON) # use this line if you prefer cpp-httplib backend
add_subdirectory(cpp_restapi)
```

Expand All @@ -46,7 +42,7 @@ 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.
Depending on your choice of backend you may need to install libcurl, Qt and/or cpp-httplib libraries.

Qt backend can be compiled with Qt5 (default) or Qt6.
Set `CppRestAPI_UseQt6` CMake variable to `TRUE` to use Qt6.
Expand Down Expand Up @@ -101,6 +97,25 @@ int main(int argc, char** argv)
}
```

cpp-httplib version:
```c++
#include <iostream>

#include <cpp_restapi/cpp-httplib_connection.hpp>


int main(int argc, char** argv)
{
// Access The Star Wars API
cpp_restapi::CppHttplibBackend::Connection connection("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.<br>
Expand Down Expand Up @@ -162,6 +177,27 @@ int main(int argc, char** argv)
}
```
#### cpp-httplib example:
```c++
#include <iostream>
#include <cpp_restapi/cpp-httplib_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>
int main(int argc, char** argv)
{
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build<cpp_restapi::CppHttplibBackend::Connection>();
cpp_restapi::GitHub::Request request(connection);
std::cout << request.getRateLimit() << '\n';
std::cout << request.getUserInfo("Kicer86") << '\n';
return 0;
}
```

Also please look into 'examples' directory for details.

## Links
Expand Down
27 changes: 16 additions & 11 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@

cmake_minimum_required(VERSION 3.10)
project(GitHubApiExample)

find_package(Qt6 COMPONENTS Network Core)
if (NOT Qt6_FOUND)
find_package(Qt5 REQUIRED COMPONENTS Network Core)
if(CppRestAPI_UseQt6)
find_package(Qt6 REQUIRED COMPONENTS Core Network)
else()
find_package(Qt5 5.15 REQUIRED COMPONENTS Core Network)
endif()

set(CppRestAPI_UseQt6 ${Qt6_FOUND})
set(CppRestAPI_QtBackend ON)
set(CppRestAPI_CurlBackend ON)
add_subdirectory(.. cpp_restapi_root) #include directory with cpp_restapi

add_executable(qt_example qt_example.cpp)
add_executable(curl_example curl_example.cpp)
add_executable(cpp-httplib_example cpp-httplib_example.cpp)
add_executable(bare_curl_connection_example bare_curl_connection_example.cpp)
add_executable(bare_qt_connection_example bare_qt_connection_example.cpp)
add_executable(bare_cpp-httplib_connection_example bare_cpp-httplib_connection_example.cpp)

target_link_libraries(qt_example
PRIVATE
Expand All @@ -28,6 +23,11 @@ target_link_libraries(curl_example
cpp_restapi
)

target_link_libraries(cpp-httplib_example
PRIVATE
cpp_restapi
)

target_link_libraries(bare_curl_connection_example
PRIVATE
cpp_restapi
Expand All @@ -37,3 +37,8 @@ target_link_libraries(bare_qt_connection_example
PRIVATE
cpp_restapi
)

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

#include <iostream>

#include <cpp_restapi/cpp-httplib_connection.hpp>


int main(int argc, char** argv)
{
// Access The Star Wars API
cpp_restapi::CppHttplibBackend::Connection connection("https://swapi.dev/api", {});

std::cout << connection.get("people/1") << '\n';
std::cout << connection.get("starships/12/") << '\n';

return 0;
}
18 changes: 18 additions & 0 deletions examples/cpp-httplib_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include <iostream>

#include <cpp_restapi/cpp-httplib_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>


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

std::cout << request.getRateLimit() << '\n';
std::cout << request.getUserInfo("Kicer86") << '\n';

return 0;
}

0 comments on commit 1e265ed

Please sign in to comment.