diff --git a/CMakeLists.txt b/CMakeLists.txt index 3568667..a7580f3 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,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 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) @@ -31,58 +23,22 @@ 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() @@ -92,4 +48,3 @@ endif() if(CppRestAPI_Examples) add_subdirectory(examples) endif() - diff --git a/README.md b/README.md index e217232..7b8edb1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/include/cpp_restapi/github/request.hpp b/include/cpp_restapi/github/request.hpp index b6801f6..85b26a5 100644 --- a/include/cpp_restapi/github/request.hpp +++ b/include/cpp_restapi/github/request.hpp @@ -2,11 +2,17 @@ #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 #include -#include "cpp_restapi_export.h" +#include "cpp_restapi_github_export.h" namespace cpp_restapi::GitHub @@ -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); diff --git a/include/cpp_restapi/link_header_pagination_strategy.hpp b/include/cpp_restapi/link_header_pagination_strategy.hpp index e8fde9a..932db43 100644 --- a/include/cpp_restapi/link_header_pagination_strategy.hpp +++ b/include/cpp_restapi/link_header_pagination_strategy.hpp @@ -1,8 +1,14 @@ #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_export.h" +#include "cpp_restapi_json_pagination_export.h" namespace cpp_restapi { @@ -13,8 +19,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/include/cpp_restapi/version.hpp.in b/include/cpp_restapi/version.hpp.in index 468b0e7..109ae67 100644 --- a/include/cpp_restapi/version.hpp.in +++ b/include/cpp_restapi/version.hpp.in @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..681c0db --- /dev/null +++ b/src/CMakeLists.txt @@ -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} +) 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/header_utils.cpp b/src/header_utils.cpp index bed04d5..f7a9f86 100644 --- a/src/header_utils.cpp +++ b/src/header_utils.cpp @@ -1,13 +1,14 @@ +#include "header_utils.hpp" + +#include +#include #include #include -#include "header_utils.hpp" - namespace HeaderUtils { - std::string getNextPageUrl(const std::string& header) { std::istringstream header_(header); @@ -36,5 +37,4 @@ namespace HeaderUtils return link; } - } diff --git a/src/json_pagination/CMakeLists.txt b/src/json_pagination/CMakeLists.txt new file mode 100644 index 0000000..ed11add --- /dev/null +++ b/src/json_pagination/CMakeLists.txt @@ -0,0 +1,40 @@ + +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 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} +) + +target_include_directories(cpp_restapi_json_pagination + PRIVATE + ${PROJECT_SOURCE_DIR}/src +) + +target_link_libraries(cpp_restapi_json_pagination + PUBLIC + cpp_restapi_core + 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 +) 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/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 new file mode 100644 index 0000000..182a5da --- /dev/null +++ b/src/services/github/CMakeLists.txt @@ -0,0 +1,19 @@ + +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_core + 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 +) 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; + } +}