Skip to content

Commit 374f69a

Browse files
Fix client capturing in ApiClientLookup
Relates-To: OLPEDGE-1091 Signed-off-by: Mykhailo Kuchma <[email protected]>
1 parent 29e2234 commit 374f69a

File tree

5 files changed

+125
-45
lines changed

5 files changed

+125
-45
lines changed

olp-cpp-sdk-dataservice-read/src/ApiClientLookup.cpp

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi(
139139
if (options != OnlineOnly) {
140140
auto url = repository.Get(service, service_version);
141141
if (url) {
142-
OLP_SDK_LOG_INFO_F(kLogTag, "getApiClient(%s, %s) -> from cache",
142+
OLP_SDK_LOG_INFO_F(kLogTag, "LookupApi(%s, %s) -> from cache",
143143
service.c_str(), service_version.c_str());
144144
client::OlpClient client;
145145
client.SetSettings(std::move(settings));
@@ -154,28 +154,36 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi(
154154

155155
client::Condition condition{};
156156

157-
auto client = std::make_shared<client::OlpClient>();
158-
client->SetSettings(std::move(settings));
159-
160157
// when the network operation took too much time we cancel it and exit
161158
// execution, to make sure that network callback will not access dangling
162159
// references we protect them with atomic bool flag.
163160
auto flag = std::make_shared<std::atomic_bool>(true);
164161

165-
ApiClientLookup::ApiClientResponse api_response;
166-
auto api_client_callback =
167-
[&, flag](ApiClientLookup::ApiClientResponse response) {
168-
if (flag->exchange(false)) {
169-
api_response = std::move(response);
170-
condition.Notify();
171-
}
172-
};
162+
PlatformApi::ApisResponse api_response;
163+
auto api_callback = [&, flag](PlatformApi::ApisResponse response) {
164+
if (flag->exchange(false)) {
165+
api_response = std::move(response);
166+
condition.Notify();
167+
}
168+
};
173169

174170
cancellation_context.ExecuteOrCancelled(
175171
[&, flag]() {
176-
auto token = ApiClientLookup::LookupApiClient(
177-
client, service, service_version, catalog,
178-
std::move(api_client_callback));
172+
const auto& base_url = GetDatastoreServerUrl(catalog.partition);
173+
174+
client::OlpClient client;
175+
client.SetBaseUrl(base_url);
176+
client.SetSettings(std::move(settings));
177+
178+
client::CancellationToken token;
179+
if (service == "config") {
180+
token = PlatformApi::GetApis(client, service, service_version,
181+
api_callback);
182+
} else {
183+
token = ResourcesApi::GetApis(client, catalog.ToCatalogHRNString(),
184+
service, service_version, api_callback);
185+
}
186+
179187
return client::CancellationToken([&, token, flag]() {
180188
if (flag->exchange(false)) {
181189
token.cancel();
@@ -204,14 +212,38 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi(
204212
"Operation cancelled.");
205213
}
206214

207-
if (api_response.IsSuccessful()) {
208-
OLP_SDK_LOG_INFO_F(kLogTag, "getApiClient(%s, %s) -> into cache",
209-
service.c_str(), service_version.c_str());
210-
repository.Put(service, service_version,
211-
api_response.GetResult().GetBaseUrl());
215+
if (!api_response.IsSuccessful()) {
216+
OLP_SDK_LOG_INFO_F(kLogTag, "LookupApi(%s/%s): %s - unsuccessful: %s",
217+
service.c_str(), service_version.c_str(),
218+
catalog.partition.c_str(),
219+
api_response.GetError().GetMessage().c_str());
220+
return api_response.GetError();
212221
}
213222

214-
return api_response;
223+
const auto& api_result = api_response.GetResult();
224+
225+
if (api_result.size() < 1) {
226+
OLP_SDK_LOG_INFO_F(kLogTag, "LookupApi(%s/%s): %s - service not available",
227+
service.c_str(), service_version.c_str(),
228+
catalog.partition.c_str());
229+
230+
return client::ApiError(client::ErrorCode::ServiceUnavailable,
231+
"Service/Version not available for given HRN");
232+
}
233+
234+
const auto& base_url = api_result.at(0).GetBaseUrl();
235+
236+
repository.Put(service, service_version, base_url);
237+
238+
OLP_SDK_LOG_INFO_F(kLogTag, "LookupApi(%s/%s): %s - OK, base_url=%s",
239+
service.c_str(), service_version.c_str(),
240+
catalog.partition.c_str(), base_url.c_str());
241+
242+
client::OlpClient client;
243+
client.SetBaseUrl(base_url);
244+
client.SetSettings(std::move(settings));
245+
246+
return std::move(client);
215247
}
216248

217249
} // namespace read

olp-cpp-sdk-dataservice-read/src/generated/api/PlatformApi.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,37 @@ namespace read {
3636

3737
client::CancellationToken PlatformApi::GetApis(
3838
std::shared_ptr<client::OlpClient> client, const std::string& service,
39-
const std::string& serviceVersion, const ApisCallback& apisCallback) {
40-
std::multimap<std::string, std::string> headerParams;
41-
headerParams.insert(std::make_pair("Accept", "application/json"));
42-
std::multimap<std::string, std::string> queryParams;
43-
std::multimap<std::string, std::string> formParams;
39+
const std::string& serviceVersion, const ApisCallback& callback) {
40+
return GetApis(*client, service, serviceVersion, callback);
41+
}
42+
43+
client::CancellationToken PlatformApi::GetApis(
44+
const client::OlpClient& client, const std::string& service,
45+
const std::string& service_version, const ApisCallback& callback) {
46+
std::multimap<std::string, std::string> header_params;
47+
header_params.insert(std::make_pair("Accept", "application/json"));
48+
std::multimap<std::string, std::string> query_params;
49+
std::multimap<std::string, std::string> form_params;
4450

45-
std::string platformUrl = "/platform/apis/" + service + "/" + serviceVersion;
51+
std::string platform_url =
52+
"/platform/apis/" + service + "/" + service_version;
4653

47-
client::NetworkAsyncCallback callback =
48-
[apisCallback](client::HttpResponse response) {
54+
client::NetworkAsyncCallback network_callback =
55+
[callback](client::HttpResponse response) {
4956
if (response.status != 200) {
50-
apisCallback(ApisResponse(
57+
callback(ApisResponse(
5158
client::ApiError(response.status, response.response.str())));
5259
} else {
5360
// parse the services
5461
// TODO catch any exception and return as Error
55-
apisCallback(
62+
callback(
5663
ApisResponse(parser::parse<olp::dataservice::read::model::Apis>(
5764
response.response)));
5865
}
5966
};
6067

61-
return client->CallApi(platformUrl, "GET", queryParams, headerParams,
62-
formParams, nullptr, "", callback);
68+
return client.CallApi(platform_url, "GET", query_params, header_params,
69+
form_params, nullptr, "", network_callback);
6370
}
6471

6572
} // namespace read

olp-cpp-sdk-dataservice-read/src/generated/api/PlatformApi.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class PlatformApi {
4242
using ApisCallback = std::function<void(ApisResponse)>;
4343

4444
/**
45+
* @deprecated
4546
* @brief Call to lookup platform base urls.
4647
* @param client Instance of OlpClient used to make REST request.
4748
* @param service Name of the service.
@@ -54,6 +55,21 @@ class PlatformApi {
5455
static client::CancellationToken GetApis(
5556
std::shared_ptr<client::OlpClient> client, const std::string& service,
5657
const std::string& serviceVersion, const ApisCallback& callback);
58+
59+
/**
60+
* @brief Call to lookup platform base urls.
61+
* @param client Instance of OlpClient used to make REST request.
62+
* @param service Name of the service.
63+
* @param service_version Version of the service.
64+
* @param A callback function to invoke with the collection of Api services
65+
* that match the parameters.
66+
*
67+
* @return The cancellation token.
68+
*/
69+
static client::CancellationToken GetApis(const client::OlpClient& client,
70+
const std::string& service,
71+
const std::string& service_version,
72+
const ApisCallback& callback);
5773
};
5874

5975
} // namespace read

olp-cpp-sdk-dataservice-read/src/generated/api/ResourcesApi.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,38 @@ namespace read {
3636
client::CancellationToken ResourcesApi::GetApis(
3737
std::shared_ptr<client::OlpClient> client, const std::string& hrn,
3838
const std::string& service, const std::string& service_version,
39-
const ApisCallback& apisCallback) {
40-
std::multimap<std::string, std::string> headerParams;
41-
headerParams.insert(std::make_pair("Accept", "application/json"));
42-
std::multimap<std::string, std::string> queryParams;
43-
std::multimap<std::string, std::string> formParams;
39+
const ApisCallback& callback) {
40+
return GetApis(*client, hrn, service, service_version, callback);
41+
}
42+
43+
client::CancellationToken ResourcesApi::GetApis(
44+
const client::OlpClient& client, const std::string& hrn,
45+
const std::string& service, const std::string& service_version,
46+
const ApisCallback& callback) {
47+
std::multimap<std::string, std::string> header_params;
48+
header_params.insert(std::make_pair("Accept", "application/json"));
49+
std::multimap<std::string, std::string> query_params;
50+
std::multimap<std::string, std::string> form_params;
4451

4552
// scan apis at resource endpoint
46-
std::string resourceUrl =
53+
std::string resource_url =
4754
"/resources/" + hrn + "/apis/" + service + "/" + service_version;
4855

49-
client::NetworkAsyncCallback callback =
50-
[apisCallback](client::HttpResponse response) {
56+
client::NetworkAsyncCallback network_callback =
57+
[callback](client::HttpResponse response) {
5158
if (response.status != 200) {
52-
apisCallback(ApisResponse(
59+
callback(ApisResponse(
5360
client::ApiError(response.status, response.response.str())));
5461
} else {
5562
// parse the services
5663
// TODO catch any exception and return as Error
57-
apisCallback(
64+
callback(
5865
ApisResponse(parser::parse<olp::dataservice::read::model::Apis>(
5966
response.response)));
6067
}
6168
};
62-
return client->CallApi(resourceUrl, "GET", queryParams, headerParams,
63-
formParams, nullptr, "", callback);
69+
return client.CallApi(resource_url, "GET", query_params, header_params,
70+
form_params, nullptr, "", network_callback);
6471
}
6572

6673
} // namespace read

olp-cpp-sdk-dataservice-read/src/generated/api/ResourcesApi.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ResourcesApi {
4242
using ApisCallback = std::function<void(ApisResponse)>;
4343

4444
/**
45+
* @deprecated
4546
* @brief Call to resources service base urls.
4647
* @param client Instance of OlpClient used to make REST request.
4748
* @param service Name of the service.
@@ -56,6 +57,23 @@ class ResourcesApi {
5657
std::shared_ptr<client::OlpClient> client, const std::string& hrn,
5758
const std::string& service, const std::string& service_version,
5859
const ApisCallback& callback);
60+
61+
/**
62+
* @brief Call to resources service base urls.
63+
* @param client Instance of OlpClient used to make REST request.
64+
* @param service Name of the service.
65+
* @param service_version Version of the service.
66+
* @param hrn Full catalog name.
67+
* @param A callback function to invoke with the collection of Api services
68+
* that match the parameters.
69+
*
70+
* @return The cancellation token.
71+
*/
72+
static client::CancellationToken GetApis(const client::OlpClient& client,
73+
const std::string& hrn,
74+
const std::string& service,
75+
const std::string& service_version,
76+
const ApisCallback& callback);
5977
};
6078

6179
} // namespace read

0 commit comments

Comments
 (0)