Skip to content

Commit 868adc5

Browse files
authored
Merge pull request #12 from bringauto/remove_ba_logger
Remove ba logger
2 parents 0665185 + 17ca4cc commit 868adc5

File tree

15 files changed

+105
-106
lines changed

15 files changed

+105
-106
lines changed

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ENDIF ()
88
SET(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
99
SET(CMAKE_INSTALL_RPATH "$ORIGIN")
1010
SET(CMAKE_CXX_STANDARD 20)
11-
SET(FLEET_HTTP_CLIENT_VERSION 1.3.0)
11+
SET(FLEET_HTTP_CLIENT_VERSION 1.5.0)
1212

1313
INCLUDE(CheckPIESupported)
1414
CHECK_PIE_SUPPORTED()
@@ -37,7 +37,6 @@ IF(NOT BRINGAUTO_SYSTEM_DEP)
3737
INCLUDE(cmake/Dependencies.cmake)
3838
ENDIF()
3939

40-
FIND_PACKAGE(libbringauto_logger 1.2.0 REQUIRED)
4140
FIND_PACKAGE(fleet-protocol-interface 2.0.0 REQUIRED)
4241
FIND_PACKAGE(fleet-protocol-cxx-helpers-static 1.1.1 REQUIRED)
4342

@@ -64,7 +63,6 @@ TARGET_LINK_LIBRARIES(fleet-http-client-shared
6463
fleet-protocol-interface::common-headers-interface
6564
fleet-protocol-cxx-helpers-static::fleet-protocol-cxx-helpers-static
6665
bringauto-fleet-http-client-generated-shared
67-
bringauto_logger::bringauto_logger
6866
)
6967

7068
IF(BRINGAUTO_TESTS)

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ This has to be done because cpprest is obsolete.
2626
### Requirements
2727

2828
- cpprestsdk
29-
- bringauto-logger
3029
- Boost
3130

3231
All requirements can be added by bringauto Packager

cmake/Dependencies.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
SET(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH OFF CACHE BOOL "Prevent using system paths for finding libraries")
22

33
BA_PACKAGE_LIBRARY(zlib v1.2.11)
4-
BA_PACKAGE_LIBRARY(ba-logger v1.2.0)
54
BA_PACKAGE_LIBRARY(boost v1.86.0)
65
BA_PACKAGE_LIBRARY(fleet-protocol-interface v2.0.0 PLATFORM_STRING_MODE any_machine NO_DEBUG ON)
76
BA_PACKAGE_LIBRARY(fleet-protocol-cxx-helpers-static v1.1.1)

example/main.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#include <bringauto/fleet_protocol/http_client/FleetApiClient.hpp>
2-
#include <bringauto/logging/ConsoleSink.hpp>
3-
#include <bringauto/logging/Logger.hpp>
42
#include <bringauto/fleet_protocol/cxx/DeviceID.hpp>
53

64

75
using namespace bringauto::fleet_protocol::http_client;
8-
using namespace bringauto::logging;
96

107
std::unique_ptr<FleetApiClient> fleetApiClient;
118

129
void createFleetApiClient() {
1310
FleetApiClient::FleetApiClientConfig facConfig {
1411
.apiUrl = "http://localhost:8080",
15-
.apiKey = "StaticAccessKeyToBeUsedByDevelopersOnEtna",
12+
.apiKey = "ProtocolStaticAccessKey",
1613
.companyName = "bringauto",
1714
.carName = "virtual_vehicle"
1815
};
@@ -33,7 +30,7 @@ void getCars() {
3330
auto cars = fleetApiClient->getCars();
3431

3532
std::cout << "Cars:" << std::endl;
36-
for(auto &car: cars) {
33+
for(const auto &car: cars.first) {
3734
std::cout << " " << car->getCompanyName() << " / " << car->getCarName() << std::endl;
3835
}
3936
} catch(const std::exception &e) {
@@ -45,7 +42,7 @@ void getCommands() {
4542
auto commands = fleetApiClient->getCommands();
4643

4744
std::cout << "Commands:" << std::endl;
48-
for(auto command: commands) {
45+
for(const auto& command: commands.first) {
4946
std::cout << " Timestamp: " << command->getTimestamp() << std::endl;
5047
std::cout << " Payload: " << command->getPayload()->getData()->getJson().serialize() << std::endl;
5148
}
@@ -55,7 +52,7 @@ void getStatuses() {
5552
auto statuses = fleetApiClient->getStatuses();
5653

5754
std::cout << "Statuses:" << std::endl;
58-
for(auto status: statuses) {
55+
for(const auto& status: statuses.first) {
5956
std::cout << " Timestamp: " << status->getTimestamp() << std::endl;
6057
std::cout << " Payload: " << status->getPayload()->getData()->getJson().serialize() << std::endl;
6158
}
@@ -82,7 +79,7 @@ void getAvailableDevices() {
8279
auto availableDevices = fleetApiClient->getAvailableDevices(1);
8380

8481
std::cout << "All module's devices:" << std::endl;
85-
for(auto device: availableDevices->getDeviceList()) {
82+
for(const auto& device: availableDevices->getDeviceList()) {
8683
std::cout << " " << device->getName() << std::endl;
8784
}
8885
} catch(const std::exception &e) {
@@ -91,16 +88,11 @@ void getAvailableDevices() {
9188
}
9289

9390
int main(int argc, char **argv) {
94-
Logger::addSink<ConsoleSink>();
95-
Logger::LoggerSettings params {"FleetHttpClientExample",
96-
Logger::Verbosity::Debug};
97-
Logger::init(params);
98-
9991
createFleetApiClient();
10092
getCars();
10193
getCommands();
10294
getStatuses();
10395
sendCommand();
10496
sendStatus();
10597
getAvailableDevices();
106-
}
98+
}

include/bringauto/fleet_protocol/http_client/FleetApiClient.hpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ class FleetApiClient {
2222
STATUS_ERROR
2323
};
2424

25+
/**
26+
* @brief Enum class for return codes
27+
*/
28+
enum class ReturnCode {
29+
// request was successful without any delays
30+
OK,
31+
// request was delayed due to request rate threshold being reached
32+
DELAYED
33+
};
34+
2535
/**
2636
* @brief Configuration struct for the FleetApiClient
2737
*/
@@ -49,56 +59,60 @@ class FleetApiClient {
4959
* @brief Sets the DeviceId parameters, required for sendCommand and sendStatuse functions
5060
* @param deviceId DeviceId struct containing the device identification parameters
5161
*/
52-
void setDeviceIdentification(const fleet_protocol::cxx::DeviceID &deviceId);
62+
void setDeviceIdentification(const cxx::DeviceID &deviceId) const;
5363

5464
/**
5565
* @brief Calls the GET function on /cars of Fleet v2 HTTP API
5666
* @param since optional, minimal timestamp of cars to look for
5767
* @param wait optional, if true, waits for a predefined period until any car is found, will check request rate and possibly delay them
58-
* @return Vector of shared pointers to the Car model
68+
* @return Vector of shared pointers to the Car model and a return code
5969
*/
60-
std::vector<std::shared_ptr<org::openapitools::client::model::Car>> getCars(std::optional<int64_t> since = std::nullopt,
61-
std::optional<bool> wait = std::nullopt);
70+
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Car>>, ReturnCode> getCars(
71+
std::optional<int64_t> since = std::nullopt,
72+
std::optional<bool> wait = std::nullopt) const;
6273

6374
/**
6475
* @brief Calls the GET function on /command/{company_name}/{car_name} of Fleet v2 HTTP API
6576
* @param since optional, minimal timestamp of commands to look for
6677
* @param wait optional, if true, waits for a predefined period until any command is found, will check request rate and possibly delay them
67-
* @return Vector of shared pointers to the Message model containing commands in payload data
78+
* @return Vector of shared pointers to the Message model containing commands in payload data and a return code
6879
*/
69-
std::vector<std::shared_ptr<org::openapitools::client::model::Message>> getCommands(std::optional<int64_t> since = std::nullopt,
70-
std::optional<bool> wait = std::nullopt);
80+
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Message>>, ReturnCode> getCommands(
81+
std::optional<int64_t> since = std::nullopt,
82+
std::optional<bool> wait = std::nullopt) const;
7183

7284
/**
7385
* @brief Calls the GET function on /status/{company_name}/{car_name} of Fleet v2 HTTP API
7486
* @param since optional, minimal timestamp of statuses to look for
7587
* @param wait optional, if true, waits for a predefined period until any status is found, will check request rate and possibly delay them
76-
* @return Vector of shared pointers to the Message model containing statuses in payload data
88+
* @return Vector of shared pointers to the Message model containing statuses in payload data and a return code
7789
*/
78-
std::vector<std::shared_ptr<org::openapitools::client::model::Message>> getStatuses(std::optional<int64_t> since = std::nullopt,
79-
std::optional<bool> wait = std::nullopt);
90+
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Message>>, ReturnCode> getStatuses(
91+
std::optional<int64_t> since = std::nullopt,
92+
const std::optional<bool>& wait = std::nullopt) const;
8093

8194
/**
8295
* @brief Calls the POST function on /command/{company_name}/{car_name} of Fleet v2 HTTP API.
8396
* setDeviceIdentification needs to be used beforehand to set DeviceId, otherwise placeholder values will be used.
8497
* @param commandJson payload data of a command represented by a json as a string
8598
*/
86-
void sendCommand(const std::string &commandJson);
99+
void sendCommand(const std::string &commandJson) const;
87100

88101
/**
89102
* @brief Calls the POST function on /status/{company_name}/{car_name} of Fleet v2 HTTP API.
90103
* setDeviceIdentification needs to be used beforehand to set DeviceId, otherwise placeholder values will be used.
91104
* @param statusJson payload data of a status represented by a json as a string
92105
* @param statusType optional, type of the status, default is STATUS
93106
*/
94-
void sendStatus(const std::string &statusJson, StatusType statusType = StatusType::STATUS);
107+
void sendStatus(const std::string &statusJson, StatusType statusType = StatusType::STATUS) const;
95108

96109
/**
97110
* @brief Calls the GET function on /available-devices/{company_name}/{car_name} of Fleet v2 HTTP API
98111
* @param moduleId optional, filters returned devices to only those with matching module Id
99112
* @return Shared pointer to the AvailableDevices model
100113
*/
101-
std::shared_ptr<org::openapitools::client::model::AvailableDevices> getAvailableDevices(std::optional<int32_t> moduleId = std::nullopt);
114+
[[nodiscard]] std::shared_ptr<org::openapitools::client::model::AvailableDevices> getAvailableDevices(
115+
std::optional<int32_t> moduleId = std::nullopt) const;
102116

103117
private:
104118
std::unique_ptr<RequestFrequencyGuard> requestFrequencyGuard_ {};

include/bringauto/fleet_protocol/http_client/RequestFrequencyGuard.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ class RequestFrequencyGuard {
3535
/**
3636
* @brief Determines how long the next request should be delayed when the max request rate is reached
3737
* Saves the current timestamp and calculates the delay based on timestamps of previous requests
38+
* @return true if threshold was reached and long delay was applied
3839
*/
39-
void handleDelays();
40+
bool handleDelays();
4041

4142
private:
4243
uint32_t maxRequestsThresholdCount_ {};
@@ -54,4 +55,4 @@ class RequestFrequencyGuard {
5455
bool isOverThreshold();
5556
};
5657

57-
}// namespace bringauto::fleet_protocol::http_client
58+
}// namespace bringauto::fleet_protocol::http_client

include/bringauto/fleet_protocol/http_client/settings/Constants.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ class Constants {
1919
inline static const std::string STATUS_ERROR_MESSAGE_TYPE {"STATUS_ERROR"};
2020
};
2121

22-
}// namespace bringauto::fleet_protocol::http_client::settings
22+
}// namespace bringauto::fleet_protocol::http_client::settings

src/bringauto/fleet_protocol/http_client/FleetApiClient.cpp

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ FleetApiClient::FleetApiClient(
1313
companyName_(facConfig.companyName),
1414
carName_(facConfig.carName)
1515
{
16-
auto apiConfigPtr = std::make_shared<api::ApiConfiguration>();
16+
const auto apiConfigPtr = std::make_shared<api::ApiConfiguration>();
1717
apiConfigPtr->setBaseUrl(facConfig.apiUrl);
1818
apiConfigPtr->setApiKey(settings::Constants::API_KEY_HEADER_KEY, facConfig.apiKey);
1919
apiClientPtr_ = std::make_shared<api::ApiClient>();
@@ -28,7 +28,7 @@ FleetApiClient::FleetApiClient(
2828
payloadPtr_ = std::make_shared<model::Payload>();
2929
payloadDataPtr_ = std::make_shared<model::Payload_data>();
3030

31-
setDeviceIdentification(fleet_protocol::cxx::DeviceID(0, 0, 0,
31+
setDeviceIdentification(cxx::DeviceID(0, 0, 0,
3232
settings::Constants::DEFAULT_DEVICE_ROLE, settings::Constants::DEFAULT_DEVICE_NAME
3333
));
3434

@@ -42,77 +42,85 @@ FleetApiClient::FleetApiClient(
4242
}
4343

4444

45-
void FleetApiClient::setDeviceIdentification(const fleet_protocol::cxx::DeviceID &deviceId) {
45+
void FleetApiClient::setDeviceIdentification(const cxx::DeviceID &deviceId) const {
4646
auto deviceIdentification = deviceId.getDeviceId();
4747
deviceIdPtr_->setModuleId(deviceIdentification.module);
4848
deviceIdPtr_->setType(deviceIdentification.device_type);
4949
deviceIdPtr_->setRole(std::string(static_cast<char*>(deviceIdentification.device_role.data)));
5050
deviceIdPtr_->setName(std::string(static_cast<char*>(deviceIdentification.device_name.data)));
5151
}
5252

53-
54-
std::vector<std::shared_ptr<model::Car>> FleetApiClient::getCars(std::optional<int64_t> since, std::optional<bool> wait) {
55-
auto carsRequest = carApi_->availableCars(wait.value_or(false), since.value_or(0));
53+
std::pair<std::vector<std::shared_ptr<model::Car>>, FleetApiClient::ReturnCode> FleetApiClient::getCars(
54+
const std::optional<int64_t> since, const std::optional<bool> wait) const
55+
{
56+
const auto carsRequest = carApi_->availableCars(wait.value_or(false), since.value_or(0));
5657
std::vector<std::shared_ptr<model::Car>> cars {};
58+
auto rc = ReturnCode::OK;
5759

5860
try {
5961
cars = carsRequest.get();
60-
} catch(std::exception &e) {
62+
} catch(std::exception &) {
6163
}
6264

63-
if(wait) {
64-
requestFrequencyGuard_->handleDelays();
65+
if(wait && requestFrequencyGuard_->handleDelays()) {
66+
rc = ReturnCode::DELAYED;
6567
}
66-
return cars;
68+
return {cars, rc};
6769
}
6870

6971

70-
std::vector<std::shared_ptr<model::Message>> FleetApiClient::getCommands(std::optional<int64_t> since, std::optional<bool> wait) {
71-
auto commandsRequest = deviceApi_->listCommands(companyName_, carName_, since.value_or(0), wait.value_or(false));
72+
std::pair<std::vector<std::shared_ptr<model::Message>>, FleetApiClient::ReturnCode> FleetApiClient::getCommands(
73+
const std::optional<int64_t> since, const std::optional<bool> wait) const
74+
{
75+
const auto commandsRequest = deviceApi_->listCommands(companyName_, carName_, since.value_or(0), wait.value_or(false));
7276
std::vector<std::shared_ptr<model::Message>> commands {};
77+
auto rc = ReturnCode::OK;
7378

7479
try {
7580
commands = commandsRequest.get();
76-
} catch(std::exception &e) {
81+
} catch(std::exception &) {
7782
}
7883

79-
if(wait) {
80-
requestFrequencyGuard_->handleDelays();
84+
if(wait && requestFrequencyGuard_->handleDelays()) {
85+
rc = ReturnCode::DELAYED;
8186
}
82-
return commands;
87+
return {commands, rc};
8388
}
8489

8590

86-
std::vector<std::shared_ptr<model::Message>> FleetApiClient::getStatuses(std::optional<int64_t> since, std::optional<bool> wait) {
87-
auto statusesRequest = deviceApi_->listStatuses(companyName_, carName_, since.value_or(0), wait.value_or(false));
91+
std::pair<std::vector<std::shared_ptr<model::Message>>, FleetApiClient::ReturnCode> FleetApiClient::getStatuses(
92+
const std::optional<int64_t> since, const std::optional<bool>& wait) const
93+
{
94+
const auto statusesRequest = deviceApi_->listStatuses(companyName_, carName_, since.value_or(0), wait.value_or(false));
8895
std::vector<std::shared_ptr<model::Message>> statuses {};
96+
auto rc = ReturnCode::OK;
8997

9098
try {
9199
statuses = statusesRequest.get();
92-
} catch(std::exception &e) {
100+
} catch(std::exception &) {
93101
}
94102

95-
if(wait) {
96-
requestFrequencyGuard_->handleDelays();
103+
if(wait && requestFrequencyGuard_->handleDelays()) {
104+
rc = ReturnCode::DELAYED;
97105
}
98-
return statuses;
106+
return {statuses, rc};
99107
}
100108

101109

102-
void FleetApiClient::sendCommand(const std::string &commandJson) {
110+
void FleetApiClient::sendCommand(const std::string &commandJson) const {
103111
payloadPtr_->setMessageType(settings::Constants::COMMAND_MESSAGE_TYPE);
104112
payloadDataPtr_->setJson(web::json::value::parse(commandJson));
105113
messagePtr_->setTimestamp(utility::datetime::utc_timestamp());
106114

107115
std::vector<std::shared_ptr<model::Message>> commands;
108116
commands.push_back(messagePtr_);
109117

110-
auto commandsRequest = deviceApi_->sendCommands(companyName_, carName_, commands);
118+
const auto commandsRequest = deviceApi_->sendCommands(companyName_, carName_, commands);
111119
commandsRequest.wait();
112120
}
113121

114122

115-
void FleetApiClient::sendStatus(const std::string &statusJson, StatusType statusType) {
123+
void FleetApiClient::sendStatus(const std::string &statusJson, const StatusType statusType) const {
116124
switch(statusType) {
117125
case StatusType::STATUS:
118126
payloadPtr_->setMessageType(settings::Constants::STATUS_MESSAGE_TYPE);
@@ -129,18 +137,18 @@ void FleetApiClient::sendStatus(const std::string &statusJson, StatusType status
129137
std::vector<std::shared_ptr<model::Message>> statuses;
130138
statuses.push_back(messagePtr_);
131139

132-
auto statusesRequest = deviceApi_->sendStatuses(companyName_, carName_, statuses);
140+
const auto statusesRequest = deviceApi_->sendStatuses(companyName_, carName_, statuses);
133141
statusesRequest.wait();
134142
}
135143

136144

137-
std::shared_ptr<model::AvailableDevices> FleetApiClient::getAvailableDevices(std::optional<int32_t> moduleId) {
145+
std::shared_ptr<model::AvailableDevices> FleetApiClient::getAvailableDevices(std::optional<int32_t> moduleId) const {
138146
boost::optional<int32_t> moduleIdBoost = boost::none;
139147
if(moduleId.has_value()) {
140148
moduleIdBoost = moduleId.value();
141149
}
142150

143-
auto availableDevicesRequest = moduleApi_->availableDevices(companyName_, carName_, moduleIdBoost);
151+
const auto availableDevicesRequest = moduleApi_->availableDevices(companyName_, carName_, moduleIdBoost);
144152
return availableDevicesRequest.get();
145153
}
146154

0 commit comments

Comments
 (0)