diff --git a/olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp b/olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp index ff6d03f56..3ff6616c2 100644 --- a/olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp +++ b/olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp @@ -275,9 +275,7 @@ client::OlpClient CreateOlpClient( settings.retry_settings.max_attempts = 0; } - client::OlpClient client; - client.SetBaseUrl(auth_settings.token_endpoint_url); - client.SetSettings(std::move(settings)); + client::OlpClient client(settings, auth_settings.token_endpoint_url); return client; } diff --git a/olp-cpp-sdk-core/include/olp/core/client/OlpClient.h b/olp-cpp-sdk-core/include/olp/core/client/OlpClient.h index 136c832e5..a472d1798 100644 --- a/olp-cpp-sdk-core/include/olp/core/client/OlpClient.h +++ b/olp-cpp-sdk-core/include/olp/core/client/OlpClient.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2023 HERE Europe B.V. + * Copyright (C) 2019-2024 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. @@ -92,21 +92,6 @@ class CORE_API OlpClient { */ ParametersType& GetMutableDefaultHeaders(); - /** - * @brief Sets the client settings. - * - * @note Handle with care and do not change while requests are ongoing. - * Ideally the settings would not change during the lifecycle of this - * instance. - * - * @param settings The client settings. - * - * @deprecated This method will be removed by 05.2021. Please use the - * constructor instead. The settings should not change during the lifetime of - * the instance. - */ - void SetSettings(const OlpClientSettings& settings); - /** * @brief Getter function to retrieve client settings. * diff --git a/olp-cpp-sdk-core/src/client/ApiLookupClientImpl.cpp b/olp-cpp-sdk-core/src/client/ApiLookupClientImpl.cpp index fa633a1a7..d50b259e9 100644 --- a/olp-cpp-sdk-core/src/client/ApiLookupClientImpl.cpp +++ b/olp-cpp-sdk-core/src/client/ApiLookupClientImpl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 HERE Europe B.V. + * Copyright (C) 2020-2024 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. @@ -47,9 +47,7 @@ std::string FindApi(const Apis& apis, const std::string& service, OlpClient CreateClient(const std::string& base_url, const OlpClientSettings& settings) { - OlpClient client; - client.SetBaseUrl(base_url); - client.SetSettings(settings); + OlpClient client(settings, base_url); return client; } @@ -227,20 +225,28 @@ CancellationToken ApiLookupClientImpl::LookupApi( OlpClient ApiLookupClientImpl::CreateAndCacheClient( const std::string& base_url, const std::string& cache_key, boost::optional expiration) { + const auto new_expiration = + std::chrono::steady_clock::now() + + std::chrono::seconds(expiration.value_or(kLookupApiDefaultExpiryTime)); + std::lock_guard lock(cached_clients_mutex_); - ClientWithExpiration& client_with_expiration = cached_clients_[cache_key]; - const auto current_base_url = client_with_expiration.client.GetBaseUrl(); - if (current_base_url.empty()) { - client_with_expiration.client.SetSettings(settings_); + auto findIt = cached_clients_.find(cache_key); + if (findIt == cached_clients_.end()) { + auto emplace_result = cached_clients_.emplace( + cache_key, + ClientWithExpiration{OlpClient(settings_, base_url), new_expiration}); + return emplace_result.first->second.client; } + + ClientWithExpiration& client_with_expiration = findIt->second; + const auto current_base_url = client_with_expiration.client.GetBaseUrl(); if (current_base_url != base_url) { client_with_expiration.client.SetBaseUrl(base_url); } - client_with_expiration.expire_at = - std::chrono::steady_clock::now() + - std::chrono::seconds(expiration.value_or(kLookupApiDefaultExpiryTime)); + client_with_expiration.expire_at = new_expiration; + return client_with_expiration.client; } diff --git a/olp-cpp-sdk-core/src/client/OlpClient.cpp b/olp-cpp-sdk-core/src/client/OlpClient.cpp index 569ed80c3..cb4739216 100644 --- a/olp-cpp-sdk-core/src/client/OlpClient.cpp +++ b/olp-cpp-sdk-core/src/client/OlpClient.cpp @@ -466,7 +466,6 @@ class OlpClient::OlpClientImpl { std::string GetBaseUrl() const; ParametersType& GetMutableDefaultHeaders(); - void SetSettings(const OlpClientSettings& settings); const OlpClientSettings& GetSettings() const { return settings_; } CancellationToken CallApi(const std::string& path, const std::string& method, @@ -527,11 +526,6 @@ OlpClient::OlpClientImpl::GetMutableDefaultHeaders() { return default_headers_; } -void OlpClient::OlpClientImpl::SetSettings(const OlpClientSettings& settings) { - // I would not expect that settings change during lifetime of the instance. - settings_ = settings; -} - boost::optional OlpClient::OlpClientImpl::AddBearer( bool query_empty, http::NetworkRequest& request, CancellationContext& context) const { @@ -853,10 +847,6 @@ OlpClient::ParametersType& OlpClient::GetMutableDefaultHeaders() { return impl_->GetMutableDefaultHeaders(); } -void OlpClient::SetSettings(const OlpClientSettings& settings) { - impl_->SetSettings(settings); -} - const OlpClientSettings& OlpClient::GetSettings() const { return impl_->GetSettings(); } diff --git a/olp-cpp-sdk-core/src/client/OlpClientFactory.cpp b/olp-cpp-sdk-core/src/client/OlpClientFactory.cpp index c16ada841..c5de4723a 100644 --- a/olp-cpp-sdk-core/src/client/OlpClientFactory.cpp +++ b/olp-cpp-sdk-core/src/client/OlpClientFactory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2021 HERE Europe B.V. + * Copyright (C) 2019-2024 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. @@ -27,8 +27,8 @@ namespace client { std::shared_ptr OlpClientFactory::Create( const OlpClientSettings& settings) { - auto olp_client = std::make_shared(); - olp_client->SetSettings(settings); + const auto kEmptyBaseUrl = std::string(); + auto olp_client = std::make_shared(settings, kEmptyBaseUrl); return olp_client; } diff --git a/olp-cpp-sdk-core/tests/client/OlpClientTest.cpp b/olp-cpp-sdk-core/tests/client/OlpClientTest.cpp index 6c1f5a141..2d9df84d5 100644 --- a/olp-cpp-sdk-core/tests/client/OlpClientTest.cpp +++ b/olp-cpp-sdk-core/tests/client/OlpClientTest.cpp @@ -42,6 +42,7 @@ namespace http = olp::http; enum class CallApiType { ASYNC, SYNC }; +static constexpr auto kEmptyBaseUrl = ""; static constexpr auto kCallbackSleepTime = std::chrono::milliseconds(50); static constexpr auto kCallbackWaitTime = std::chrono::seconds(10); static const auto kToManyRequestResponse = @@ -142,24 +143,23 @@ class OlpClientTest : public ::testing::TestWithParam { void SetUp() override { network_ = std::make_shared(); client_settings_.network_request_handler = network_; + } + + std::shared_ptr MakeCallWrapper( + const olp::client::OlpClient& client) { switch (GetParam()) { case CallApiType::ASYNC: - call_wrapper_ = - std::make_shared>(client_); - break; + return std::make_shared>(client); case CallApiType::SYNC: - call_wrapper_ = - std::make_shared>(client_); - break; + return std::make_shared>(client); default: ADD_FAILURE() << "Invalid type of CallApi wrapper"; break; } + return nullptr; } olp::client::OlpClientSettings client_settings_; - olp::client::OlpClient client_; - std::shared_ptr call_wrapper_; std::shared_ptr network_; }; @@ -168,7 +168,8 @@ TEST_P(OlpClientTest, NumberOfAttempts) { client_settings_.retry_settings.max_attempts = 5; client_settings_.retry_settings.retry_condition = [](const olp::client::HttpResponse&) { return true; }; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::vector> futures; olp::http::RequestId request_id = 5; @@ -196,7 +197,8 @@ TEST_P(OlpClientTest, NumberOfAttempts) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); for (auto& future : futures) { future.wait(); @@ -211,7 +213,8 @@ TEST_P(OlpClientTest, ZeroAttempts) { client_settings_.retry_settings.max_attempts = 0; client_settings_.retry_settings.retry_condition = ([](const olp::client::HttpResponse&) { return true; }); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::vector> futures; olp::http::RequestId request_id = 5; @@ -237,7 +240,8 @@ TEST_P(OlpClientTest, ZeroAttempts) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); for (auto& future : futures) { future.wait(); @@ -262,7 +266,8 @@ TEST_P(OlpClientTest, DefaultRetryCondition) { [](std::chrono::milliseconds, size_t) { return std::chrono::milliseconds(0); }; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); EXPECT_CALL(*network, Send(_, _, _, _, _)) .Times(static_cast(attempt_statuses.size())) @@ -298,7 +303,8 @@ TEST_P(OlpClientTest, DefaultRetryCondition) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); for (auto& future : futures) { future.wait(); @@ -317,7 +323,8 @@ TEST_P(OlpClientTest, RetryCondition) { ([](const olp::client::HttpResponse& response) { return response.GetStatus() == http::HttpStatusCode::TOO_MANY_REQUESTS; }); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::vector> futures; olp::http::RequestId request_id = 5; @@ -356,7 +363,8 @@ TEST_P(OlpClientTest, RetryCondition) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); for (auto& future : futures) { future.wait(); @@ -391,7 +399,8 @@ TEST_P(OlpClientTest, RetryWithExponentialBackdownStrategy) { wait_times.push_back(wait_time.count()); return wait_time; }; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); const auto requests_count = retry_settings.max_attempts + 1; std::vector timestamps; @@ -423,7 +432,8 @@ TEST_P(OlpClientTest, RetryWithExponentialBackdownStrategy) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); ASSERT_EQ(kToManyRequestResponse.GetStatus(), response.GetStatus()); ASSERT_EQ(client_settings_.retry_settings.max_attempts, expected_retry_count); @@ -458,7 +468,8 @@ TEST_P(OlpClientTest, RetryTimeout) { return initial_backdown_period * static_cast(std::pow(2, retry_count)); }; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); auto network = network_; size_t current_attempt = 0; @@ -502,7 +513,8 @@ TEST_P(OlpClientTest, RetryTimeout) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); for (auto& future : futures) { future.wait(); @@ -521,7 +533,8 @@ TEST_P(OlpClientTest, Timeout) { std::chrono::milliseconds connection_timeout{0}; std::chrono::milliseconds transfer_timeout{0}; auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::vector> futures; olp::http::RequestId request_id = 5; @@ -554,7 +567,8 @@ TEST_P(OlpClientTest, Timeout) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); for (auto& future : futures) { future.wait(); @@ -580,7 +594,8 @@ TEST_P(OlpClientTest, Proxy) { client_settings_.proxy_settings = expected_settings; olp::http::NetworkProxySettings result_settings; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -604,7 +619,8 @@ TEST_P(OlpClientTest, Proxy) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); future.wait(); @@ -624,7 +640,8 @@ TEST_P(OlpClientTest, EmptyProxy) { ASSERT_FALSE(client_settings_.proxy_settings); olp::http::NetworkProxySettings result_settings; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -648,7 +665,8 @@ TEST_P(OlpClientTest, EmptyProxy) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); future.wait(); @@ -659,7 +677,8 @@ TEST_P(OlpClientTest, EmptyProxy) { TEST_P(OlpClientTest, HttpResponse) { auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -682,7 +701,8 @@ TEST_P(OlpClientTest, HttpResponse) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); future.wait(); @@ -696,8 +716,8 @@ TEST_P(OlpClientTest, HttpResponse) { TEST_P(OlpClientTest, Paths) { auto network = network_; - client_.SetBaseUrl("https://here.com"); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, "https://here.com"); std::future future; olp::http::RequestId request_id = 5; @@ -721,8 +741,9 @@ TEST_P(OlpClientTest, Paths) { return olp::http::SendOutcome(current_request_id); }); + auto call_wrapper = MakeCallWrapper(client); auto response = - call_wrapper_->CallApi("/index", "GET", {}, {}, {}, nullptr, {}); + call_wrapper->CallApi("/index", "GET", {}, {}, {}, nullptr, {}); future.wait(); testing::Mock::VerifyAndClearExpectations(network.get()); @@ -730,7 +751,8 @@ TEST_P(OlpClientTest, Paths) { TEST_P(OlpClientTest, Method) { auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -765,13 +787,14 @@ TEST_P(OlpClientTest, Method) { return olp::http::SendOutcome(current_request_id); }); + auto call_wrapper = MakeCallWrapper(client); for (size_t idx = 0; idx < methods.size(); ++idx) { const auto& method = methods[idx]; expected_verb = expected[idx]; SCOPED_TRACE(testing::Message() << "Method=" << method); - auto response = call_wrapper_->CallApi({}, method, {}, {}, {}, nullptr, {}); + auto response = call_wrapper->CallApi({}, method, {}, {}, {}, nullptr, {}); future.wait(); } @@ -780,7 +803,8 @@ TEST_P(OlpClientTest, Method) { TEST_P(OlpClientTest, QueryParam) { auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -807,8 +831,9 @@ TEST_P(OlpClientTest, QueryParam) { std::multimap query_params = {{"var1", ""}, {"var2", "2"}}; + auto call_wrapper = MakeCallWrapper(client); auto response = - call_wrapper_->CallApi("index", "GET", query_params, {}, {}, nullptr, {}); + call_wrapper->CallApi("index", "GET", query_params, {}, {}, nullptr, {}); future.wait(); testing::Mock::VerifyAndClearExpectations(network.get()); @@ -818,7 +843,8 @@ TEST_P(OlpClientTest, HeaderParams) { auto network = network_; std::multimap header_params = {{"head1", "value1"}, {"head2", "value2"}}; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -853,8 +879,9 @@ TEST_P(OlpClientTest, HeaderParams) { return olp::http::SendOutcome(current_request_id); }); + auto call_wrapper = MakeCallWrapper(client); auto response = - call_wrapper_->CallApi({}, "GET", {}, header_params, {}, nullptr, {}); + call_wrapper->CallApi({}, "GET", {}, header_params, {}, nullptr, {}); future.wait(); testing::Mock::VerifyAndClearExpectations(network.get()); @@ -862,9 +889,11 @@ TEST_P(OlpClientTest, HeaderParams) { TEST_P(OlpClientTest, DefaultHeaderParams) { auto network = network_; - client_.GetMutableDefaultHeaders().insert(std::make_pair("head1", "value1")); - client_.GetMutableDefaultHeaders().insert(std::make_pair("head2", "value2")); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); + + client.GetMutableDefaultHeaders().insert(std::make_pair("head1", "value1")); + client.GetMutableDefaultHeaders().insert(std::make_pair("head2", "value2")); std::future future; olp::http::RequestId request_id = 5; @@ -899,7 +928,8 @@ TEST_P(OlpClientTest, DefaultHeaderParams) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}); future.wait(); testing::Mock::VerifyAndClearExpectations(network.get()); @@ -907,11 +937,13 @@ TEST_P(OlpClientTest, DefaultHeaderParams) { TEST_P(OlpClientTest, CombineHeaderParams) { auto network = network_; - client_.GetMutableDefaultHeaders().insert(std::make_pair("head1", "value1")); - client_.GetMutableDefaultHeaders().insert(std::make_pair("head2", "value2")); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); + + client.GetMutableDefaultHeaders().insert(std::make_pair("head1", "value1")); + client.GetMutableDefaultHeaders().insert(std::make_pair("head2", "value2")); std::multimap header_params; header_params.insert(std::make_pair("head3", "value3")); - client_.SetSettings(client_settings_); std::future future; olp::http::RequestId request_id = 5; @@ -948,8 +980,9 @@ TEST_P(OlpClientTest, CombineHeaderParams) { return olp::http::SendOutcome(current_request_id); }); + auto call_wrapper = MakeCallWrapper(client); auto response = - call_wrapper_->CallApi({}, "GET", {}, header_params, {}, nullptr, {}); + call_wrapper->CallApi({}, "GET", {}, header_params, {}, nullptr, {}); future.wait(); testing::Mock::VerifyAndClearExpectations(network.get()); @@ -959,7 +992,8 @@ TEST_P(OlpClientTest, QueryMultiParams) { std::string uri; std::vector> headers; auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::multimap query_params = { {"a", "a1"}, {"b", "b1"}, {"b", "b2"}, @@ -992,8 +1026,9 @@ TEST_P(OlpClientTest, QueryMultiParams) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, {}, query_params, header_params, - {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, {}, query_params, header_params, {}, + nullptr, {}); // query test for (auto q : query_params) { std::string param_equal_value = q.first + "=" + q.second; @@ -1022,12 +1057,14 @@ TEST_P(OlpClientTest, QueryMultiParams) { TEST_P(OlpClientTest, Content) { auto network = network_; - client_.GetMutableDefaultHeaders().insert(std::make_pair("head1", "value1")); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); + + client.GetMutableDefaultHeaders().insert(std::make_pair("head1", "value1")); std::multimap header_params = {{"head3", "value3"}}; const std::string content_string = "something"; const auto content = std::make_shared>( content_string.begin(), content_string.end()); - client_.SetSettings(client_settings_); std::future future; olp::http::RequestId request_id = 5; @@ -1066,8 +1103,9 @@ TEST_P(OlpClientTest, Content) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi({}, "GET", {}, header_params, {}, - content, "plain-text"); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, "GET", {}, header_params, {}, + content, "plain-text"); future.wait(); testing::Mock::VerifyAndClearExpectations(network.get()); @@ -1080,8 +1118,8 @@ TEST_P(OlpClientTest, CancelBeforeResponse) { auto cancelled = std::make_shared(false); constexpr int kExpectedError = static_cast(olp::http::ErrorCode::CANCELLED_ERROR); - client_.SetBaseUrl("https://www.google.com"); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, "https://www.google.com"); std::future future; olp::http::RequestId request_id = 5; @@ -1115,8 +1153,9 @@ TEST_P(OlpClientTest, CancelBeforeResponse) { olp::client::CancellationContext context; + auto call_wrapper = MakeCallWrapper(client); auto response_future = std::async(std::launch::async, [&]() { - return call_wrapper_->CallApi({}, "GET", {}, {}, {}, nullptr, {}, context); + return call_wrapper->CallApi({}, "GET", {}, {}, {}, nullptr, {}, context); }); // Wait for Network call and cancel it @@ -1142,8 +1181,8 @@ TEST_P(OlpClientTest, HeadersCallbackAfterCancel) { auto cancel_wait = std::make_shared>(); auto network_wait = std::make_shared>(); auto cancelled = std::make_shared(false); - client_.SetBaseUrl("https://www.google.com"); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, "https://www.google.com"); std::future future; olp::http::RequestId request_id = 5; @@ -1180,9 +1219,10 @@ TEST_P(OlpClientTest, HeadersCallbackAfterCancel) { olp::client::CancellationContext context; + auto call_wrapper = MakeCallWrapper(client); auto response_future = std::async(std::launch::async, [&]() { - return call_wrapper_->CallApi({}, "GET", {}, {{"header", "header"}}, {}, - nullptr, {}, context); + return call_wrapper->CallApi({}, "GET", {}, {{"header", "header"}}, {}, + nullptr, {}, context); }); network_wait->get_future().wait(); @@ -1203,22 +1243,22 @@ TEST_P(OlpClientTest, HeadersCallbackAfterCancel) { // Test is valid only valid for sync api TEST_P(OlpClientTest, CancelBeforeExecution) { - client_.SetBaseUrl("https://www.google.com"); auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, "https://www.google.com"); EXPECT_CALL(*network, Send(_, _, _, _, _)).Times(0); olp::client::CancellationContext context; context.CancelOperation(); - auto response = client_.CallApi({}, "GET", {}, {}, {}, nullptr, {}, context); + auto response = client.CallApi({}, "GET", {}, {}, {}, nullptr, {}, context); ASSERT_EQ(response.GetStatus(), static_cast(olp::http::ErrorCode::CANCELLED_ERROR)); } TEST_P(OlpClientTest, CancelAfterCompletion) { - client_.SetBaseUrl("https://www.google.com"); auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, "https://www.google.com"); std::future future; olp::http::RequestId request_id = 5; @@ -1248,10 +1288,10 @@ TEST_P(OlpClientTest, CancelAfterCompletion) { std::promise promise; auto cancel_token = - client_.CallApi({}, "GET", {}, {}, {}, nullptr, {}, - [&](olp::client::HttpResponse http_response) { - promise.set_value(std::move(http_response)); - }); + client.CallApi({}, "GET", {}, {}, {}, nullptr, {}, + [&](olp::client::HttpResponse http_response) { + promise.set_value(std::move(http_response)); + }); auto response = promise.get_future().get(); ASSERT_EQ(http::HttpStatusCode::OK, response.GetStatus()); @@ -1291,10 +1331,10 @@ TEST_P(OlpClientTest, CancelAfterCompletion) { std::promise promise; auto cancel_token = - client_.CallApi({}, "GET", {}, {}, {}, content, {}, - [&](olp::client::HttpResponse http_response) { - promise.set_value(std::move(http_response)); - }); + client.CallApi({}, "GET", {}, {}, {}, content, {}, + [&](olp::client::HttpResponse http_response) { + promise.set_value(std::move(http_response)); + }); auto response = promise.get_future().get(); ASSERT_EQ(http::HttpStatusCode::OK, response.GetStatus()); @@ -1310,9 +1350,9 @@ TEST_P(OlpClientTest, CancelAfterCompletion) { // Test only make sense for async api, as CancellationContext guards for // double cancellation. TEST_P(OlpClientTest, CancelDuplicate) { - client_.SetBaseUrl("https://www.google.com"); auto network = network_; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, "https://www.google.com"); std::future future; olp::http::RequestId request_id = 5; @@ -1351,7 +1391,7 @@ TEST_P(OlpClientTest, CancelDuplicate) { }; auto cancel_token = - client_.CallApi({}, "GET", {}, {}, {}, nullptr, {}, callback); + client.CallApi({}, "GET", {}, {}, {}, nullptr, {}, callback); // Cancel multiple times cancel_token.Cancel(); @@ -1406,7 +1446,7 @@ TEST_P(OlpClientTest, CancelDuplicate) { content_string.begin(), content_string.end()); auto cancel_token = - client_.CallApi({}, "GET", {}, {}, {}, content, {}, callback); + client.CallApi({}, "GET", {}, {}, {}, content, {}, callback); // Cancel multiple times cancel_token.Cancel(); @@ -1432,7 +1472,8 @@ TEST_P(OlpClientTest, CancelRetry) { ([](const olp::client::HttpResponse& response) { return response.GetStatus() == http::HttpStatusCode::TOO_MANY_REQUESTS; }); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -1475,8 +1516,9 @@ TEST_P(OlpClientTest, CancelRetry) { olp::client::CancellationContext context; + auto call_wrapper = MakeCallWrapper(client); auto response = std::async(std::launch::async, [&]() { - return call_wrapper_->CallApi({}, {}, {}, {}, {}, nullptr, {}, context); + return call_wrapper->CallApi({}, {}, {}, {}, {}, nullptr, {}, context); }); cancel_wait->get_future().get(); @@ -1535,8 +1577,9 @@ TEST_P(OlpClientTest, CancelRetry) { const auto content = std::make_shared>( content_string.begin(), content_string.end()); + auto call_wrapper = MakeCallWrapper(client); auto response = std::async(std::launch::async, [&]() { - return call_wrapper_->CallApi({}, {}, {}, {}, {}, content, {}, context); + return call_wrapper->CallApi({}, {}, {}, {}, {}, content, {}, context); }); cancel_wait->get_future().get(); @@ -1558,7 +1601,8 @@ TEST_P(OlpClientTest, CancelRetry) { TEST_P(OlpClientTest, SlowDownError) { auto network = network_; client_settings_.retry_settings.max_attempts = 0; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); constexpr int kExpectedError = static_cast(http::ErrorCode::NETWORK_OVERLOAD_ERROR); @@ -1574,7 +1618,8 @@ TEST_P(OlpClientTest, SlowDownError) { http::ErrorCode::NETWORK_OVERLOAD_ERROR); }); - auto response = call_wrapper_->CallApi({}, {}, {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, {}, {}, {}, {}, nullptr, {}); EXPECT_EQ(kExpectedError, response.GetStatus()); @@ -1596,7 +1641,8 @@ TEST_P(OlpClientTest, SlowDownError) { const auto content = std::make_shared>( content_string.begin(), content_string.end()); - auto response = call_wrapper_->CallApi({}, {}, {}, {}, {}, content, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi({}, {}, {}, {}, {}, content, {}); EXPECT_EQ(kExpectedError, response.GetStatus()); @@ -1622,7 +1668,8 @@ TEST_P(OlpClientTest, ApiKey) { auto network = network_; client_settings_.authentication_settings = authentication_settings; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); std::future future; olp::http::RequestId request_id = 5; @@ -1646,8 +1693,9 @@ TEST_P(OlpClientTest, ApiKey) { return olp::http::SendOutcome(current_request_id); }); - auto response = call_wrapper_->CallApi("https://here.com", "GET", {}, {}, {}, - nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = + call_wrapper->CallApi("https://here.com", "GET", {}, {}, {}, nullptr, {}); future.wait(); testing::Mock::VerifyAndClearExpectations(network.get()); @@ -1664,12 +1712,14 @@ TEST_P(OlpClientTest, EmptyBearerToken) { auto network = network_; client_settings_.authentication_settings = authentication_settings; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); EXPECT_CALL(*network, Send(_, _, _, _, _)).Times(0); - auto response = call_wrapper_->CallApi("https://here.com", "GET", {}, {}, {}, - nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = + call_wrapper->CallApi("https://here.com", "GET", {}, {}, {}, nullptr, {}); EXPECT_EQ(response.GetStatus(), static_cast(http::ErrorCode::AUTHORIZATION_ERROR)); @@ -1689,12 +1739,14 @@ TEST_P(OlpClientTest, ErrorOnTokenRequest) { auto network = network_; client_settings_.authentication_settings = authentication_settings; - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, kEmptyBaseUrl); EXPECT_CALL(*network, Send(_, _, _, _, _)).Times(0); - auto response = call_wrapper_->CallApi("https://here.com", "GET", {}, {}, {}, - nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = + call_wrapper->CallApi("https://here.com", "GET", {}, {}, {}, nullptr, {}); EXPECT_EQ(response.GetStatus(), static_cast(http::ErrorCode::NETWORK_OVERLOAD_ERROR)); @@ -1710,6 +1762,9 @@ class OlpClientMergeTest : public ::testing::Test { void SetUp() override { network_ = std::make_shared>(); settings_.network_request_handler = network_; + client_ = olp::client::OlpClient(settings_, + "https://api.platform.here.com/query/v1/" + "catalogs/hrn:here:data:::dummy"); } olp::client::OlpClientSettings settings_; @@ -1719,10 +1774,7 @@ class OlpClientMergeTest : public ::testing::Test { TEST_F(OlpClientMergeTest, MergeMultipleCallbacks) { const std::string path = "/layers/xyz/versions/1/quadkeys/23618402/depths/4"; - client_.SetBaseUrl( - "https://api.platform.here.com/query/v1/catalogs/hrn:here:data:::dummy"); auto network = network_; - client_.SetSettings(settings_); constexpr size_t kExpectedCallbacks = 3u; std::future future; @@ -1902,10 +1954,7 @@ TEST_F(OlpClientMergeTest, MergeMultipleCallbacks) { TEST_F(OlpClientMergeTest, NoMergeMultipleCallbacks) { const std::string path = "/layers/xyz/versions/1/quadkeys/23618402/depths/4"; - client_.SetBaseUrl( - "https://api.platform.here.com/query/v1/catalogs/hrn:here:data:::dummy"); auto network = network_; - client_.SetSettings(settings_); constexpr size_t kExpectedCallbacks = 3u; std::vector> futures; @@ -2120,10 +2169,10 @@ TEST_P(OlpClientTest, UrlWithoutProtocol) { { SCOPED_TRACE("Url without protocol"); - client_.SetBaseUrl("here.com"); - client_.SetSettings(client_settings_); + olp::client::OlpClient client(client_settings_, "here.com"); - auto response = call_wrapper_->CallApi("", "GET", {}, {}, {}, nullptr, {}); + auto call_wrapper = MakeCallWrapper(client); + auto response = call_wrapper->CallApi("", "GET", {}, {}, {}, nullptr, {}); EXPECT_EQ(response.GetStatus(), static_cast(http::ErrorCode::INVALID_URL_ERROR)); } diff --git a/olp-cpp-sdk-dataservice-read/src/ApiClientLookup.cpp b/olp-cpp-sdk-dataservice-read/src/ApiClientLookup.cpp index 8ca2069ae..ebd947eae 100644 --- a/olp-cpp-sdk-dataservice-read/src/ApiClientLookup.cpp +++ b/olp-cpp-sdk-dataservice-read/src/ApiClientLookup.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2022 HERE Europe B.V. + * Copyright (C) 2019-2024 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. @@ -77,9 +77,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi( OLP_SDK_LOG_DEBUG_F(kLogTag, "LookupApi(%s/%s) found in cache, hrn='%s'", service.c_str(), service_version.c_str(), hrn.c_str()); - client::OlpClient client; - client.SetSettings(std::move(settings)); - client.SetBaseUrl(*url); + client::OlpClient client(settings, *url); return client; } else if (options == CacheOnly) { return {{client::ErrorCode::NotFound, @@ -92,10 +90,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi( service.c_str(), service_version.c_str(), hrn.c_str()); const auto& base_url = GetDatastoreServerUrl(catalog.GetPartition()); - client::OlpClient client; - client.SetBaseUrl(base_url); - // Do not move settings, we still need them later on! - client.SetSettings(settings); + client::OlpClient client(settings, base_url); PlatformApi::ApisResponse api_response; if (service == "config") { @@ -142,9 +137,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi( service.c_str(), service_version.c_str(), hrn.c_str(), service_url.c_str()); - client::OlpClient service_client; - service_client.SetBaseUrl(service_url); - service_client.SetSettings(settings); + client::OlpClient service_client(settings, service_url); return service_client; } } // namespace read diff --git a/olp-cpp-sdk-dataservice-read/src/StreamLayerClientImpl.cpp b/olp-cpp-sdk-dataservice-read/src/StreamLayerClientImpl.cpp index 4b4fd3168..84dcca0bc 100644 --- a/olp-cpp-sdk-dataservice-read/src/StreamLayerClientImpl.cpp +++ b/olp-cpp-sdk-dataservice-read/src/StreamLayerClientImpl.cpp @@ -133,11 +133,8 @@ client::CancellationToken StreamLayerClientImpl::Subscribe( client_context_ = std::make_unique( subscripton_id, subscription_mode, correlation_id, - std::make_shared()); - - client_context_->client->SetBaseUrl( - subscription.GetResult().GetNodeBaseURL()); - client_context_->client->SetSettings(settings_); + std::make_shared( + settings_, subscription.GetResult().GetNodeBaseURL())); } OLP_SDK_LOG_INFO_F(kLogTag, diff --git a/olp-cpp-sdk-dataservice-read/tests/StreamApiTest.cpp b/olp-cpp-sdk-dataservice-read/tests/StreamApiTest.cpp index 2ce9045aa..8328f46b9 100644 --- a/olp-cpp-sdk-dataservice-read/tests/StreamApiTest.cpp +++ b/olp-cpp-sdk-dataservice-read/tests/StreamApiTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 HERE Europe B.V. + * Copyright (C) 2020-2024 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. @@ -53,25 +53,6 @@ model::StreamOffsets GetStreamOffsets() { return offsets; } -class StreamApiTest : public testing::Test { - protected: - void SetUp() override { - network_mock_ = std::make_shared(); - - client::OlpClientSettings settings; - settings.network_request_handler = network_mock_; - settings.task_scheduler = - client::OlpClientSettingsFactory::CreateDefaultTaskScheduler(1); - olp_client_.SetSettings(std::move(settings)); - } - - void TearDown() override { network_mock_.reset(); } - - protected: - client::OlpClient olp_client_; - std::shared_ptr network_mock_; -}; - const std::string kBaseUrl{ "https://some.base.url/stream/v2/catalogs/" "hrn:here:data::olp-here-test:hereos-internal-test-v2"}; @@ -141,6 +122,25 @@ constexpr auto kHttpRequestBodyWithConsumerProperties = constexpr auto kHttpRequestBodyWithStreamOffsets = R"jsonString({"offsets":[{"partition":7,"offset":38562},{"partition":8,"offset":27458}]})jsonString"; +class StreamApiTest : public testing::Test { + protected: + void SetUp() override { + network_mock_ = std::make_shared(); + + client::OlpClientSettings settings; + settings.network_request_handler = network_mock_; + settings.task_scheduler = + client::OlpClientSettingsFactory::CreateDefaultTaskScheduler(1); + olp_client_ = client::OlpClient(settings, kNodeBaseUrl); + } + + void TearDown() override { network_mock_.reset(); } + + protected: + client::OlpClient olp_client_; + std::shared_ptr network_mock_; +}; + TEST_F(StreamApiTest, Subscribe) { { SCOPED_TRACE("Subscribe without optional input fields succeeds"); @@ -237,7 +237,6 @@ TEST_F(StreamApiTest, ConsumeData) { http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), kHttpResponseConsumeDataSucceeds, {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto consume_data_response = @@ -262,7 +261,6 @@ TEST_F(StreamApiTest, ConsumeData) { http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), kHttpResponseConsumeDataSucceeds, {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto consume_data_response = @@ -287,7 +285,6 @@ TEST_F(StreamApiTest, ConsumeData) { http::NetworkResponse().WithStatus(http::HttpStatusCode::NOT_FOUND), kHttpResponseConsumeDataFails)); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto consume_data_response = @@ -321,7 +318,6 @@ TEST_F(StreamApiTest, CommitOffsets) { http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), "", {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto commit_offsets_response = read::StreamApi::CommitOffsets( @@ -347,7 +343,6 @@ TEST_F(StreamApiTest, CommitOffsets) { http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), "", {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto commit_offsets_response = read::StreamApi::CommitOffsets( @@ -373,7 +368,6 @@ TEST_F(StreamApiTest, CommitOffsets) { http::NetworkResponse().WithStatus(http::HttpStatusCode::CONFLICT), kHttpResponseCommitOffsetsFails, {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto commit_offsets_response = read::StreamApi::CommitOffsets( @@ -406,7 +400,6 @@ TEST_F(StreamApiTest, SeekToOffset) { http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), "", {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto seek_to_offset_response = read::StreamApi::SeekToOffset( @@ -432,7 +425,6 @@ TEST_F(StreamApiTest, SeekToOffset) { http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), "", {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto commit_offsets_response = read::StreamApi::SeekToOffset( @@ -459,7 +451,6 @@ TEST_F(StreamApiTest, SeekToOffset) { kHttpResponseSeekToOffsetFails, {kCorrelationIdHeader})); - olp_client_.SetBaseUrl(kNodeBaseUrl); std::string x_correlation_id = kCorrelationId; client::CancellationContext context; const auto commit_offsets_response = read::StreamApi::SeekToOffset( @@ -487,7 +478,6 @@ TEST_F(StreamApiTest, DeleteSubscription) { .WillOnce(ReturnHttpResponse( http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), "")); - olp_client_.SetBaseUrl(kNodeBaseUrl); client::CancellationContext context; const auto unsubscribe_response = read::StreamApi::DeleteSubscription( olp_client_, kLayerId, kSubscriptionId, kParallelMode, kCorrelationId, @@ -510,7 +500,6 @@ TEST_F(StreamApiTest, DeleteSubscription) { http::HttpStatusCode::UNAUTHORIZED), kHttpResponseUnsubscribeFails)); - olp_client_.SetBaseUrl(kNodeBaseUrl); client::CancellationContext context; const auto unsubscribe_response = read::StreamApi::DeleteSubscription( olp_client_, kLayerId, kSubscriptionId, kParallelMode, kCorrelationId, diff --git a/olp-cpp-sdk-dataservice-write/src/ApiClientLookup.cpp b/olp-cpp-sdk-dataservice-write/src/ApiClientLookup.cpp index be032afd4..d31c1c7f1 100644 --- a/olp-cpp-sdk-dataservice-write/src/ApiClientLookup.cpp +++ b/olp-cpp-sdk-dataservice-write/src/ApiClientLookup.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2021 HERE Europe B.V. + * Copyright (C) 2019-2024 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. @@ -152,9 +152,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApiClient( auto base_url = boost::any_cast(url); OLP_SDK_LOG_INFO_F(kLogTag, "LookupApiClient(%s, %s) -> from cache", service.c_str(), service_version.c_str()); - client::OlpClient client; - client.SetSettings(settings); - client.SetBaseUrl(base_url); + client::OlpClient client(settings, base_url); return ApiClientResponse{client}; } } @@ -174,9 +172,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApiClient( } ApisResponse api_response; - client::OlpClient input_client; - input_client.SetBaseUrl(base_url); - input_client.SetSettings(settings); + client::OlpClient input_client(settings, base_url); if (service == "config") { OLP_SDK_LOG_INFO_F(kLogTag, "LookupApiClient(%s/%s): %s - config service", @@ -219,9 +215,6 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApiClient( service_version.c_str(), catalog.GetPartition().c_str()); } - client::OlpClient output_client; - output_client.SetBaseUrl(output_base_url); - if (cache) { constexpr time_t kExpiryTimeInSecs = 3600; if (cache->Put(cache_key, output_base_url, @@ -234,7 +227,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApiClient( } } - output_client.SetSettings(settings); + client::OlpClient output_client(settings, output_base_url); return ApiClientResponse{std::move(output_client)}; } diff --git a/tests/functional/olp-cpp-sdk-core/OlpClientDefaultAsyncHttpTest.cpp b/tests/functional/olp-cpp-sdk-core/OlpClientDefaultAsyncHttpTest.cpp index 05ff4d65a..17ced04cc 100644 --- a/tests/functional/olp-cpp-sdk-core/OlpClientDefaultAsyncHttpTest.cpp +++ b/tests/functional/olp-cpp-sdk-core/OlpClientDefaultAsyncHttpTest.cpp @@ -33,15 +33,13 @@ namespace http = olp::http; class OlpClientDefaultAsyncHttpTest : public ::testing::Test { protected: olp::client::OlpClientSettings client_settings_; - olp::client::OlpClient client_; }; TEST_F(OlpClientDefaultAsyncHttpTest, GetGoogleWebsite) { - client_.SetBaseUrl("https://www.google.com"); - client_settings_.network_request_handler = olp::client:: OlpClientSettingsFactory::CreateDefaultNetworkRequestHandler(); - client_.SetSettings(client_settings_); + + olp::client::OlpClient client(client_settings_, "https://www.google.com"); std::promise p; olp::client::NetworkAsyncCallback callback = @@ -49,11 +47,11 @@ TEST_F(OlpClientDefaultAsyncHttpTest, GetGoogleWebsite) { p.set_value(std::move(response)); }; - auto cancel_token = client_.CallApi(std::string(), std::string(), - std::multimap(), - std::multimap(), - std::multimap(), - nullptr, std::string(), callback); + auto cancel_token = client.CallApi(std::string(), std::string(), + std::multimap(), + std::multimap(), + std::multimap(), + nullptr, std::string(), callback); auto response = p.get_future().get(); ASSERT_EQ(http::HttpStatusCode::OK, response.GetStatus()); @@ -61,8 +59,6 @@ TEST_F(OlpClientDefaultAsyncHttpTest, GetGoogleWebsite) { } TEST_F(OlpClientDefaultAsyncHttpTest, GetNonExistentWebsite) { - // RFC 2606. Use reserved domain name that nobody could register. - client_.SetBaseUrl("https://example.test"); std::promise p; olp::client::NetworkAsyncCallback callback = [&p](olp::client::HttpResponse response) { @@ -71,13 +67,15 @@ TEST_F(OlpClientDefaultAsyncHttpTest, GetNonExistentWebsite) { client_settings_.network_request_handler = olp::client:: OlpClientSettingsFactory::CreateDefaultNetworkRequestHandler(); - client_.SetSettings(client_settings_); - auto cancel_token = client_.CallApi(std::string(), std::string(), - std::multimap(), - std::multimap(), - std::multimap(), - nullptr, std::string(), callback); + // RFC 2606. Use reserved domain name that nobody could register. + olp::client::OlpClient client(client_settings_, "https://example.test"); + + auto cancel_token = client.CallApi(std::string(), std::string(), + std::multimap(), + std::multimap(), + std::multimap(), + nullptr, std::string(), callback); auto response = p.get_future().get(); ASSERT_EQ(olp::http::ErrorCode::INVALID_URL_ERROR, static_cast(response.GetStatus()));