3535#include " olp/dataservice/read/VersionsRequest.h"
3636
3737namespace {
38+ constexpr auto kDefaultStartVersion = 0 ;
3839constexpr auto kLogTag = " CatalogRepository" ;
40+
41+ // The function retrieves the maximum version among cached/online/user-provided
42+ // versions.
43+ // Update the cache, if the retrieved version is greater than the cached one.
44+ olp::dataservice::read::CatalogVersionResponse RetrieveLatestVersion (
45+ const olp::client::HRN& catalog,
46+ const olp::dataservice::read::CatalogVersionRequest& request,
47+ olp::dataservice::read::repository::CatalogCacheRepository& repository,
48+ olp::dataservice::read::CatalogVersionResponse&& version_response) {
49+ auto cached_version = repository.GetVersion ();
50+ auto fetch_option = request.GetFetchOption ();
51+
52+ // Using `GetStartVersion` to set up a new latest version for CacheOnly
53+ // requests in case of absence of the previous latest version or it's less
54+ // than the new user set version.
55+ if (fetch_option == olp::dataservice::read::FetchOptions::CacheOnly) {
56+ const auto start_version = request.GetStartVersion ();
57+ if (start_version >= kDefaultStartVersion &&
58+ (!cached_version || start_version > cached_version->GetVersion ())) {
59+ olp::dataservice::read::model::VersionResponse new_response;
60+ new_response.SetVersion (start_version);
61+ version_response = std::move (new_response);
62+ }
63+ }
64+
65+ if (version_response) {
66+ const auto new_version = version_response.GetResult ().GetVersion ();
67+
68+ // Write or update a version in the cache, updating happens only when the
69+ // new version is greater than cached.
70+ if (!cached_version || cached_version->GetVersion () < new_version) {
71+ repository.PutVersion (version_response.GetResult ());
72+ if (fetch_option == olp::dataservice::read::FetchOptions::CacheOnly) {
73+ OLP_SDK_LOG_DEBUG_F (
74+ kLogTag , " Latest user set version, hrn='%s', version=%" PRId64,
75+ catalog.ToCatalogHRNString ().c_str (), new_version);
76+ } else {
77+ OLP_SDK_LOG_DEBUG_F (kLogTag ,
78+ " Latest online version, hrn='%s', version=%" PRId64,
79+ catalog.ToCatalogHRNString ().c_str (), new_version);
80+ }
81+ }
82+
83+ return std::move (version_response);
84+ }
85+
86+ if (cached_version) {
87+ OLP_SDK_LOG_DEBUG_F (
88+ kLogTag , " Latest cached version, hrn='%s', version=%" PRId64,
89+ catalog.ToCatalogHRNString ().c_str (), cached_version->GetVersion ());
90+ version_response = std::move (*cached_version);
91+ }
92+
93+ return std::move (version_response);
94+ }
95+
3996} // namespace
4097
4198namespace olp {
@@ -56,8 +113,8 @@ CatalogResponse CatalogRepository::GetCatalog(
56113 const auto fetch_options = request.GetFetchOption ();
57114 const auto catalog_str = catalog_.ToCatalogHRNString ();
58115
59- repository:: CatalogCacheRepository repository (
60- catalog_, settings_. cache , settings_.default_cache_expiration );
116+ CatalogCacheRepository repository (catalog_, settings_. cache ,
117+ settings_.default_cache_expiration );
61118
62119 if (fetch_options != OnlineOnly && fetch_options != CacheWithUpdate) {
63120 auto cached = repository.Get ();
@@ -80,18 +137,19 @@ CatalogResponse CatalogRepository::GetCatalog(
80137 " config" , " v1" , static_cast <client::FetchOptions>(fetch_options),
81138 context);
82139
83- if (!config_api. IsSuccessful () ) {
140+ if (!config_api) {
84141 return config_api.GetError ();
85142 }
86143
87144 const client::OlpClient& config_client = config_api.GetResult ();
88145 auto catalog_response = ConfigApi::GetCatalog (
89146 config_client, catalog_str, request.GetBillingTag (), context);
90147
91- if (catalog_response. IsSuccessful () && fetch_options != OnlineOnly) {
148+ if (catalog_response && fetch_options != OnlineOnly) {
92149 repository.Put (catalog_response.GetResult ());
93150 }
94- if (!catalog_response.IsSuccessful ()) {
151+
152+ if (!catalog_response) {
95153 const auto & error = catalog_response.GetError ();
96154 if (error.GetHttpStatusCode () == http::HttpStatusCode::FORBIDDEN) {
97155 OLP_SDK_LOG_WARNING_F (kLogTag ,
@@ -107,12 +165,12 @@ CatalogResponse CatalogRepository::GetCatalog(
107165
108166CatalogVersionResponse CatalogRepository::GetLatestVersion (
109167 const CatalogVersionRequest& request, client::CancellationContext context) {
110- repository:: CatalogCacheRepository repository (
111- catalog_, settings_. cache , settings_.default_cache_expiration );
168+ CatalogCacheRepository repository (catalog_, settings_. cache ,
169+ settings_.default_cache_expiration );
112170
113171 const auto fetch_option = request.GetFetchOption ();
114- // in case get version online was never called and version was not found in
115- // cache
172+ // In case `GetVersionOnline` was never called and version was not found in
173+ // the cache
116174 CatalogVersionResponse version_response = {
117175 {client::ErrorCode::NotFound, " Failed to find version." }};
118176
@@ -124,7 +182,7 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
124182 return version_response;
125183 }
126184
127- if (!version_response. IsSuccessful () ) {
185+ if (!version_response) {
128186 const auto & error = version_response.GetError ();
129187 if (error.GetHttpStatusCode () == http::HttpStatusCode::FORBIDDEN) {
130188 OLP_SDK_LOG_WARNING_F (
@@ -143,8 +201,6 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
143201 // requests in case of absence of previous latest version or it less than the
144202 // new user set version
145203 if (fetch_option == CacheOnly) {
146- constexpr auto kDefaultStartVersion = 0 ;
147-
148204 auto user_set_version = request.GetStartVersion ();
149205 if (user_set_version >= kDefaultStartVersion ) {
150206 if (!cached_version || user_set_version > cached_version->GetVersion ()) {
@@ -155,11 +211,11 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
155211 }
156212 }
157213
158- if (version_response. IsSuccessful () ) {
214+ if (version_response) {
159215 const auto new_version = version_response.GetResult ().GetVersion ();
160216 // Write or update the version in cache, updating happens only when the new
161217 // version is greater than cached.
162- if (!cached_version || (* cached_version). GetVersion () < new_version) {
218+ if (!cached_version || cached_version-> GetVersion () < new_version) {
163219 repository.PutVersion (version_response.GetResult ());
164220 if (fetch_option == CacheOnly) {
165221 OLP_SDK_LOG_DEBUG_F (
@@ -171,25 +227,69 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
171227 catalog_.ToCatalogHRNString ().c_str (), new_version);
172228 }
173229 }
230+
174231 return version_response;
175232 }
176233
177234 if (cached_version) {
178235 OLP_SDK_LOG_DEBUG_F (
179236 kLogTag , " Latest cached version, hrn='%s', version=%" PRId64,
180- catalog_.ToCatalogHRNString ().c_str (), (* cached_version). GetVersion ());
181- version_response = * std::move (cached_version);
237+ catalog_.ToCatalogHRNString ().c_str (), cached_version-> GetVersion ());
238+ version_response = std::move (* cached_version);
182239 }
183240
184241 return version_response;
185242}
186243
244+ client::CancellationToken CatalogRepository::GetLatestVersion (
245+ const CatalogVersionRequest& request, CatalogVersionCallback callback) {
246+ CatalogCacheRepository repository (catalog_, settings_.cache ,
247+ settings_.default_cache_expiration );
248+
249+ const auto fetch_option = request.GetFetchOption ();
250+
251+ if (fetch_option == CacheOnly) {
252+ // Initialize response to an error. It will be overwritten in case a valid
253+ // version is found in the cache or user request.
254+ CatalogVersionResponse version_response{
255+ {client::ErrorCode::NotFound, " Failed to find a catalog version." }};
256+
257+ callback (RetrieveLatestVersion (catalog_, request, repository,
258+ std::move (version_response)));
259+ return client::CancellationToken ();
260+ }
261+
262+ return GetLatestVersionOnline (
263+ request.GetBillingTag (), [=](CatalogVersionResponse response) mutable {
264+ if (fetch_option == OnlineOnly) {
265+ callback (std::move (response));
266+ return ;
267+ }
268+
269+ if (!response) {
270+ const auto & error = response.GetError ();
271+ if (error.GetHttpStatusCode () == http::HttpStatusCode::FORBIDDEN) {
272+ OLP_SDK_LOG_WARNING_F (
273+ kLogTag ,
274+ " Latest version request ended with 403 HTTP code, hrn='%s'" ,
275+ catalog_.ToCatalogHRNString ().c_str ());
276+ repository.Clear ();
277+ callback (std::move (response));
278+ return ;
279+ }
280+ }
281+
282+ callback (RetrieveLatestVersion (catalog_, request, repository,
283+ std::move (response)));
284+ });
285+ }
286+
187287VersionsResponse CatalogRepository::GetVersionsList (
188288 const VersionsRequest& request, client::CancellationContext context) {
189289 auto metadata_api =
190290 lookup_client_.LookupApi (" metadata" , " v1" , client::OnlineOnly, context);
191291
192- if (!metadata_api. IsSuccessful () ) {
292+ if (!metadata_api) {
193293 return metadata_api.GetError ();
194294 }
195295
@@ -206,7 +306,7 @@ CatalogVersionResponse CatalogRepository::GetLatestVersionOnline(
206306 auto metadata_api = lookup_client_.LookupApi (
207307 " metadata" , " v1" , client::OnlineIfNotFound, context);
208308
209- if (!metadata_api. IsSuccessful () ) {
309+ if (!metadata_api) {
210310 return metadata_api.GetError ();
211311 }
212312
@@ -216,13 +316,34 @@ CatalogVersionResponse CatalogRepository::GetLatestVersionOnline(
216316 context);
217317}
218318
319+ client::CancellationToken CatalogRepository::GetLatestVersionOnline (
320+ const boost::optional<std::string>& billing_tag,
321+ CatalogVersionCallback callback) {
322+ return lookup_client_.LookupApi (
323+ " metadata" , " v1" , client::OnlineIfNotFound,
324+ [=](client::ApiLookupClient::LookupApiResponse response) {
325+ if (!response) {
326+ callback (response.GetError ());
327+ return client::CancellationToken ();
328+ }
329+
330+ const auto & metadata_client = response.GetResult ();
331+
332+ return MetadataApi::GetLatestCatalogVersion (
333+ metadata_client, -1 , billing_tag,
334+ [=](CatalogVersionResponse catalog_version_response) {
335+ callback (std::move (catalog_version_response));
336+ });
337+ });
338+ }
339+
219340CompatibleVersionsResponse CatalogRepository::GetCompatibleVersions (
220341 const CompatibleVersionsRequest& request,
221342 client::CancellationContext context) {
222343 auto metadata_api =
223344 lookup_client_.LookupApi (" metadata" , " v1" , client::OnlineOnly, context);
224345
225- if (!metadata_api. IsSuccessful () ) {
346+ if (!metadata_api) {
226347 return metadata_api.GetError ();
227348 }
228349
0 commit comments