From 6be81fc65baac9d8dc488362badbef7862804b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Apr 2026 22:03:13 +0200 Subject: [PATCH 01/13] Split LinkHeaderPaginationStrategy and GitHub helpers into optional target Move src/link_header_pagination_strategy.cpp and src/services/github/request.cpp into a new CMake library target 'cpp_restapi_json_pagination'. This target is optional and controlled by the CppRestAPI_JsonPagination CMake option (ON by default; forced ON when tests or examples are enabled). The core 'cpp_restapi' library no longer depends on jsoncpp. Users that do not need JSON-aware pagination merging or the GitHub request/builder helpers can set -DCppRestAPI_JsonPagination=OFF and drop the jsoncpp build/run-time dependency completely. Public headers for the split classes now use a new export macro generated for the new target (CPP_RESTAPI_JSON_PAGINATION_EXPORT). Tests and GitHub examples are updated to link the new target in addition to cpp_restapi. README is updated with a short section describing the new library and its CMake option. --- CMakeLists.txt | 63 +++++++++++++------ README.md | 21 +++++++ examples/CMakeLists.txt | 3 + include/cpp_restapi/github/request.hpp | 7 ++- .../link_header_pagination_strategy.hpp | 7 ++- tests/CMakeLists.txt | 1 + 6 files changed, 79 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3568667..b9c35f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -24,6 +14,7 @@ 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 pagination and GitHub helpers (requires jsoncpp)" 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) @@ -31,6 +22,7 @@ if(CppRestAPI_Tests OR CppRestAPI_Examples) set(CppRestAPI_QtBackend ON) set(CppRestAPI_CurlBackend ON) set(CppRestAPI_CppHttplibBackend ON) + set(CppRestAPI_JsonPagination ON) endif() add_library(cpp_restapi @@ -38,21 +30,12 @@ add_library(cpp_restapi 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) @@ -84,6 +67,48 @@ if(CppRestAPI_CppHttplibBackend) add_subdirectory(src/cpp-httplib_backend) endif() +if(CppRestAPI_JsonPagination) + # Optional component pulling in the jsoncpp dependency. Contains the + # Link-header pagination strategy and GitHub request helpers. Users who + # do not need JSON-aware pagination do not have to link against it + # (and do not pay the jsoncpp dependency cost). + find_package(jsoncpp CONFIG) + + if(jsoncpp_FOUND) + set(JSONCPP_LIBRARIES JsonCpp::JsonCpp) + else() + find_package(PkgConfig REQUIRED) + + pkg_check_modules(JSONCPP REQUIRED jsoncpp) + endif() + + add_library(cpp_restapi_json_pagination + src/link_header_pagination_strategy.cpp + src/services/github/request.cpp + ) + + target_include_directories(cpp_restapi_json_pagination + PRIVATE SYSTEM + ${JSONCPP_INCLUDE_DIRS} + ) + + target_link_libraries(cpp_restapi_json_pagination + PUBLIC + cpp_restapi + PRIVATE + ${JSONCPP_LIBRARIES} + ) + + generate_export_header(cpp_restapi_json_pagination + BASE_NAME cpp_restapi_json_pagination + ) + + set_target_properties(cpp_restapi_json_pagination PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) +endif() + if(CppRestAPI_Tests) enable_testing() add_subdirectory(tests) diff --git a/README.md b/README.md index e217232..053a421 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,27 @@ target_link_libraries(app and that's all. +##### JSON-aware pagination and GitHub helpers: +The optional `cpp_restapi_json_pagination` library provides +`cpp_restapi::LinkHeaderPaginationStrategy` (RFC 5988 `Link`-header based pagination +with JSON-aware merging) and the `cpp_restapi::GitHub::Request` / `ConnectionBuilder` +helpers. It is built by default (controlled by the `CppRestAPI_JsonPagination` CMake +option, ON by default) and adds a dependency on the `jsoncpp` library. + +Applications that use JSON-aware pagination or the GitHub helpers must link against +it explicitly in addition to `cpp_restapi`: + +```cmake +target_link_libraries(app + PRIVATE + cpp_restapi + cpp_restapi_json_pagination +) +``` + +Applications that do not need those features can set +`-DCppRestAPI_JsonPagination=OFF` to drop the `jsoncpp` dependency entirely. + ##### Note: Depending on your choice of backend you may need to install libcurl, Qt and/or cpp-httplib libraries. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3ca973c..e726fae 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -28,17 +28,20 @@ add_executable(coroutine_qt_example coroutine_qt_example.cpp) target_link_libraries(github_qt_example PRIVATE cpp_restapi + cpp_restapi_json_pagination Qt::Network ) target_link_libraries(github_curl_example PRIVATE cpp_restapi + cpp_restapi_json_pagination ) target_link_libraries(github_cpp-httplib_example PRIVATE cpp_restapi + cpp_restapi_json_pagination ) target_link_libraries(bare_curl_connection_example diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp index b6801f6..8281bc0 100644 --- a/include/cpp_restapi/github/request.hpp +++ b/include/cpp_restapi/github/request.hpp @@ -6,7 +6,7 @@ #include #include -#include "cpp_restapi_export.h" +#include "cpp_restapi_json_pagination_export.h" namespace cpp_restapi::GitHub @@ -22,8 +22,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_json_pagination` CMake target + * (requires jsoncpp). */ - class CPP_RESTAPI_EXPORT Request + class CPP_RESTAPI_JSON_PAGINATION_EXPORT Request { public: Request(std::shared_ptr); diff --git a/include/cpp_restapi/link_header_pagination_strategy.hpp b/include/cpp_restapi/link_header_pagination_strategy.hpp index e8fde9a..7de68ee 100644 --- a/include/cpp_restapi/link_header_pagination_strategy.hpp +++ b/include/cpp_restapi/link_header_pagination_strategy.hpp @@ -2,7 +2,7 @@ #define LINK_HEADER_PAGINATION_STRATEGY_HPP_INCLUDED #include -#include "cpp_restapi_export.h" +#include "cpp_restapi_json_pagination_export.h" namespace cpp_restapi { @@ -13,8 +13,11 @@ namespace cpp_restapi * Discovers the next page via the `Link: ; 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; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 131296f..a2921d1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,6 +31,7 @@ target_link_libraries(api_tests ${HTTPLIB_LIBRARIES} cpp_restapi + cpp_restapi_json_pagination ) add_test(NAME api_tests COMMAND api_tests) From 4f414ec74b6ed5a293b595b7a0c1386dfe5e8ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Apr 2026 22:21:39 +0200 Subject: [PATCH 02/13] Split GitHub helpers into separate optional cpp_restapi_github target Move src/services/github/request.cpp out of cpp_restapi_json_pagination into a new optional library target cpp_restapi_github controlled by the new CppRestAPI_GitHub CMake option (ON by default). The target depends on cpp_restapi_json_pagination (for LinkHeaderPaginationStrategy used to merge paginated responses), so enabling CppRestAPI_GitHub automatically enables CppRestAPI_JsonPagination. This lets consumers that only need the generic Link-header pagination strategy skip pulling in the GitHub helpers, and lets consumers that do not use GitHub disable that surface independently. Public github/request.hpp now uses a new CPP_RESTAPI_GITHUB_EXPORT macro. Tests and GitHub examples are updated to link the new target. README is updated accordingly. --- CMakeLists.txt | 38 +++++++++++++++++++++++--- README.md | 36 ++++++++++++++++++------ examples/CMakeLists.txt | 6 ++-- include/cpp_restapi/github/request.hpp | 8 +++--- tests/CMakeLists.txt | 1 + 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b9c35f4..1d463c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,8 @@ 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 pagination and GitHub helpers (requires jsoncpp)" ON) +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) @@ -23,6 +24,12 @@ if(CppRestAPI_Tests OR CppRestAPI_Examples) set(CppRestAPI_CurlBackend ON) set(CppRestAPI_CppHttplibBackend ON) set(CppRestAPI_JsonPagination ON) + set(CppRestAPI_GitHub ON) +endif() + +if(CppRestAPI_GitHub AND NOT CppRestAPI_JsonPagination) + message(STATUS "CppRestAPI_GitHub implies CppRestAPI_JsonPagination; enabling it.") + set(CppRestAPI_JsonPagination ON) endif() add_library(cpp_restapi @@ -69,8 +76,8 @@ endif() if(CppRestAPI_JsonPagination) # Optional component pulling in the jsoncpp dependency. Contains the - # Link-header pagination strategy and GitHub request helpers. Users who - # do not need JSON-aware pagination do not have to link against it + # Link-header based pagination strategy. Users that do not need + # JSON-aware multi-page merging do not have to link against it # (and do not pay the jsoncpp dependency cost). find_package(jsoncpp CONFIG) @@ -84,7 +91,6 @@ if(CppRestAPI_JsonPagination) add_library(cpp_restapi_json_pagination src/link_header_pagination_strategy.cpp - src/services/github/request.cpp ) target_include_directories(cpp_restapi_json_pagination @@ -109,6 +115,30 @@ if(CppRestAPI_JsonPagination) ) endif() +if(CppRestAPI_GitHub) + # Optional component with GitHub API helpers (ConnectionBuilder, Request). + # Relies on LinkHeaderPaginationStrategy from cpp_restapi_json_pagination + # to merge paginated responses. + add_library(cpp_restapi_github + src/services/github/request.cpp + ) + + target_link_libraries(cpp_restapi_github + PUBLIC + cpp_restapi + cpp_restapi_json_pagination + ) + + generate_export_header(cpp_restapi_github + BASE_NAME cpp_restapi_github + ) + + set_target_properties(cpp_restapi_github PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) +endif() + if(CppRestAPI_Tests) enable_testing() add_subdirectory(tests) diff --git a/README.md b/README.md index 053a421..a73d300 100644 --- a/README.md +++ b/README.md @@ -39,15 +39,15 @@ target_link_libraries(app and that's all. -##### JSON-aware pagination and GitHub helpers: +##### JSON-aware pagination: The optional `cpp_restapi_json_pagination` library provides -`cpp_restapi::LinkHeaderPaginationStrategy` (RFC 5988 `Link`-header based pagination -with JSON-aware merging) and the `cpp_restapi::GitHub::Request` / `ConnectionBuilder` -helpers. It is built by default (controlled by the `CppRestAPI_JsonPagination` CMake -option, ON by default) and adds a dependency on the `jsoncpp` library. +`cpp_restapi::LinkHeaderPaginationStrategy` — 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. -Applications that use JSON-aware pagination or the GitHub helpers must link against -it explicitly in addition to `cpp_restapi`: +Applications that use JSON-aware pagination must link against it explicitly in +addition to `cpp_restapi`: ```cmake target_link_libraries(app @@ -57,8 +57,26 @@ target_link_libraries(app ) ``` -Applications that do not need those features can set -`-DCppRestAPI_JsonPagination=OFF` to drop the `jsoncpp` dependency entirely. +Applications that do not need it can set `-DCppRestAPI_JsonPagination=OFF` to +drop the `jsoncpp` dependency entirely. + +##### GitHub helpers: +The optional `cpp_restapi_github` library provides +`cpp_restapi::GitHub::ConnectionBuilder` and `cpp_restapi::GitHub::Request` — +convenience wrappers for the GitHub REST API. It depends on +`cpp_restapi_json_pagination` (and transitively on `jsoncpp`). It is built by +default (controlled by the `CppRestAPI_GitHub` CMake option; enabling it +automatically enables `CppRestAPI_JsonPagination`). + +Applications using the GitHub helpers must link against it explicitly: + +```cmake +target_link_libraries(app + PRIVATE + cpp_restapi + cpp_restapi_github +) +``` ##### Note: Depending on your choice of backend you may need to install libcurl, Qt and/or cpp-httplib libraries. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e726fae..1efba31 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -28,20 +28,20 @@ add_executable(coroutine_qt_example coroutine_qt_example.cpp) target_link_libraries(github_qt_example PRIVATE cpp_restapi - cpp_restapi_json_pagination + cpp_restapi_github Qt::Network ) target_link_libraries(github_curl_example PRIVATE cpp_restapi - cpp_restapi_json_pagination + cpp_restapi_github ) target_link_libraries(github_cpp-httplib_example PRIVATE cpp_restapi - cpp_restapi_json_pagination + cpp_restapi_github ) target_link_libraries(bare_curl_connection_example diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp index 8281bc0..17c82f1 100644 --- a/include/cpp_restapi/github/request.hpp +++ b/include/cpp_restapi/github/request.hpp @@ -6,7 +6,7 @@ #include #include -#include "cpp_restapi_json_pagination_export.h" +#include "cpp_restapi_github_export.h" namespace cpp_restapi::GitHub @@ -23,10 +23,10 @@ namespace cpp_restapi::GitHub * * All methods return a response in json format. * - * Provided by the optional `cpp_restapi_json_pagination` CMake target - * (requires jsoncpp). + * Provided by the optional `cpp_restapi_github` CMake target + * (which itself depends on `cpp_restapi_json_pagination` and jsoncpp). */ - class CPP_RESTAPI_JSON_PAGINATION_EXPORT Request + class CPP_RESTAPI_GITHUB_EXPORT Request { public: Request(std::shared_ptr); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a2921d1..c997f44 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,6 +32,7 @@ target_link_libraries(api_tests cpp_restapi cpp_restapi_json_pagination + cpp_restapi_github ) add_test(NAME api_tests COMMAND api_tests) From 43386326df93d6821058230644f99b95e0287bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Apr 2026 22:30:53 +0200 Subject: [PATCH 03/13] Re-export optional libraries through cpp_restapi umbrella target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Link cpp_restapi_json_pagination and cpp_restapi_github as INTERFACE dependencies of cpp_restapi. Downstream applications now just need `target_link_libraries(app PRIVATE cpp_restapi)` to transparently pull in the enabled optional components — no separate entries required for the pagination strategy or the GitHub helpers. To avoid a dependency cycle between cpp_restapi and cpp_restapi_json_pagination (which only needs the HeaderUtils helper from core), move src/header_utils.{hpp,cpp} into cpp_restapi_json_pagination. The helper has no other users in core. Tests and examples are simplified accordingly (no longer listing the optional targets explicitly). README is updated to describe the new, simpler usage. --- CMakeLists.txt | 10 +++++++++- README.md | 29 +++++++---------------------- examples/CMakeLists.txt | 3 --- tests/CMakeLists.txt | 2 -- 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d463c4..cc7d169 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,6 @@ endif() add_library(cpp_restapi src/base_connection.cpp src/threaded_connection.cpp - src/header_utils.cpp src/sse_parser.cpp ) @@ -90,6 +89,7 @@ if(CppRestAPI_JsonPagination) endif() add_library(cpp_restapi_json_pagination + src/header_utils.cpp src/link_header_pagination_strategy.cpp ) @@ -113,6 +113,10 @@ if(CppRestAPI_JsonPagination) VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) + + # Re-export via cpp_restapi so downstream users only have to link + # against cpp_restapi to get access to the optional component. + target_link_libraries(cpp_restapi INTERFACE cpp_restapi_json_pagination) endif() if(CppRestAPI_GitHub) @@ -137,6 +141,10 @@ if(CppRestAPI_GitHub) VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) + + # Re-export via cpp_restapi so downstream users only have to link + # against cpp_restapi to get the GitHub helpers. + target_link_libraries(cpp_restapi INTERFACE cpp_restapi_github) endif() if(CppRestAPI_Tests) diff --git a/README.md b/README.md index a73d300..1dde7bd 100644 --- a/README.md +++ b/README.md @@ -46,19 +46,10 @@ 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. -Applications that use JSON-aware pagination must link against it explicitly in -addition to `cpp_restapi`: - -```cmake -target_link_libraries(app - PRIVATE - cpp_restapi - cpp_restapi_json_pagination -) -``` - -Applications that do not need it can set `-DCppRestAPI_JsonPagination=OFF` to -drop the `jsoncpp` dependency entirely. +When enabled it is automatically exposed through the main `cpp_restapi` +interface, so applications do not need any extra `target_link_libraries` entry +to use it. To drop the `jsoncpp` dependency entirely, configure with +`-DCppRestAPI_JsonPagination=OFF`. ##### GitHub helpers: The optional `cpp_restapi_github` library provides @@ -68,15 +59,9 @@ convenience wrappers for the GitHub REST API. It depends on default (controlled by the `CppRestAPI_GitHub` CMake option; enabling it automatically enables `CppRestAPI_JsonPagination`). -Applications using the GitHub helpers must link against it explicitly: - -```cmake -target_link_libraries(app - PRIVATE - cpp_restapi - cpp_restapi_github -) -``` +Like the JSON-pagination library, it is automatically exposed through +`cpp_restapi`; no extra link entry is required in consumer projects. 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. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1efba31..3ca973c 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -28,20 +28,17 @@ add_executable(coroutine_qt_example coroutine_qt_example.cpp) target_link_libraries(github_qt_example PRIVATE cpp_restapi - cpp_restapi_github Qt::Network ) target_link_libraries(github_curl_example PRIVATE cpp_restapi - cpp_restapi_github ) target_link_libraries(github_cpp-httplib_example PRIVATE cpp_restapi - cpp_restapi_github ) target_link_libraries(bare_curl_connection_example diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c997f44..131296f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,8 +31,6 @@ target_link_libraries(api_tests ${HTTPLIB_LIBRARIES} cpp_restapi - cpp_restapi_json_pagination - cpp_restapi_github ) add_test(NAME api_tests COMMAND api_tests) From 6c798753c5b1a56fcaea4997c9cbe0b1fc2f358d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Apr 2026 23:13:16 +0200 Subject: [PATCH 04/13] Make HeaderUtils header-only The previous commit moved src/header_utils.{hpp,cpp} into cpp_restapi_json_pagination purely to avoid a link-time dependency cycle between cpp_restapi and cpp_restapi_json_pagination. That placement was awkward: HeaderUtils is a generic Link-header parsing helper, not something specific to JSON pagination. Collapse the implementation into the header (single regex-based function). No .cpp file means no archive symbol to resolve, so nothing links back into core and the cycle stays broken regardless of where the header lives. The header returns to being an internal helper in src/ that any future pagination strategy can include. --- CMakeLists.txt | 1 - src/header_utils.cpp | 40 ---------------------------------------- src/header_utils.hpp | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 42 deletions(-) delete mode 100644 src/header_utils.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cc7d169..44d2574 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,7 +89,6 @@ if(CppRestAPI_JsonPagination) endif() add_library(cpp_restapi_json_pagination - src/header_utils.cpp src/link_header_pagination_strategy.cpp ) diff --git a/src/header_utils.cpp b/src/header_utils.cpp deleted file mode 100644 index bed04d5..0000000 --- a/src/header_utils.cpp +++ /dev/null @@ -1,40 +0,0 @@ - -#include -#include - -#include "header_utils.hpp" - - -namespace HeaderUtils -{ - - std::string getNextPageUrl(const std::string& header) - { - std::istringstream header_(header); - std::string line; - std::string link; - - const std::regex nextPageRegex(R"(^[Ll]ink\:.*<([^>]+)>; rel="next".*)"); - - // iterating through each line of string - while (std::getline(header_, line)) - { - line.erase( - std::remove_if(line.begin(), line.end(), iscntrl), - line.end() - ); - - std::smatch nextPageMatch; - const bool match = std::regex_match(line, nextPageMatch, nextPageRegex); - - if (match) - { - link = nextPageMatch[1]; - break; - } - } - - return link; - } - -} diff --git a/src/header_utils.hpp b/src/header_utils.hpp index cc8dbb5..935b263 100644 --- a/src/header_utils.hpp +++ b/src/header_utils.hpp @@ -11,6 +11,10 @@ #ifndef HEADER_UTILS_HPP_INCLUDED #define HEADER_UTILS_HPP_INCLUDED +#include +#include +#include +#include #include @@ -24,7 +28,34 @@ namespace HeaderUtils * the api call * @return next page url */ - std::string getNextPageUrl(const std::string& header); + inline std::string getNextPageUrl(const std::string& header) + { + std::istringstream header_(header); + std::string line; + std::string link; + + const std::regex nextPageRegex(R"(^[Ll]ink\:.*<([^>]+)>; rel="next".*)"); + + // iterating through each line of string + while (std::getline(header_, line)) + { + line.erase( + std::remove_if(line.begin(), line.end(), iscntrl), + line.end() + ); + + std::smatch nextPageMatch; + const bool match = std::regex_match(line, nextPageMatch, nextPageRegex); + + if (match) + { + link = nextPageMatch[1]; + break; + } + } + + return link; + } } #endif From 81f068274ac8336458af33203b0cef2f28af4392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Fri, 24 Apr 2026 23:18:22 +0200 Subject: [PATCH 05/13] README: drop internal library-split details from feature docs The JSON pagination and GitHub helper sections described the cpp_restapi_json_pagination / cpp_restapi_github CMake targets and how they are re-exposed through the umbrella cpp_restapi target. That is an implementation detail of the build; from a consumer perspective there is only one library to link against. Keep just the CMake options users need to toggle the features. --- README.md | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1dde7bd..7b8edb1 100644 --- a/README.md +++ b/README.md @@ -40,28 +40,18 @@ target_link_libraries(app and that's all. ##### JSON-aware pagination: -The optional `cpp_restapi_json_pagination` library provides -`cpp_restapi::LinkHeaderPaginationStrategy` — an RFC 5988 `Link`-header based +`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. - -When enabled it is automatically exposed through the main `cpp_restapi` -interface, so applications do not need any extra `target_link_libraries` entry -to use it. To drop the `jsoncpp` dependency entirely, configure with -`-DCppRestAPI_JsonPagination=OFF`. +CMake option) and adds a dependency on the `jsoncpp` library. To drop the +`jsoncpp` dependency entirely, configure with `-DCppRestAPI_JsonPagination=OFF`. ##### GitHub helpers: -The optional `cpp_restapi_github` library provides -`cpp_restapi::GitHub::ConnectionBuilder` and `cpp_restapi::GitHub::Request` — -convenience wrappers for the GitHub REST API. It depends on -`cpp_restapi_json_pagination` (and transitively on `jsoncpp`). It is built by -default (controlled by the `CppRestAPI_GitHub` CMake option; enabling it -automatically enables `CppRestAPI_JsonPagination`). - -Like the JSON-pagination library, it is automatically exposed through -`cpp_restapi`; no extra link entry is required in consumer projects. Disable -with `-DCppRestAPI_GitHub=OFF` if not needed. +`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. From 3ed29ec49bf190a6581caa95077cf2437964cd05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Apr 2026 15:09:18 +0200 Subject: [PATCH 06/13] Move optional components into add_subdirectory layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The top-level CMakeLists.txt carried inline definitions for the cpp_restapi_json_pagination and cpp_restapi_github targets — duplicating the structural pattern already used by every backend (src/_backend/CMakeLists.txt + add_subdirectory). Move each optional component to its own subdirectory: src/json_pagination/ (link_header_pagination_strategy.cpp + CMakeLists) src/services/github/ (now also owns its own CMakeLists.txt) The root CMakeLists.txt now just guards each component with its option and calls add_subdirectory, mirroring the backend handling. generate_export_header is given an explicit EXPORT_FILE_NAME pointing at ${PROJECT_BINARY_DIR} so the generated *_export.h headers stay where public headers in include/cpp_restapi expect to find them (the default output directory is CMAKE_CURRENT_BINARY_DIR, which would otherwise move into the per-subdir build folder). --- CMakeLists.txt | 69 +------------------ src/json_pagination/CMakeLists.txt | 49 +++++++++++++ .../link_header_pagination_strategy.cpp | 0 src/services/github/CMakeLists.txt | 27 ++++++++ 4 files changed, 78 insertions(+), 67 deletions(-) create mode 100644 src/json_pagination/CMakeLists.txt rename src/{ => json_pagination}/link_header_pagination_strategy.cpp (100%) create mode 100644 src/services/github/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 44d2574..3c43b81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,76 +74,11 @@ if(CppRestAPI_CppHttplibBackend) endif() if(CppRestAPI_JsonPagination) - # Optional component pulling in the jsoncpp dependency. Contains the - # Link-header based pagination strategy. Users that do not need - # JSON-aware multi-page merging do not have to link against it - # (and do not pay the jsoncpp dependency cost). - find_package(jsoncpp CONFIG) - - if(jsoncpp_FOUND) - set(JSONCPP_LIBRARIES JsonCpp::JsonCpp) - else() - find_package(PkgConfig REQUIRED) - - pkg_check_modules(JSONCPP REQUIRED jsoncpp) - endif() - - add_library(cpp_restapi_json_pagination - src/link_header_pagination_strategy.cpp - ) - - target_include_directories(cpp_restapi_json_pagination - PRIVATE SYSTEM - ${JSONCPP_INCLUDE_DIRS} - ) - - target_link_libraries(cpp_restapi_json_pagination - PUBLIC - cpp_restapi - PRIVATE - ${JSONCPP_LIBRARIES} - ) - - generate_export_header(cpp_restapi_json_pagination - BASE_NAME cpp_restapi_json_pagination - ) - - set_target_properties(cpp_restapi_json_pagination PROPERTIES - VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} - ) - - # Re-export via cpp_restapi so downstream users only have to link - # against cpp_restapi to get access to the optional component. - target_link_libraries(cpp_restapi INTERFACE cpp_restapi_json_pagination) + add_subdirectory(src/json_pagination) endif() if(CppRestAPI_GitHub) - # Optional component with GitHub API helpers (ConnectionBuilder, Request). - # Relies on LinkHeaderPaginationStrategy from cpp_restapi_json_pagination - # to merge paginated responses. - add_library(cpp_restapi_github - src/services/github/request.cpp - ) - - target_link_libraries(cpp_restapi_github - PUBLIC - cpp_restapi - cpp_restapi_json_pagination - ) - - generate_export_header(cpp_restapi_github - BASE_NAME cpp_restapi_github - ) - - set_target_properties(cpp_restapi_github PROPERTIES - VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} - ) - - # Re-export via cpp_restapi so downstream users only have to link - # against cpp_restapi to get the GitHub helpers. - target_link_libraries(cpp_restapi INTERFACE cpp_restapi_github) + add_subdirectory(src/services/github) endif() if(CppRestAPI_Tests) diff --git a/src/json_pagination/CMakeLists.txt b/src/json_pagination/CMakeLists.txt new file mode 100644 index 0000000..dd96051 --- /dev/null +++ b/src/json_pagination/CMakeLists.txt @@ -0,0 +1,49 @@ + +# Optional component pulling in the jsoncpp dependency. Contains the +# Link-header based pagination strategy. Users that do not need +# JSON-aware multi-page merging do not have to link against it +# (and do not pay the jsoncpp dependency cost). +find_package(jsoncpp CONFIG) + +if(jsoncpp_FOUND) + set(JSONCPP_LIBRARIES JsonCpp::JsonCpp) +else() + find_package(PkgConfig REQUIRED) + + pkg_check_modules(JSONCPP REQUIRED jsoncpp) +endif() + +add_library(cpp_restapi_json_pagination + link_header_pagination_strategy.cpp +) + +target_include_directories(cpp_restapi_json_pagination + PRIVATE SYSTEM + ${JSONCPP_INCLUDE_DIRS} +) + +target_include_directories(cpp_restapi_json_pagination + PRIVATE + ${PROJECT_SOURCE_DIR}/src +) + +target_link_libraries(cpp_restapi_json_pagination + PUBLIC + cpp_restapi + PRIVATE + ${JSONCPP_LIBRARIES} +) + +generate_export_header(cpp_restapi_json_pagination + BASE_NAME cpp_restapi_json_pagination + EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/cpp_restapi_json_pagination_export.h +) + +set_target_properties(cpp_restapi_json_pagination PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} +) + +# Re-export via cpp_restapi so downstream users only have to link +# against cpp_restapi to get access to the optional component. +target_link_libraries(cpp_restapi INTERFACE cpp_restapi_json_pagination) diff --git a/src/link_header_pagination_strategy.cpp b/src/json_pagination/link_header_pagination_strategy.cpp similarity index 100% rename from src/link_header_pagination_strategy.cpp rename to src/json_pagination/link_header_pagination_strategy.cpp diff --git a/src/services/github/CMakeLists.txt b/src/services/github/CMakeLists.txt new file mode 100644 index 0000000..713c8ef --- /dev/null +++ b/src/services/github/CMakeLists.txt @@ -0,0 +1,27 @@ + +# Optional component with GitHub API helpers (ConnectionBuilder, Request). +# Relies on LinkHeaderPaginationStrategy from cpp_restapi_json_pagination +# to merge paginated responses. +add_library(cpp_restapi_github + request.cpp +) + +target_link_libraries(cpp_restapi_github + PUBLIC + cpp_restapi + cpp_restapi_json_pagination +) + +generate_export_header(cpp_restapi_github + BASE_NAME cpp_restapi_github + EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/cpp_restapi_github_export.h +) + +set_target_properties(cpp_restapi_github PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} +) + +# Re-export via cpp_restapi so downstream users only have to link +# against cpp_restapi to get the GitHub helpers. +target_link_libraries(cpp_restapi INTERFACE cpp_restapi_github) From b5954b7ee7706dc8378c1004341e3e1af114d2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Apr 2026 20:13:30 +0200 Subject: [PATCH 07/13] Restructure cpp_restapi as umbrella over OBJECT libraries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous layout had cpp_restapi own the core sources/backends and the optional cpp_restapi_json_pagination / cpp_restapi_github targets PUBLIC-link back to cpp_restapi while cpp_restapi INTERFACE-linked them in turn. That worked but described what looked like a dependency cycle and exposed three separate library targets to consumers. Reorganize the dependency graph as a strict tree and reduce the public surface to a single artifact: cpp_restapi_core (OBJECT) -- backends contribute here cpp_restapi_json_pagination (OBJECT) -- depends on _core cpp_restapi_github (OBJECT) -- depends on _core, _json_pagination cpp_restapi (umbrella) -- links all of the above cpp_restapi is the only target consumers see and the only artifact produced (.so / .a). Its type is left to BUILD_SHARED_LIBS — the user decides; we no longer pin it. All internal pieces are OBJECT libraries with POSITION_INDEPENDENT_CODE. That guarantees there is exactly one place where their object code lands (inside cpp_restapi); they cannot be linked twice into a downstream build, and there is no chance of multiple consumer shared libraries each embedding a private copy of a static helper. Internal layout: - cpp_restapi_core gets the three core sources and is the sink for each backend's target_sources / target_link_libraries, replacing cpp_restapi as the backend target name. - cpp_restapi_export.h is generated against cpp_restapi_core (with BASE_NAME cpp_restapi to keep CPP_RESTAPI_EXPORT) so existing public headers do not change. - The optional components no longer back-link into cpp_restapi; the umbrella PUBLIC-links them, removing the apparent cycle. - The umbrella library has a tiny auto-generated stub TU as its only direct source; everything else arrives via the linked OBJECT libs. Verified with both static (default) and BUILD_SHARED_LIBS=ON builds: ctest passes, only libcpp_restapi.{so,a} is produced, ldd on the shared library shows just the expected backend/jsoncpp deps. --- CMakeLists.txt | 49 +++++++++++++++++++------- src/cpp-httplib_backend/CMakeLists.txt | 6 ++-- src/curl_backend/CMakeLists.txt | 6 ++-- src/json_pagination/CMakeLists.txt | 21 ++++------- src/qt_backend/CMakeLists.txt | 6 ++-- src/services/github/CMakeLists.txt | 20 ++++------- 6 files changed, 57 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c43b81..4a531d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,19 +32,30 @@ if(CppRestAPI_GitHub AND NOT CppRestAPI_JsonPagination) set(CppRestAPI_JsonPagination ON) endif() -add_library(cpp_restapi +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() + +add_library(cpp_restapi_core OBJECT src/base_connection.cpp src/threaded_connection.cpp src/sse_parser.cpp ) -target_include_directories(cpp_restapi +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 ) -generate_export_header(cpp_restapi) +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 +) configure_file( ${PROJECT_SOURCE_DIR}/include/cpp_restapi/version.hpp.in @@ -52,15 +63,6 @@ configure_file( @ONLY ) -set_target_properties(cpp_restapi PROPERTIES - VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} -) - -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() @@ -81,6 +83,28 @@ if(CppRestAPI_GitHub) add_subdirectory(src/services/github) endif() +# Umbrella library: the only artifact consumers link against. Composed +# entirely of OBJECT libraries; type follows BUILD_SHARED_LIBS. +set(_cpp_restapi_stub "${PROJECT_BINARY_DIR}/cpp_restapi_umbrella.cpp") +file(WRITE ${_cpp_restapi_stub} "// Auto-generated stub for cpp_restapi umbrella library.\n") + +add_library(cpp_restapi ${_cpp_restapi_stub}) + +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} +) + if(CppRestAPI_Tests) enable_testing() add_subdirectory(tests) @@ -89,4 +113,3 @@ endif() if(CppRestAPI_Examples) add_subdirectory(examples) endif() - diff --git a/src/cpp-httplib_backend/CMakeLists.txt b/src/cpp-httplib_backend/CMakeLists.txt index afeb33f..ebb2ab7 100644 --- a/src/cpp-httplib_backend/CMakeLists.txt +++ b/src/cpp-httplib_backend/CMakeLists.txt @@ -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 ) diff --git a/src/curl_backend/CMakeLists.txt b/src/curl_backend/CMakeLists.txt index a4d40e9..63896df 100644 --- a/src/curl_backend/CMakeLists.txt +++ b/src/curl_backend/CMakeLists.txt @@ -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 ) diff --git a/src/json_pagination/CMakeLists.txt b/src/json_pagination/CMakeLists.txt index dd96051..ed11add 100644 --- a/src/json_pagination/CMakeLists.txt +++ b/src/json_pagination/CMakeLists.txt @@ -1,8 +1,4 @@ -# Optional component pulling in the jsoncpp dependency. Contains the -# Link-header based pagination strategy. Users that do not need -# JSON-aware multi-page merging do not have to link against it -# (and do not pay the jsoncpp dependency cost). find_package(jsoncpp CONFIG) if(jsoncpp_FOUND) @@ -13,10 +9,14 @@ else() pkg_check_modules(JSONCPP REQUIRED jsoncpp) endif() -add_library(cpp_restapi_json_pagination +add_library(cpp_restapi_json_pagination OBJECT link_header_pagination_strategy.cpp ) +set_target_properties(cpp_restapi_json_pagination PROPERTIES POSITION_INDEPENDENT_CODE ON) + +target_compile_definitions(cpp_restapi_json_pagination PRIVATE cpp_restapi_json_pagination_EXPORTS) + target_include_directories(cpp_restapi_json_pagination PRIVATE SYSTEM ${JSONCPP_INCLUDE_DIRS} @@ -29,7 +29,7 @@ target_include_directories(cpp_restapi_json_pagination target_link_libraries(cpp_restapi_json_pagination PUBLIC - cpp_restapi + cpp_restapi_core PRIVATE ${JSONCPP_LIBRARIES} ) @@ -38,12 +38,3 @@ generate_export_header(cpp_restapi_json_pagination BASE_NAME cpp_restapi_json_pagination EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/cpp_restapi_json_pagination_export.h ) - -set_target_properties(cpp_restapi_json_pagination PROPERTIES - VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} -) - -# Re-export via cpp_restapi so downstream users only have to link -# against cpp_restapi to get access to the optional component. -target_link_libraries(cpp_restapi INTERFACE cpp_restapi_json_pagination) diff --git a/src/qt_backend/CMakeLists.txt b/src/qt_backend/CMakeLists.txt index 64fd493..cda2b34 100644 --- a/src/qt_backend/CMakeLists.txt +++ b/src/qt_backend/CMakeLists.txt @@ -9,19 +9,19 @@ else() endif() endif() -target_sources(cpp_restapi PRIVATE +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 Qt::Core Qt::Network ) -target_include_directories(cpp_restapi +target_include_directories(cpp_restapi_core PRIVATE ${PROJECT_SOURCE_DIR}/src ) diff --git a/src/services/github/CMakeLists.txt b/src/services/github/CMakeLists.txt index 713c8ef..182a5da 100644 --- a/src/services/github/CMakeLists.txt +++ b/src/services/github/CMakeLists.txt @@ -1,14 +1,15 @@ -# Optional component with GitHub API helpers (ConnectionBuilder, Request). -# Relies on LinkHeaderPaginationStrategy from cpp_restapi_json_pagination -# to merge paginated responses. -add_library(cpp_restapi_github +add_library(cpp_restapi_github OBJECT request.cpp ) +set_target_properties(cpp_restapi_github PROPERTIES POSITION_INDEPENDENT_CODE ON) + +target_compile_definitions(cpp_restapi_github PRIVATE cpp_restapi_github_EXPORTS) + target_link_libraries(cpp_restapi_github PUBLIC - cpp_restapi + cpp_restapi_core cpp_restapi_json_pagination ) @@ -16,12 +17,3 @@ generate_export_header(cpp_restapi_github BASE_NAME cpp_restapi_github EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/cpp_restapi_github_export.h ) - -set_target_properties(cpp_restapi_github PROPERTIES - VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} -) - -# Re-export via cpp_restapi so downstream users only have to link -# against cpp_restapi to get the GitHub helpers. -target_link_libraries(cpp_restapi INTERFACE cpp_restapi_github) From a655b59c5dedef1f09a372e40e3f2765e61b9c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Apr 2026 20:16:45 +0200 Subject: [PATCH 08/13] Move library targets into src/CMakeLists.txt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The top-level CMakeLists.txt was carrying the cpp_restapi_core target definition, the per-component add_subdirectory calls, and the umbrella cpp_restapi target — none of which belong at project scope. Move them into src/CMakeLists.txt so the root file only handles project-wide concerns: project()/version, options, the no-backend guard, and the add_subdirectory dispatch to src/, tests/ and examples/. --- CMakeLists.txt | 69 +--------------------------------------------- src/CMakeLists.txt | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 src/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a531d3..5a63701 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,74 +36,7 @@ if(NOT CppRestAPI_QtBackend AND NOT CppRestAPI_CurlBackend AND NOT CppRestAPI_Cp message(FATAL_ERROR "No backend was chosen. Set CppRestAPI_QtBackend, CppRestAPI_CurlBackend or CppRestAPI_CppHttplibBackend variable to ON") endif() -add_library(cpp_restapi_core OBJECT - src/base_connection.cpp - src/threaded_connection.cpp - src/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 -) - -configure_file( - ${PROJECT_SOURCE_DIR}/include/cpp_restapi/version.hpp.in - ${PROJECT_BINARY_DIR}/cpp_restapi/version.hpp - @ONLY -) - -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() - -if(CppRestAPI_JsonPagination) - add_subdirectory(src/json_pagination) -endif() - -if(CppRestAPI_GitHub) - add_subdirectory(src/services/github) -endif() - -# Umbrella library: the only artifact consumers link against. Composed -# entirely of OBJECT libraries; type follows BUILD_SHARED_LIBS. -set(_cpp_restapi_stub "${PROJECT_BINARY_DIR}/cpp_restapi_umbrella.cpp") -file(WRITE ${_cpp_restapi_stub} "// Auto-generated stub for cpp_restapi umbrella library.\n") - -add_library(cpp_restapi ${_cpp_restapi_stub}) - -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} -) +add_subdirectory(src) if(CppRestAPI_Tests) enable_testing() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..b67431c --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,69 @@ + +add_library(cpp_restapi_core OBJECT + base_connection.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 +) + +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; type follows BUILD_SHARED_LIBS. +set(_cpp_restapi_stub "${PROJECT_BINARY_DIR}/cpp_restapi_umbrella.cpp") +file(WRITE ${_cpp_restapi_stub} "// Auto-generated stub for cpp_restapi umbrella library.\n") + +add_library(cpp_restapi ${_cpp_restapi_stub}) + +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} +) From eac3bd90f8a9dc5bbca1880f64102e7c5e358d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sat, 25 Apr 2026 20:28:04 +0200 Subject: [PATCH 09/13] Restore HeaderUtils as a regular core source It was forced to be header-only earlier to avoid an archive-symbol link cycle between cpp_restapi and cpp_restapi_json_pagination. With the new umbrella-OBJECT-libraries layout the dependency graph is acyclic (json_pagination depends on core, never the other way around), so the .cpp can come back: a single definition of getNextPageUrl lives in cpp_restapi_core and is reused by anyone that includes header_utils.hpp, instead of being inlined into every translation unit. --- src/CMakeLists.txt | 1 + src/header_utils.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/header_utils.hpp | 33 +-------------------------------- 3 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 src/header_utils.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b67431c..a06146a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(cpp_restapi_core OBJECT base_connection.cpp + header_utils.cpp threaded_connection.cpp sse_parser.cpp ) diff --git a/src/header_utils.cpp b/src/header_utils.cpp new file mode 100644 index 0000000..f7a9f86 --- /dev/null +++ b/src/header_utils.cpp @@ -0,0 +1,40 @@ + +#include "header_utils.hpp" + +#include +#include +#include +#include + + +namespace HeaderUtils +{ + std::string getNextPageUrl(const std::string& header) + { + std::istringstream header_(header); + std::string line; + std::string link; + + const std::regex nextPageRegex(R"(^[Ll]ink\:.*<([^>]+)>; rel="next".*)"); + + // iterating through each line of string + while (std::getline(header_, line)) + { + line.erase( + std::remove_if(line.begin(), line.end(), iscntrl), + line.end() + ); + + std::smatch nextPageMatch; + const bool match = std::regex_match(line, nextPageMatch, nextPageRegex); + + if (match) + { + link = nextPageMatch[1]; + break; + } + } + + return link; + } +} diff --git a/src/header_utils.hpp b/src/header_utils.hpp index 935b263..cc8dbb5 100644 --- a/src/header_utils.hpp +++ b/src/header_utils.hpp @@ -11,10 +11,6 @@ #ifndef HEADER_UTILS_HPP_INCLUDED #define HEADER_UTILS_HPP_INCLUDED -#include -#include -#include -#include #include @@ -28,34 +24,7 @@ namespace HeaderUtils * the api call * @return next page url */ - inline std::string getNextPageUrl(const std::string& header) - { - std::istringstream header_(header); - std::string line; - std::string link; - - const std::regex nextPageRegex(R"(^[Ll]ink\:.*<([^>]+)>; rel="next".*)"); - - // iterating through each line of string - while (std::getline(header_, line)) - { - line.erase( - std::remove_if(line.begin(), line.end(), iscntrl), - line.end() - ); - - std::smatch nextPageMatch; - const bool match = std::regex_match(line, nextPageMatch, nextPageRegex); - - if (match) - { - link = nextPageMatch[1]; - break; - } - } - - return link; - } + std::string getNextPageUrl(const std::string& header); } #endif From 7a66ac67689696577de2b7616c1c1c71f766d81e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sun, 26 Apr 2026 08:54:14 +0200 Subject: [PATCH 10/13] Use checked-in umbrella stub source The umbrella library's stub source was generated via file(WRITE) on every CMake reconfigure, which updated its mtime and forced a relink of libcpp_restapi each time. Replace it with a tiny checked-in source file. Same end result, no spurious rebuilds. --- src/CMakeLists.txt | 5 +---- src/cpp_restapi_umbrella.cpp | 5 +++++ 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 src/cpp_restapi_umbrella.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a06146a..afda467 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -49,10 +49,7 @@ endif() # Umbrella library: the only artifact consumers link against. Composed # entirely of OBJECT libraries; type follows BUILD_SHARED_LIBS. -set(_cpp_restapi_stub "${PROJECT_BINARY_DIR}/cpp_restapi_umbrella.cpp") -file(WRITE ${_cpp_restapi_stub} "// Auto-generated stub for cpp_restapi umbrella library.\n") - -add_library(cpp_restapi ${_cpp_restapi_stub}) +add_library(cpp_restapi cpp_restapi_umbrella.cpp) target_link_libraries(cpp_restapi PUBLIC cpp_restapi_core) diff --git a/src/cpp_restapi_umbrella.cpp b/src/cpp_restapi_umbrella.cpp new file mode 100644 index 0000000..bd2364c --- /dev/null +++ b/src/cpp_restapi_umbrella.cpp @@ -0,0 +1,5 @@ +// Stub source for the cpp_restapi umbrella library. +// The umbrella aggregates OBJECT libraries (cpp_restapi_core and the +// optional cpp_restapi_json_pagination / cpp_restapi_github targets); +// CMake requires at least one source file, so this translation unit +// exists solely to satisfy that requirement. From 781e390868acbbf13d9b402d241914010b6ef443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sun, 26 Apr 2026 08:54:30 +0200 Subject: [PATCH 11/13] Warn when CppRestAPI_GitHub overrides CppRestAPI_JsonPagination Previously the implication CppRestAPI_GitHub => CppRestAPI_JsonPagination was applied silently with a STATUS message, which meant explicitly passing -DCppRestAPI_JsonPagination=OFF on the command line was ignored without any visible feedback. Promote the message to WARNING so the inconsistent configuration is clearly surfaced. --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a63701..a7580f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,9 @@ if(CppRestAPI_Tests OR CppRestAPI_Examples) endif() if(CppRestAPI_GitHub AND NOT CppRestAPI_JsonPagination) - message(STATUS "CppRestAPI_GitHub implies CppRestAPI_JsonPagination; enabling it.") + message(WARNING + "CppRestAPI_GitHub=ON requires CppRestAPI_JsonPagination=ON; " + "overriding the requested CppRestAPI_JsonPagination=OFF.") set(CppRestAPI_JsonPagination ON) endif() From 864391e2e25aff0c88c70fe7750abb03e2e84f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sun, 26 Apr 2026 08:55:33 +0200 Subject: [PATCH 12/13] Diagnose use of optional headers when feature is disabled When CppRestAPI_GitHub or CppRestAPI_JsonPagination is OFF, the matching public headers (cpp_restapi/github/request.hpp, cpp_restapi/link_header_pagination_strategy.hpp) used to break the build with a low-level missing-header error referring to the generated *_export.h that was never produced. Add CPP_RESTAPI_HAS_GITHUB and CPP_RESTAPI_HAS_JSON_PAGINATION feature macros to the configured version.hpp, and #error out from those headers when the corresponding feature is not present, so the diagnostic clearly tells the user how to fix the configuration. --- include/cpp_restapi/github/request.hpp | 6 ++++++ include/cpp_restapi/link_header_pagination_strategy.hpp | 6 ++++++ include/cpp_restapi/version.hpp.in | 7 +++++++ src/CMakeLists.txt | 4 ++++ 4 files changed, 23 insertions(+) diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp index 17c82f1..85b26a5 100644 --- a/include/cpp_restapi/github/request.hpp +++ b/include/cpp_restapi/github/request.hpp @@ -2,6 +2,12 @@ #ifndef REQUEST_HPP #define REQUEST_HPP +#include + +#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 #include diff --git a/include/cpp_restapi/link_header_pagination_strategy.hpp b/include/cpp_restapi/link_header_pagination_strategy.hpp index 7de68ee..932db43 100644 --- a/include/cpp_restapi/link_header_pagination_strategy.hpp +++ b/include/cpp_restapi/link_header_pagination_strategy.hpp @@ -1,6 +1,12 @@ #ifndef LINK_HEADER_PAGINATION_STRATEGY_HPP_INCLUDED #define LINK_HEADER_PAGINATION_STRATEGY_HPP_INCLUDED +#include + +#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 #include "cpp_restapi_json_pagination_export.h" diff --git a/include/cpp_restapi/version.hpp.in b/include/cpp_restapi/version.hpp.in index 468b0e7..e2cc853 100644 --- a/include/cpp_restapi/version.hpp.in +++ b/include/cpp_restapi/version.hpp.in @@ -6,6 +6,13 @@ #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@; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index afda467..4178dd8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,10 @@ generate_export_header(cpp_restapi_core 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 From 88cfe36da8afca5df4c7845669c86b3f62560f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sun, 26 Apr 2026 09:07:19 +0200 Subject: [PATCH 13/13] Replace umbrella stub with runtimeVersion() implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The umbrella library needs at least one source file, which previously existed solely as an empty stub. Replace it with src/version.cpp, defining cpp_restapi::runtimeVersion() — a small but useful runtime counterpart to the compile-time CPP_RESTAPI_VERSION constants. It returns the version string baked into the actually-linked library, allowing consumers to detect header/library mismatch after an in-place upgrade. The function is declared in the configured version.hpp and exported via CPP_RESTAPI_EXPORT; it lives in the umbrella TU so it's part of libcpp_restapi itself rather than any sub-component. --- include/cpp_restapi/version.hpp.in | 12 ++++++++++++ src/CMakeLists.txt | 16 ++++++++++++++-- src/cpp_restapi_umbrella.cpp | 5 ----- src/version.cpp | 10 ++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) delete mode 100644 src/cpp_restapi_umbrella.cpp create mode 100644 src/version.cpp diff --git a/include/cpp_restapi/version.hpp.in b/include/cpp_restapi/version.hpp.in index e2cc853..109ae67 100644 --- a/include/cpp_restapi/version.hpp.in +++ b/include/cpp_restapi/version.hpp.in @@ -1,6 +1,8 @@ #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@ @@ -19,6 +21,16 @@ namespace cpp_restapi 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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4178dd8..681c0db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,8 +52,20 @@ if(CppRestAPI_GitHub) endif() # Umbrella library: the only artifact consumers link against. Composed -# entirely of OBJECT libraries; type follows BUILD_SHARED_LIBS. -add_library(cpp_restapi cpp_restapi_umbrella.cpp) +# 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) diff --git a/src/cpp_restapi_umbrella.cpp b/src/cpp_restapi_umbrella.cpp deleted file mode 100644 index bd2364c..0000000 --- a/src/cpp_restapi_umbrella.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// Stub source for the cpp_restapi umbrella library. -// The umbrella aggregates OBJECT libraries (cpp_restapi_core and the -// optional cpp_restapi_json_pagination / cpp_restapi_github targets); -// CMake requires at least one source file, so this translation unit -// exists solely to satisfy that requirement. diff --git a/src/version.cpp b/src/version.cpp new file mode 100644 index 0000000..5b802bc --- /dev/null +++ b/src/version.cpp @@ -0,0 +1,10 @@ +#include + + +namespace cpp_restapi +{ + const char* runtimeVersion() noexcept + { + return CPP_RESTAPI_VERSION; + } +}