Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kicer86/cpp_restapi
Rest API library for c++
https://github.com/kicer86/cpp_restapi
cpp github-api libcurl qt5 qt6 rest-api restapi
Last synced: about 1 month ago
JSON representation
Rest API library for c++
- Host: GitHub
- URL: https://github.com/kicer86/cpp_restapi
- Owner: Kicer86
- License: mit
- Created: 2020-11-09T16:37:07.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-14T07:54:35.000Z (4 months ago)
- Last Synced: 2024-09-29T07:42:03.031Z (about 1 month ago)
- Topics: cpp, github-api, libcurl, qt5, qt6, rest-api, restapi
- Language: C++
- Homepage:
- Size: 586 KB
- Stars: 45
- Watchers: 5
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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 Rest API available.It supports three backends for establishing connections with remote API servers:
Qt5/6, Curl and cpp-httplib.# Submodules
This repository has submodules which are not necessary to build and use this project.
You may need them if you want to build unit tests (`CppRestAPI_Tests` cmake varible set to `TRUE`).Another submodule is `vcpkg` which can simplify build by providing required dependencies.
Please mind that vcpkg uses **telemetry**.
Visit https://learn.microsoft.com/vcpkg/about/privacy for more details:## How to use it
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 (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_CppHttplibBackend ON) # use this line if you prefer cpp-httplib backend
add_subdirectory(cpp_restapi)
```Then you can link your application against cpp_restapi:
```cmake
target_link_libraries(app
PRIVATE
cpp_restapi
)
```and that's all.
##### Note:
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.## 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;
}
```cpp-httplib version:
```c++
#include#include
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.
However, for conveniance, there are also additional helpers available:#### Qt example
```c++
#include
#include
#include#include
#include
#includeint main(int argc, char** argv)
{
QCoreApplication qapp(argc, argv);
QNetworkAccessManager manager;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();return 0;
}
```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#include
#include
#includeint main(int argc, char** argv)
{
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build();
cpp_restapi::GitHub::Request request(connection);std::cout << request.getRateLimit() << '\n';
std::cout << request.getUserInfo("Kicer86") << '\n';return 0;
}
```#### cpp-httplib example:
```c++
#include#include
#include
#includeint main(int argc, char** argv)
{
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build();
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
Code documentation available at https://kicer86.github.io/cpp_restapi/index.html