Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 HERE Europe B.V.
* Copyright (C) 2020-2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
#include <string>

#include <olp/authentication/AuthenticationApi.h>
#include <olp/core/client/RetrySettings.h>
#include <olp/core/http/NetworkProxySettings.h>
#include <boost/optional.hpp>

Expand Down Expand Up @@ -89,6 +90,12 @@ struct AUTHENTICATION_API AuthenticationSettings {
* authentication server.
*/
bool use_system_time{true};

/**
* @brief A collection of settings that controls how failed requests should be
* treated.
*/
client::RetrySettings retry_settings;
};

} // namespace authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>
#include <string>

#include <olp/core/client/RetrySettings.h>
#include <olp/core/http/NetworkProxySettings.h>

#include "AuthenticationApi.h"
Expand Down Expand Up @@ -98,6 +99,12 @@ struct AUTHENTICATION_API Settings {
* authentication server.
*/
bool use_system_time{true};

/**
* @brief A collection of settings that controls how failed requests should be
* treated.
*/
client::RetrySettings retry_settings;
};

} // namespace authentication
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 HERE Europe B.V.
* Copyright (C) 2019-2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -119,9 +119,9 @@ class TokenProvider {

/// @copydoc TokenProvider::operator()()
std::string operator()() const {
return GetResponse().IsSuccessful()
? GetResponse().GetResult().GetAccessToken()
: "";
auto response = GetResponse();
return response.IsSuccessful() ? response.GetResult().GetAccessToken()
: "";
}

/// @copydoc TokenProvider::GetErrorResponse()
Expand Down
48 changes: 27 additions & 21 deletions olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 HERE Europe B.V.
* Copyright (C) 2020-2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -99,19 +99,17 @@ constexpr auto kOperator = "operator";
constexpr auto kErrorWrongTimestamp = 401204;
constexpr auto kLogTag = "AuthenticationClient";

constexpr auto kDefaultRetryCount = 3;
constexpr auto kDefaultRetryTime = std::chrono::milliseconds(200);

bool HasWrongTimestamp(olp::authentication::SignInResult& result) {
const auto& error_response = result.GetErrorResponse();
const auto status = result.GetStatus();
return status == olp::http::HttpStatusCode::UNAUTHORIZED &&
error_response.code == kErrorWrongTimestamp;
}

void RetryDelay(size_t retry) {
client::ExponentialBackdownStrategy retry_delay;
std::this_thread::sleep_for(retry_delay(kDefaultRetryTime, retry));
void RetryDelay(const client::RetrySettings& retry_settings, size_t retry) {
std::this_thread::sleep_for(retry_settings.backdown_strategy(
std::chrono::milliseconds(retry_settings.initial_backdown_period),
retry));
}

client::OlpClient::RequestBodyType GenerateAppleSignInBody(
Expand Down Expand Up @@ -284,7 +282,9 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(

SignInResult response;

for (auto retry = 0; retry < kDefaultRetryCount; ++retry) {
const auto& retry_settings = settings_.retry_settings;

for (auto retry = 0; retry < retry_settings.max_attempts; ++retry) {
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
Expand All @@ -310,8 +310,8 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(

response = ParseAuthResponse(status, auth_response.response);

if (client::DefaultRetryCondition(auth_response)) {
RetryDelay(retry);
if (retry_settings.retry_condition(auth_response)) {
RetryDelay(retry_settings, retry);
continue;
}

Expand Down Expand Up @@ -492,7 +492,9 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(

SignInUserResult response;

for (auto retry = 0; retry < kDefaultRetryCount; ++retry) {
const auto& retry_settings = settings_.retry_settings;

for (auto retry = 0; retry < retry_settings.max_attempts; ++retry) {
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
Expand All @@ -517,8 +519,8 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(

response = ParseUserAuthResponse(status, auth_response.response);

if (client::DefaultRetryCondition(auth_response)) {
RetryDelay(retry);
if (retry_settings.retry_condition(auth_response)) {
RetryDelay(retry_settings, retry);
continue;
}

Expand Down Expand Up @@ -560,10 +562,12 @@ client::CancellationToken AuthenticationClientImpl::SignUpHereUser(
std::string url = settings_.token_endpoint_url;
url.append(kUserEndpoint);
http::NetworkRequest request(url);
http::NetworkSettings network_settings;
if (settings_.network_proxy_settings) {
network_settings.WithProxySettings(settings_.network_proxy_settings.get());
}
auto network_settings =
http::NetworkSettings()
.WithTransferTimeout(settings_.retry_settings.timeout)
.WithConnectionTimeout(settings_.retry_settings.timeout)
.WithProxySettings(settings_.network_proxy_settings.get_value_or({}));

request.WithVerb(http::NetworkRequest::HttpVerb::POST);

auto auth_header = GenerateAuthorizationHeader(
Expand Down Expand Up @@ -637,10 +641,12 @@ client::CancellationToken AuthenticationClientImpl::SignOut(
std::string url = settings_.token_endpoint_url;
url.append(kSignoutEndpoint);
http::NetworkRequest request(url);
http::NetworkSettings network_settings;
if (settings_.network_proxy_settings) {
network_settings.WithProxySettings(settings_.network_proxy_settings.get());
}
auto network_settings =
http::NetworkSettings()
.WithTransferTimeout(settings_.retry_settings.timeout)
.WithConnectionTimeout(settings_.retry_settings.timeout)
.WithProxySettings(settings_.network_proxy_settings.get_value_or({}));

request.WithVerb(http::NetworkRequest::HttpVerb::POST);
request.WithHeader(http::kAuthorizationHeader,
GenerateBearerHeader(access_token));
Expand Down
5 changes: 2 additions & 3 deletions olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 HERE Europe B.V.
* Copyright (C) 2020-2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -264,8 +264,7 @@ client::OlpClient CreateOlpClient(
settings.network_request_handler = auth_settings.network_request_handler;
settings.authentication_settings = authentication_settings;
settings.proxy_settings = auth_settings.network_proxy_settings;
settings.retry_settings.backdown_strategy =
client::ExponentialBackdownStrategy();
settings.retry_settings = auth_settings.retry_settings;

if (!retry) {
settings.retry_settings.max_attempts = 0;
Expand Down
3 changes: 2 additions & 1 deletion olp-cpp-sdk-authentication/src/TokenEndpoint.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 HERE Europe B.V.
* Copyright (C) 2019-2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@ AuthenticationSettings ConvertSettings(Settings settings) {
auth_settings.network_request_handler = settings.network_request_handler;
auth_settings.token_endpoint_url = settings.token_endpoint_url;
auth_settings.use_system_time = settings.use_system_time;
auth_settings.retry_settings = settings.retry_settings;
return auth_settings;
}
} // namespace
Expand Down
3 changes: 2 additions & 1 deletion olp-cpp-sdk-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ set(OLP_SDK_CLIENT_HEADERS
./include/olp/core/client/OlpClientSettings.h
./include/olp/core/client/OlpClientSettingsFactory.h
./include/olp/core/client/PendingRequests.h
./include/olp/core/client/RetrySettings.h
./include/olp/core/client/TaskContext.h
)

Expand Down Expand Up @@ -253,11 +254,11 @@ set(OLP_SDK_CLIENT_SOURCES
./src/client/HRN.cpp
./src/client/OlpClient.cpp
./src/client/OlpClientFactory.cpp
./src/client/OlpClientSettings.cpp
./src/client/OlpClientSettingsFactory.cpp
./src/client/PendingRequests.cpp
./src/client/PendingUrlRequests.h
./src/client/PendingUrlRequests.cpp
./src/client/RetrySettings.cpp
./src/client/Tokenizer.h
)

Expand Down
66 changes: 2 additions & 64 deletions olp-cpp-sdk-core/include/olp/core/client/OlpClientSettings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 HERE Europe B.V.
* Copyright (C) 2019-2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,11 +26,11 @@

#include <boost/optional.hpp>

#include <olp/core/client/BackdownStrategy.h>
#include <olp/core/client/CancellationToken.h>
#include <olp/core/client/DefaultLookupEndpointProvider.h>
#include <olp/core/client/HRN.h>
#include <olp/core/client/HttpResponse.h>
#include <olp/core/client/RetrySettings.h>
#include <olp/core/http/Network.h>

namespace olp {
Expand Down Expand Up @@ -61,11 +61,6 @@ using NetworkAsyncCallback = std::function<void(HttpResponse)>;
*/
using NetworkAsyncCancel = std::function<void()>;

/**
* @brief The default retry condition that disables retries.
*/
CORE_API bool DefaultRetryCondition(const olp::client::HttpResponse& response);

/**
* @brief A set of settings that manages the `TokenProviderCallback` and
* `TokenProviderCancelCallback` functions.
Expand Down Expand Up @@ -146,63 +141,6 @@ struct CORE_API AuthenticationSettings {
boost::optional<TokenProviderCancelCallback> cancel;
};

/**
* @brief A collection of settings that controls how failed requests should be
* treated by the Data SDK.
*
* For example, it specifies whether the failed request should be retried, how
* long Data SDK needs to wait for the next retry attempt, the number of maximum
* retries, and so on.
*
* You can customize all of these settings. The settings are used internally by
* the `OlpClient` class.
*/
struct CORE_API RetrySettings {
/**
* @brief Calculates the number of retry timeouts based on
* the initial backdown duration and retries count.
*/
using BackdownStrategy = std::function<std::chrono::milliseconds(
std::chrono::milliseconds, size_t)>;

/**
* @brief Checks whether the retry is desired.
*
* @see `HttpResponse` for more details.
*/
using RetryCondition = std::function<bool(const HttpResponse&)>;

/**
* @brief The number of attempts.
*
* The default value is 3.
*/
int max_attempts = 3;

/**
* @brief The connection timeout limit (in seconds).
*/
int timeout = 60;

/**
* @brief The period between the error and the first retry attempt (in
* milliseconds).
*/
int initial_backdown_period = 200;

/**
* @brief The backdown strategy.
*
* Defines the delay between retries on a failed request.
*/
BackdownStrategy backdown_strategy = ExponentialBackdownStrategy();

/**
* @brief Evaluates responses to determine if the retry should be attempted.
*/
RetryCondition retry_condition = DefaultRetryCondition;
};

/**
* @brief Settings to provide URLs for API lookup requests.
*/
Expand Down
Loading