Skip to content

Commit

Permalink
return enum instead of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioIvancik committed Aug 28, 2024
1 parent 84450da commit 9716efb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
20 changes: 14 additions & 6 deletions include/bringauto/fleet_protocol/http_client/FleetApiClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class FleetApiClient {
STATUS_ERROR
};

/**
* @brief Enum class for return codes
*/
enum class ReturnCode {
OK,
DELAYED
};

/**
* @brief Configuration struct for the FleetApiClient
*/
Expand Down Expand Up @@ -55,29 +63,29 @@ class FleetApiClient {
* @brief Calls the GET function on /cars of Fleet v2 HTTP API
* @param since optional, minimal timestamp of cars to look for
* @param wait optional, if true, waits for a predefined period until any car is found, will check request rate and possibly delay them
* @return Vector of shared pointers to the Car model and a boolean indicating if the request was delayed
* @return Vector of shared pointers to the Car model and a return code
*/
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Car>>, bool> getCars(
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Car>>, ReturnCode> getCars(
std::optional<int64_t> since = std::nullopt,
std::optional<bool> wait = std::nullopt) const;

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

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

Expand Down
30 changes: 15 additions & 15 deletions src/bringauto/fleet_protocol/http_client/FleetApiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,60 +50,60 @@ void FleetApiClient::setDeviceIdentification(const cxx::DeviceID &deviceId) cons
deviceIdPtr_->setName(std::string(static_cast<char*>(deviceIdentification.device_name.data)));
}

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

try {
cars = carsRequest.get();
} catch(std::exception &) {
}

if(wait) {
delayed = requestFrequencyGuard_->handleDelays();
if(wait && requestFrequencyGuard_->handleDelays()) {
rc = ReturnCode::DELAYED;
}
return {cars, delayed};
return {cars, rc};
}


std::pair<std::vector<std::shared_ptr<model::Message>>, bool> FleetApiClient::getCommands(
std::pair<std::vector<std::shared_ptr<model::Message>>, FleetApiClient::ReturnCode> FleetApiClient::getCommands(
const std::optional<int64_t> since, const std::optional<bool> wait) const
{
const auto commandsRequest = deviceApi_->listCommands(companyName_, carName_, since.value_or(0), wait.value_or(false));
std::vector<std::shared_ptr<model::Message>> commands {};
bool delayed = false;
auto rc = ReturnCode::OK;

try {
commands = commandsRequest.get();
} catch(std::exception &) {
}

if(wait) {
delayed = requestFrequencyGuard_->handleDelays();
if(wait && requestFrequencyGuard_->handleDelays()) {
rc = ReturnCode::DELAYED;
}
return {commands, delayed};
return {commands, rc};
}


std::pair<std::vector<std::shared_ptr<model::Message>>, bool> FleetApiClient::getStatuses(
std::pair<std::vector<std::shared_ptr<model::Message>>, FleetApiClient::ReturnCode> FleetApiClient::getStatuses(
const std::optional<int64_t> since, const std::optional<bool>& wait) const
{
const auto statusesRequest = deviceApi_->listStatuses(companyName_, carName_, since.value_or(0), wait.value_or(false));
std::vector<std::shared_ptr<model::Message>> statuses {};
bool delayed = false;
auto rc = ReturnCode::OK;

try {
statuses = statusesRequest.get();
} catch(std::exception &) {
}

if(wait) {
delayed = requestFrequencyGuard_->handleDelays();
if(wait && requestFrequencyGuard_->handleDelays()) {
rc = ReturnCode::DELAYED;
}
return {statuses, delayed};
return {statuses, rc};
}


Expand Down
9 changes: 4 additions & 5 deletions test/src/FleetApiClientTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <TestConstants.hpp>

#include <chrono>
#include <cstdio>
#include <gtest/gtest.h>


Expand All @@ -32,7 +31,7 @@ TEST(FleetApiClientTests, DelayRepeatedRequests) {

const auto fleetApiClient = std::make_unique<FleetApiClient>(facConfig, rfgConfig);
auto timeBefore = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
bool delayed;
FleetApiClient::ReturnCode delayed;

// Do MAX_REQUEST_THRESHOLD_COUNT requests with no delay which should trigger the threshold
std::cout << "Expecting " << DELAY_AFTER_THRESHOLD_REACHED_MS << "ms delay" << std::endl;
Expand All @@ -41,7 +40,7 @@ TEST(FleetApiClientTests, DelayRepeatedRequests) {
delayed = ret.second;
}

ASSERT_TRUE(delayed);
ASSERT_EQ(delayed, FleetApiClient::ReturnCode::DELAYED);
auto timeAfter = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
ASSERT_GE(timeAfter - timeBefore, DELAY_AFTER_THRESHOLD_REACHED_MS);
ASSERT_LT(timeAfter - timeBefore, MAX_DELAY_AFTER_THRESHOLD_REACHED_MS);
Expand All @@ -52,7 +51,7 @@ TEST(FleetApiClientTests, DelayRepeatedRequests) {
for(int i = 0; i < MAX_REQUEST_THRESHOLD_COUNT; i++) {
std::cout << "Expecting " << RETRY_REQUESTS_DELAY_MS << "ms delay" << std::endl;
auto ret = fleetApiClient->getStatuses(0, true);
ASSERT_FALSE(ret.second);
ASSERT_EQ(ret.second, FleetApiClient::ReturnCode::OK);
}

timeAfter = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
Expand All @@ -64,7 +63,7 @@ TEST(FleetApiClientTests, DelayRepeatedRequests) {
// The next request should be delayed by DELAY_AFTER_THRESHOLD_REACHED_MS
std::cout << "Expecting " << DELAY_AFTER_THRESHOLD_REACHED_MS << "ms delay" << std::endl;
auto ret = fleetApiClient->getCars(0, true);
ASSERT_TRUE(ret.second);
ASSERT_EQ(ret.second, FleetApiClient::ReturnCode::DELAYED);

timeAfter = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
ASSERT_GE(timeAfter - timeBefore, DELAY_AFTER_THRESHOLD_REACHED_MS);
Expand Down

0 comments on commit 9716efb

Please sign in to comment.