Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 11 additions & 56 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()

find_package(jsoncpp CONFIG)

if(jsoncpp_FOUND)
set(JSONCPP_LIBRARIES JsonCpp::JsonCpp)
else()
find_package(PkgConfig REQUIRED)

pkg_check_modules(JSONCPP REQUIRED jsoncpp)
endif()

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand All @@ -24,65 +14,31 @@ include(GenerateExportHeader)
option(CppRestAPI_QtBackend "Qt backend for CppRestAPI" OFF)
option(CppRestAPI_CurlBackend "libcurl backend for CppRestAPI" OFF)
option(CppRestAPI_CppHttplibBackend "cpp-httplib backend for CppRestAPI" OFF)
option(CppRestAPI_JsonPagination "JSON-aware Link-header pagination strategy (requires jsoncpp)" ON)
option(CppRestAPI_GitHub "GitHub API helpers (ConnectionBuilder, Request). Implies CppRestAPI_JsonPagination." ON)
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 OR CppRestAPI_Examples)
set(CppRestAPI_QtBackend ON)
set(CppRestAPI_CurlBackend ON)
set(CppRestAPI_CppHttplibBackend ON)
set(CppRestAPI_JsonPagination ON)
set(CppRestAPI_GitHub ON)
endif()

add_library(cpp_restapi
src/base_connection.cpp
src/threaded_connection.cpp
src/header_utils.cpp
src/sse_parser.cpp
src/link_header_pagination_strategy.cpp
src/services/github/request.cpp
)

target_include_directories(cpp_restapi
PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/include
PRIVATE SYSTEM
${JSONCPP_INCLUDE_DIRS}
)

target_link_libraries(cpp_restapi
PRIVATE
${JSONCPP_LIBRARIES}
)

generate_export_header(cpp_restapi)

configure_file(
${PROJECT_SOURCE_DIR}/include/cpp_restapi/version.hpp.in
${PROJECT_BINARY_DIR}/cpp_restapi/version.hpp
@ONLY
)

