Skip to content

Commit

Permalink
Merge pull request #1 from bringauto/BAF-734/http_api_client_wrapper
Browse files Browse the repository at this point in the history
Http api client wrapper
  • Loading branch information
MarioIvancik authored Mar 15, 2024
2 parents f769c18 + c8784f0 commit b063054
Show file tree
Hide file tree
Showing 77 changed files with 2,610 additions and 415 deletions.
33 changes: 4 additions & 29 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
.idea
.vscode
cmake-build-debug
_*/
43 changes: 0 additions & 43 deletions .openapi-generator/FILES

This file was deleted.

1 change: 0 additions & 1 deletion .openapi-generator/VERSION

This file was deleted.

3 changes: 3 additions & 0 deletions CMLibStorage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SET(STORAGE_LIST DEP)

SET(STORAGE_LIST_DEP "https://github.com/bringauto/package-tracker.git")
176 changes: 85 additions & 91 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,93 +1,87 @@
#
# Fleet v2 HTTP API
# HTTP-based API for Fleet Protocol v2 serving for communication between the External Server and the end users.
#
# The version of the OpenAPI document: 1.0.0
#
# https://openapi-generator.tech
#
# NOTE: Auto generated by OpenAPI Generator (https://openapi-generator.tech).

cmake_minimum_required (VERSION 3.1)

project(CppRestOpenAPIClient)

# Force -fPIC even if the project is configured for building a static library.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CXX_STANDARD_REQUIRED ON)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

find_package(cpprestsdk REQUIRED)
find_package(Boost REQUIRED)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

file(GLOB_RECURSE HEADER_FILES "include/*.h")
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp")

add_library(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES})

target_compile_options(${PROJECT_NAME}
PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wno-unused-variable>
)

target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

if (UNIX)
message(STATUS "Building client library for Linux/Unix")
if (BUILD_SHARED_LIBS)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::headers cpprestsdk::cpprest)
else()
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::headers cpprestsdk::cpprest crypto)
endif()
else()
message(STATUS "Building client library for Windows")
if (BUILD_SHARED_LIBS)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::headers cpprestsdk::cpprest)
else()
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::headers cpprestsdk::cpprest bcrypt)
endif()
endif()

configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)

install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
CMAKE_MINIMUM_REQUIRED(VERSION 3.25 FATAL_ERROR)
PROJECT(fleet-http-client C CXX)
INCLUDE(GNUInstallDirs)

SET(FLEET_HTTP_CLIENT_VERSION 1.0.0)
SET(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
SET(CMAKE_CXX_STANDARD 20)

INCLUDE(CheckPIESupported)
CHECK_PIE_SUPPORTED()
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)

OPTION(BRINGAUTO_TESTS "Enable tests" OFF)
OPTION(BRINGAUTO_PACKAGE "Package creation" OFF)
OPTION(BRINGAUTO_INSTALL "Enable install" OFF)
OPTION(BRINGAUTO_SYSTEM_DEP "Enable system dependencies" OFF)
OPTION(BRINGAUTO_SAMPLES "Enable build of sample app, not used in project" OFF)

IF(BRINGAUTO_PACKAGE)
IF(NOT BRINGAUTO_INSTALL)
SET(BRINGAUTO_INSTALL ON CACHE BOOL "Forced install due to BRINGAUTO_PACKAGE=ON" FORCE)
MESSAGE(WARNING "BRINGAUTO_INSTALL is switched to on because of BRINGAUTO_PACKAGE=ON")
ENDIF()
ENDIF()

FIND_PACKAGE(CMLIB
COMPONENTS CMDEF CMUTIL STORAGE
REQUIRED
)

IF(NOT BRINGAUTO_SYSTEM_DEP)
INCLUDE(cmake/Dependencies.cmake)
ENDIF()

FIND_PACKAGE(libbringauto_logger 1.2.0 REQUIRED)
FIND_PACKAGE(fleet-protocol-interface 2.0.0 REQUIRED)
FIND_PACKAGE(fleet-protocol-cxx-helpers-static 1.1.1 REQUIRED)

SET(BUILD_SHARED_LIBS OFF)
ADD_SUBDIRECTORY("lib/bringauto-fleet-http-client-generated")

IF(BRINGAUTO_SAMPLES)
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/example/)
ENDIF()