set_target_properties(cpp_restapi PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
if(CppRestAPI_GitHub AND NOT CppRestAPI_JsonPagination)
message(WARNING
"CppRestAPI_GitHub=ON requires CppRestAPI_JsonPagination=ON; "
"overriding the requested CppRestAPI_JsonPagination=OFF.")
set(CppRestAPI_JsonPagination ON)
endif()

if(NOT CppRestAPI_QtBackend AND NOT CppRestAPI_CurlBackend AND NOT CppRestAPI_CppHttplibBackend)
message(FATAL_ERROR "No backend was chosen. Set CppRestAPI_QtBackend, CppRestAPI_CurlBackend or CppRestAPI_CppHttplibBackend variable to ON")
endif()

if(CppRestAPI_QtBackend)
add_subdirectory(src/qt_backend)
endif()

if(CppRestAPI_CurlBackend)
add_subdirectory(src/curl_backend)
endif()

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

if(CppRestAPI_Tests)
enable_testing()
Expand All @@ -92,4 +48,3 @@ endif()
if(CppRestAPI_Examples)
add_subdirectory(examples)
endif()

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ target_link_libraries(app

and that's all.

##### JSON-aware pagination:
`cpp_restapi::LinkHeaderPaginationStrategy` is an RFC 5988 `Link`-header based
pagination strategy with JSON-aware merging (concatenates arrays, deep-merges
objects). It is built by default (controlled by the `CppRestAPI_JsonPagination`
CMake option) and adds a dependency on the `jsoncpp` library. To drop the
`jsoncpp` dependency entirely, configure with `-DCppRestAPI_JsonPagination=OFF`.

##### GitHub helpers:
`cpp_restapi::GitHub::ConnectionBuilder` and `cpp_restapi::GitHub::Request`
are convenience wrappers for the GitHub REST API. They are built by default
(controlled by the `CppRestAPI_GitHub` CMake option; enabling it automatically
enables `CppRestAPI_JsonPagination`). Disable with `-DCppRestAPI_GitHub=OFF`
if not needed.

##### Note:
Depending on your choice of backend you may need to install libcurl, Qt and/or cpp-httplib libraries.

Expand Down
13 changes: 11 additions & 2 deletions include/cpp_restapi/github/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
#ifndef REQUEST_HPP
#define REQUEST_HPP

#include <cpp_restapi/version.hpp>

#if !CPP_RESTAPI_HAS_GITHUB
#error "cpp_restapi was built without GitHub helpers. Rebuild with -DCppRestAPI_GitHub=ON to use cpp_restapi::GitHub::Request."
#endif

#include <memory>
#include <string>

#include <cpp_restapi/iconnection.hpp>
#include "cpp_restapi_export.h"
#include "cpp_restapi_github_export.h"


namespace cpp_restapi::GitHub
Expand All @@ -22,8 +28,11 @@ namespace cpp_restapi::GitHub
* to construct a \ref cpp_restapi::IConnection object.
*
* All methods return a response in json format.
*
* Provided by the optional `cpp_restapi_github` CMake target
* (which itself depends on `cpp_restapi_json_pagination` and jsoncpp).
*/
class CPP_RESTAPI_EXPORT Request
class CPP_RESTAPI_GITHUB_EXPORT Request
{
public:
Request(std::shared_ptr<cpp_restapi::IConnection>);
Expand Down
13 changes: 11 additions & 2 deletions include/cpp_restapi/link_header_pagination_strategy.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#ifndef LINK_HEADER_PAGINATION_STRATEGY_HPP_INCLUDED
#define LINK_HEADER_PAGINATION_STRATEGY_HPP_INCLUDED

#include <cpp_restapi/version.hpp>

#if !CPP_RESTAPI_HAS_JSON_PAGINATION
#error "cpp_restapi was built without JSON pagination support. Rebuild with -DCppRestAPI_JsonPagination=ON to use LinkHeaderPaginationStrategy."
#endif

#include <cpp_restapi/ipagination_strategy.hpp>
#include "cpp_restapi_export.h"
#include "cpp_restapi_json_pagination_export.h"

namespace cpp_restapi
{
Expand All @@ -13,8 +19,11 @@ namespace cpp_restapi
* Discovers the next page via the `Link: <url>; rel="next"` header
* and merges JSON responses by concatenating arrays or deep-merging objects.
* Used by GitHub API and other services that paginate with Link headers.
*
* Provided by the optional `cpp_restapi_json_pagination` CMake target
* (requires jsoncpp).
*/
class CPP_RESTAPI_EXPORT LinkHeaderPaginationStrategy: public IPaginationStrategy
class CPP_RESTAPI_JSON_PAGINATION_EXPORT LinkHeaderPaginationStrategy: public IPaginationStrategy
{
public:
std::string nextPageUrl(const std::string& responseHeaders) override;
Expand Down
19 changes: 19 additions & 0 deletions include/cpp_restapi/version.hpp.in
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
#ifndef CPP_RESTAPI_VERSION_HPP
#define CPP_RESTAPI_VERSION_HPP

#include "cpp_restapi_export.h"

#define CPP_RESTAPI_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define CPP_RESTAPI_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define CPP_RESTAPI_VERSION_PATCH @PROJECT_VERSION_PATCH@
#define CPP_RESTAPI_VERSION "@PROJECT_VERSION@"

// Feature macros: set to 1 when the corresponding optional component
// was enabled at build time, 0 otherwise. Public headers belonging to
// an optional component check these to fail with a readable diagnostic
// when included against a library that was built without the feature.
#cmakedefine01 CPP_RESTAPI_HAS_JSON_PAGINATION
#cmakedefine01 CPP_RESTAPI_HAS_GITHUB

namespace cpp_restapi
{
inline constexpr int versionMajor = @PROJECT_VERSION_MAJOR@;
inline constexpr int versionMinor = @PROJECT_VERSION_MINOR@;
inline constexpr int versionPatch = @PROJECT_VERSION_PATCH@;
inline constexpr char version[] = "@PROJECT_VERSION@";

/**
* @brief Returns the version string of the linked cpp_restapi library.
*
* Unlike the compile-time @ref version constant (which reflects the
* headers the consumer was built against), this function returns the
* version baked into the actually-linked shared library, allowing
* detection of header/library mismatch after an in-place upgrade.
*/
CPP_RESTAPI_EXPORT const char* runtimeVersion() noexcept;
}

#endif
83 changes: 83 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

add_library(cpp_restapi_core OBJECT
base_connection.cpp
header_utils.cpp
threaded_connection.cpp
sse_parser.cpp
)

set_target_properties(cpp_restapi_core PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_include_directories(cpp_restapi_core
PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/include
)

target_compile_definitions(cpp_restapi_core PRIVATE cpp_restapi_EXPORTS)

generate_export_header(cpp_restapi_core
BASE_NAME cpp_restapi
EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/cpp_restapi_export.h
)

# Map CMake options to cmakedefine01 inputs consumed by version.hpp.in.
set(CPP_RESTAPI_HAS_JSON_PAGINATION ${CppRestAPI_JsonPagination})
set(CPP_RESTAPI_HAS_GITHUB ${CppRestAPI_GitHub})

configure_file(
${PROJECT_SOURCE_DIR}/include/cpp_restapi/version.hpp.in
${PROJECT_BINARY_DIR}/cpp_restapi/version.hpp
@ONLY
)

if(CppRestAPI_QtBackend)
add_subdirectory(qt_backend)
endif()

if(CppRestAPI_CurlBackend)
add_subdirectory(curl_backend)
endif()

if(CppRestAPI_CppHttplibBackend)
add_subdirectory(cpp-httplib_backend)
endif()

if(CppRestAPI_JsonPagination)
add_subdirectory(json_pagination)
endif()

if(CppRestAPI_GitHub)
add_subdirectory(services/github)
endif()

# Umbrella library: the only artifact consumers link against. Composed
# entirely of OBJECT libraries plus its own version.cpp; type follows
# BUILD_SHARED_LIBS.
add_library(cpp_restapi version.cpp)

# version.cpp includes the configured cpp_restapi/version.hpp and
# cpp_restapi_export.h, so it needs the same include paths the core
# OBJECT lib publishes. CPP_RESTAPI_EXPORTS makes runtimeVersion() an
# exported symbol on shared builds.
target_include_directories(cpp_restapi
PRIVATE
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/include
)
target_compile_definitions(cpp_restapi PRIVATE cpp_restapi_EXPORTS)

target_link_libraries(cpp_restapi PUBLIC cpp_restapi_core)

if(TARGET cpp_restapi_json_pagination)
target_link_libraries(cpp_restapi PUBLIC cpp_restapi_json_pagination)
endif()

if(TARGET cpp_restapi_github)
target_link_libraries(cpp_restapi PUBLIC cpp_restapi_github)
endif()

set_target_properties(cpp_restapi PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
6 changes: 3 additions & 3 deletions src/cpp-httplib_backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ else()
pkg_check_modules(HTTPLIB REQUIRED cpp-httplib)
endif()

target_sources(cpp_restapi
target_sources(cpp_restapi_core
PRIVATE
connection.cpp
create_connection.cpp
sse_connection.cpp
)

target_link_libraries(cpp_restapi
target_link_libraries(cpp_restapi_core
PUBLIC
${HTTPLIB_LIBRARIES}
)

target_include_directories(cpp_restapi
target_include_directories(cpp_restapi_core
PRIVATE
${PROJECT_SOURCE_DIR}/src
)
6 changes: 3 additions & 3 deletions src/curl_backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

find_package(CURL REQUIRED)

target_sources(cpp_restapi
target_sources(cpp_restapi_core
PRIVATE
connection.cpp
create_connection.cpp
sse_connection.cpp
)

target_link_libraries(cpp_restapi
target_link_libraries(cpp_restapi_core
PUBLIC
CURL::libcurl
)

target_include_directories(cpp_restapi
target_include_directories(cpp_restapi_core
PRIVATE
${PROJECT_SOURCE_DIR}/src
)
8 changes: 4 additions & 4 deletions src/header_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

#include "header_utils.hpp"

#include <algorithm>
#include <cctype>
#include <regex>
#include <sstream>

#include "header_utils.hpp"


namespace HeaderUtils
{

std::string getNextPageUrl(const std::string& header)
{
std::istringstream header_(header);
Expand Down Expand Up @@ -36,5 +37,4 @@ namespace HeaderUtils

return link;
}

}
Loading
Loading