FILE(GLOB_RECURSE source_files "src/*")
ADD_LIBRARY(bringautolib STATIC ${source_files})
TARGET_INCLUDE_DIRECTORIES(bringautolib PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
TARGET_LINK_LIBRARIES(bringautolib PUBLIC
fleet-protocol-interface::common-headers-interface
fleet-protocol-cxx-helpers-static::fleet-protocol-cxx-helpers-static
bringauto-fleet-http-client-generated
bringauto_logger::bringauto_logger)

CMDEF_ADD_LIBRARY(
LIBRARY_GROUP fleet-http-client
TYPE STATIC
SOURCES ${source_files}
INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/>"
INSTALL_INCLUDE_DIRECTORIES "include/"
VERSION ${FLEET_HTTP_CLIENT_VERSION}
)

install(
EXPORT ${PROJECT_NAME}Targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
TARGET_LINK_LIBRARIES(fleet-http-client-static PUBLIC "$<BUILD_INTERFACE:bringautolib>")

IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
TARGET_COMPILE_OPTIONS(fleet-http-client-static PRIVATE -Wall -Wextra -Wpedantic -fsanitize=address)
TARGET_LINK_LIBRARIES(fleet-http-client-static PUBLIC -fsanitize=address)
ENDIF()

IF(BRINGAUTO_TESTS)
ENABLE_TESTING()
Add_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/test/)
ENDIF()

IF (BRINGAUTO_INSTALL)
IF (PROJECT_IS_TOP_LEVEL)
INCLUDE(GNUInstallDirs)
CMDEF_INSTALL(TARGET fleet-http-client-static)
ENDIF ()
ENDIF ()

IF (BRINGAUTO_PACKAGE)
IF (PROJECT_IS_TOP_LEVEL)
MESSAGE(STATUS "Fleet HTTP Client will not be released as a package as it is a static library.")
ENDIF ()
ENDIF ()
56 changes: 16 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
# Necessary changes
# Fleet HTTP client

- timestamp for Message model needs to be of int64_t type, otherwise it overflows
- Payload_data needs m_Json to store data
A simple wrapper for a c++ rest API client generated by the [OpenAPI Generator](https://openapi-generator.tech). Simplifies functions used to call endpoints of the API. Also limits the amount of requests in case of an error which may cause the API to not wait for new statuses/commands.

# C++ API client
### Request frequency limit

HTTP-based API for Fleet Protocol v2 serving for communication between the External Server and the end users.
Is only used on getCommands and getStatuses functions. Each time these functions get called, the current time gets added to a container which is then analyzed to find out whether there are too many requests. This needs to be configured by 4 parameters:

## Overview
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [OpenAPI spec](https://openapis.org) from a remote server, you can easily generate an API client.
- `maxRequestsThresholdCount` max amount of allowed requests in the specified time period, default: 3

- API version: 2.0.0
- Package version:
- Build package: org.openapitools.codegen.languages.CppRestSdkClientCodegen
- `maxRequestsThresholdPeriodMs` time period in which the amount of requests is checked, default: 1000

- API namespace: org.openapitools.client.api
- Model namespace: org.openapitools.client.model
- `delayAfterThresholdReachedMs` time to sleep for in ms when threshold is initially reached, default: 500

## Installation
- `retryRequestsDelayMs` delay in ms between requests until request rate is no longer over the threshold, default: 220 (this value should be lower than `maxRequestsThresholdPeriodMs` / `maxRequestsThresholdCount` otherwise the threshold will never be reached a second time; 220 was chosen as the above calculation lowered by an additional third of the delay to account for additional delays created by communication)

### Prerequisites
After the initial threshold delay ends, the time container is emptied and subsequent requests are delayed for a shorter time. If the threshold is no longer reached afterwards, the following requests work without any delay as originally.

Install [cpprestsdk](https://github.com/Microsoft/cpprestsdk).
![Flow chart](./doc/img/request_frequency_guard.png)

- Windows: `vcpkg install cpprestsdk cpprestsdk:x64-windows boost-uuid boost-uuid:x64-windows`
- Mac: `brew install cpprestsdk`
- Linux: `sudo apt-get install libcpprest-dev`
## Requirements

### Build
- cpprestsdk: `sudo apt-get install libcpprest-dev`

```sh
cmake -DCPPREST_ROOT=/usr -DCMAKE_CXX_FLAGS="-I/usr/local/opt/openssl/include" -DCMAKE_MODULE_LINKER_FLAGS="-L/usr/local/opt/openssl/lib"
make
```
## Generating client code

### Build on Windows with Visual Studio (VS2017)
To regenerate the Cpp-rest-openapi-client lib, use the `regen.sh` script. [Information on how to run the script](./openapi/README.md).

- Right click on folder containing source code
- Select 'Open in visual studio'
- Once visual studio opens, CMake should show up in top menu bar.
- Select CMake > Build All.

*Note: If the CMake menu item doesn't show up in Visual Studio, CMake
for Visual Studio must be installed. In this case, open the 'Visual Studio
Installer' application. Select 'modify' Visual Studio 2017. Make sure
'Desktop Development with C++' is installed, and specifically that 'Visual
C++ tools for CMake' is selected in the 'Installation Details' section.

Also be sure to review the CMakeLists.txt file. Edits are likely required.*

## Author

jiri.strouhal@bringauto.com
## Tests

[Tests Readme](./test/README.md)
11 changes: 11 additions & 0 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SET(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH OFF CACHE BOOL "Prevent using system paths for finding libraries")

BA_PACKAGE_LIBRARY(zlib v1.2.11)
BA_PACKAGE_LIBRARY(ba-logger v1.2.0)
BA_PACKAGE_LIBRARY(boost v1.78.0)
BA_PACKAGE_LIBRARY(fleet-protocol-interface v2.0.0 PLATFORM_STRING_MODE any_machine NO_DEBUG ON)
BA_PACKAGE_LIBRARY(fleet-protocol-cxx-helpers-static v1.1.1)

IF (BRINGAUTO_TESTS)
BA_PACKAGE_LIBRARY(gtest v1.12.1)
ENDIF ()
Binary file added doc/img/request_frequency_guard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ADD_EXECUTABLE(fleet_http_client_sample_app ${CMAKE_CURRENT_LIST_DIR}/main.cpp ${CMAKE_CURRENT_LIST_DIR}/memory_management.cpp)

TARGET_LINK_LIBRARIES(fleet_http_client_sample_app PUBLIC bringautolib)
Loading

0 comments on commit b063054

Please sign in to comment.