Skip to content

Commit 02352c7

Browse files
Update of code snippets in the docs (#1495)
Verified snippets to be sure they works as expected Relates-To: OLPEDGE-2865 Signed-off-by: Andrey Kashcheev <ext-andrey.kashcheev@here.com>
1 parent 91d6934 commit 02352c7

5 files changed

+79
-61
lines changed

docs/authenticate.md

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ For instructions, see the [OAuth tokens](https://developer.here.com/documentatio
1515

1616
You get the `credentials.properties` file.
1717

18-
2. Initialize the authentification settings using the **here.access.key.іd** and **here.access.key.secret** from the `credentials.properties` file as `kKeyId` and `kKeySecret` respectively.
18+
2. Initialize the authentication settings using the **here.access.key.іd** and **here.access.key.secret** from the `credentials.properties` file as `kKeyId` and `kKeySecret` respectively.
1919

2020
> #### Note
2121
> You can also retrieve your credentials from the `credentials.properties` file using the `ReadFromFile` method. For more information, see the [related API documentation](https://developer.here.com/documentation/sdk-cpp/api_reference/classolp_1_1authentication_1_1_authentication_credentials.html#a6bfd8347ebe89e45713b966e621dccdd).
@@ -56,25 +56,28 @@ You can use the `AuthenticationSettings` object to create the `OlpClientSettings
5656
3. Create an authentication client.
5757
5858
```cpp
59-
olp::authentication::AuthenticationSettings settings;
60-
settings.task_scheduler = task_scheduler;
61-
settings.network_request_handler = http_client;
62-
authentication::AutnhentucationClient client(settings);
59+
olp::authentication::AuthenticationSettings auth_client_settings;
60+
auth_client_settings.task_scheduler = task_scheduler;
61+
auth_client_settings.network_request_handler = http_client;
62+
olp::authentication::AuthenticationClient client(auth_client_settings);
6363
```
6464

6565
4. Create the `SignInProperties` object with your project ID.
6666

6767
```cpp
68-
authentication::SignInProperties signin_properties;
68+
olp::authentication::AuthenticationClient::SignInProperties signin_properties;
6969
signin_properties.scope = "<project ID>";
7070
```
7171

72-
5. Create the `SignInClient` object.
72+
5. Call the `SignInClient` API on the previously created `client` object.
7373

7474
```cpp
75-
authentication:: SignInClient(AuthenticationCredentials credentials,
76-
SignInProperties properties,
77-
SignInClientCallback callback);
75+
client.SignInClient(
76+
credentials, signin_properties,
77+
[](olp::authentication::Response<olp::authentication::SignInResult>
78+
response) {
79+
// Handle the response
80+
});
7881
```
7982
8083
You get an access token.
@@ -98,13 +101,12 @@ You can use the `AuthenticationSettings` object to create the `OlpClientSettings
98101
olp::authentication::AuthenticationCredentials credentials(kKeyId, kKeySecret);
99102
```
100103

101-
3. Create an authentication client.
104+
3. Create an authentication client's settings.
102105

103106
```cpp
104-
olp::authentication::AuthenticationSettings settings;
105-
settings.task_scheduler = task_scheduler;
106-
settings.network_request_handler = http_client;
107-
authentication::AutnhentucationClient client(settings);
107+
olp::authentication::AuthenticationSettings auth_client_settings;
108+
auth_client_settings.task_scheduler = task_scheduler;
109+
auth_client_settings.network_request_handler = http_client;
108110
```
109111

110112
4. Get your federated (Facebook or ArcGIS) properties.
@@ -114,36 +116,53 @@ You can use the `AuthenticationSettings` object to create the `OlpClientSettings
114116
5. Initialize your federated properties.
115117

116118
```cpp
117-
olp::authentication::AunthenticationClient::FederatedProperties properties;
119+
olp::authentication::AuthenticationClient::FederatedProperties properties;
118120
properties.access_token = "your-access-token";
119121
```
120122

121-
6. Create the `SignInUserCallback` class.
122-
123-
For more information, see the [`AuthenticationClient` reference documentation](https://developer.here.com/documentation/sdk-cpp/api_reference/classolp_1_1authentication_1_1_authentication_client.html).
124-
125-
7. Create your own token provider using the authentication client created in step 3, your federated credentials, and the `SignInUserCallback` class.
123+
6. Create your own token provider using the authentication client's settings created in step 3, your federated credentials.
126124

127125
> #### Note
128126
> You can call your custom token provider form different threads.
129127
130128
```cpp
131-
auto token = std::make_shared<std::string>();
132-
133-
settings.token_provider = [token](){
134-
if (token->empty() || isExpired(token)) {
135-
std::promise<AuthenticationClient::SignInUserResponse> token_promise;
136-
137-
auto callback = [&token_promise](AuthenticationClient::SignInUserResponse response)
138-
{ token_promise.set(response); };
139-
140-
authentication::AutnhentucationClient client(settings);
141-
client.SignInFacebook(credentials, properties, callback);
142-
auto response = token_promise.get_future().get();
143-
(*token) = response.GetResult().GetAccessToken();
144-
}
145-
return *token;
146-
}
129+
auto token = std::make_shared<olp::client::OauthToken>();
130+
131+
olp::client::AuthenticationSettings auth_settings;
132+
auth_settings.token_provider =
133+
[token, auth_client_settings, credentials,
134+
properties](olp::client::CancellationContext context)
135+
-> olp::client::OauthTokenResponse {
136+
if (context.IsCancelled()) {
137+
return olp::client::ApiError::Cancelled();
138+
}
139+
140+
if (!token->GetAccessToken().empty() &&
141+
std::chrono::system_clock::to_time_t(
142+
std::chrono::system_clock::now()) >= token->GetExpiryTime()) {
143+
return *token;
144+
}
145+
146+
std::promise<olp::authentication::AuthenticationClient::SignInUserResponse>
147+
token_promise;
148+
149+
auto callback =
150+
[&token_promise](
151+
olp::authentication::AuthenticationClient::SignInUserResponse
152+
response) { token_promise.set_value(std::move(response)); };
153+
154+
olp::authentication::AuthenticationClient client(auth_client_settings);
155+
client.SignInFacebook(credentials, properties, callback);
156+
auto response = token_promise.get_future().get();
157+
if (!response) {
158+
return response.GetError();
159+
}
160+
161+
(*token) = olp::client::OauthToken(response.GetResult().GetAccessToken(),
162+
response.GetResult().GetExpiresIn());
163+
164+
return *token;
165+
};
147166
```
148167
149168
You get an access token. By default, it expires in 24 hours. To continue working with the HERE platform after your token expires, generate a new access token.

docs/create-platform-client-settings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ You need to create the `OlpClientSettings` object to get catalog and partition m
3838
retry_settings.retry_condition = [](const olp::client::HttpResponse& response) {
3939
return olp::http::HttpStatusCode::TOO_MANY_REQUESTS == response.status;
4040
};
41-
retry_settings.backdown_strategy = ExponentialBackdownStrategy();
41+
retry_settings.backdown_strategy = olp::client::ExponentialBackdownStrategy();
4242
retry_settings.max_attempts = 3;
4343
retry_settings.timeout = 60;
4444
retry_settings.initial_backdown_period = 200;
@@ -55,7 +55,7 @@ You need to create the `OlpClientSettings` object to get catalog and partition m
5555
5656
```cpp
5757
olp::cache::CacheSettings cache_settings;
58-
//On iOS, the path is relative to the Application Data folder.
58+
// On iOS, the path is relative to the Application Data folder.
5959
cache_settings.disk_path_mutable = "path to mutable cache";
6060
cache_settings.disk_path_protected = "path to protected cache";
6161
cache_settings.max_disk_storage = 1024ull * 1024ull * 32ull;

docs/dataservice-cache-example.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ After building and running the example project, you see the following informatio
5151

5252
```bash
5353
[INFO] protected-cache-example - Mutable cache path is "none"
54-
[INFO] protected-cache-example - Protected cache path is "/tmp/cata.log_client_example/cache"
54+
[INFO] protected-cache-example - Protected cache path is "/tmp/catalog_client_example/cache"
5555
[INFO] ThreadPoolTaskScheduler - Starting thread 'OLPSDKPOOL_0'
56-
[INFO] CatalogRepository - cache catalog '@0^0' found!
57-
[INFO] PartitionsCacheRepository - Get 'hrn:here:data::olp-here-test:edge-example-catalog::versioned-world-layer::1::0::partition'
58-
[INFO] PartitionsRepository - cache data 'versioned-world-layer[1]@0^0' found!
59-
[INFO] DataRepository - cache data 'versioned-world-layer[1]@0^0' found!
56+
[DEBUG] CatalogCacheRepository - GetVersion -> 'hrn:here:data::olp-here-test:edge-example-catalog::latestVersion'
57+
[DEBUG] CatalogRepository - Latest cached version, hrn='hrn:here:data::olp-here-test:edge-example-catalog', version=0
58+
[DEBUG] PartitionsCacheRepository - Get 'hrn:here:data::olp-here-test:edge-example-catalog::versioned-world-layer::1::0::partition'
59+
[DEBUG] PartitionsRepository - GetPartitionById found in cache, hrn='hrn:here:data::olp-here-test:edge-example-catalog', key='versioned-world-layer[1]@0^2'
60+
[DEBUG] DataCacheRepository - Get 'hrn:here:data::olp-here-test:edge-example-catalog::versioned-world-layer::8daa637d-7c81-4322-a600-063f4ae0ef98::Data'
61+
[DEBUG] DataRepository - GetBlobData found in cache, hrn='hrn:here:data::olp-here-test:edge-example-catalog', key='8daa637d-7c81-4322-a600-063f4ae0ef98'
6062
[INFO] protected-cache-example - Request partition data - Success, data size - 3375
6163
```
6264

@@ -101,7 +103,7 @@ You can get data from a [versioned layer](https://developer.here.com/documentati
101103
auto request = olp::dataservice::read::DataRequest()
102104
.WithPartitionId(first_partition_id)
103105
.WithBillingTag(boost::none)
104-
.WithFetchOption(FetchOptions::OnlineIfNotFound);
106+
.WithFetchOption(olp::dataservice::read::FetchOptions::OnlineIfNotFound);
105107
```
106108

107109
4. Call the `GetRequest` method with the `DataRequest` parameter.
@@ -113,8 +115,7 @@ You can get data from a [versioned layer](https://developer.here.com/documentati
113115
5. Wait for the `DataResponse` future.
114116

115117
```cpp
116-
olp::dataservice::read::DataResponse data_response =
117-
future.GetFuture().get();
118+
olp::dataservice::read::DataResponse data_response = future.GetFuture().get();
118119
```
119120

120121
The `DataResponse` object holds details of the completed operation and is used to determine operation success and access resultant data:
@@ -136,5 +137,5 @@ if (data_response.IsSuccessful()) {
136137
The received data is stored in the cache. You can find the path to the cache in the log message.
137138

138139
```bash
139-
[INFO] protected-cache-example - Mutable cache path is "/tmp/cata.log_client_example/cache"
140+
[INFO] protected-cache-example - Mutable cache path is "/tmp/catalog_client_example/cache"
140141
```

docs/dataservice-read-catalog-example.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ Catalog metadata contains a list of configurations that describe the catalog and
199199
3. Create the `CatalogRequest` object.
200200
201201
```cpp
202-
auto request =
203-
olp::dataservice::read::CatalogRequest();
202+
auto request = olp::dataservice::read::CatalogRequest();
204203
```
205204

206205
4. (Optional) Set the needed parameters. For example, to set the billing tag, set the `WithBillingTag` parameter.
@@ -229,8 +228,7 @@ The `CatalogResponse` object holds details of the completed operation and is use
229228

230229
```cpp
231230
if (catalog_response.IsSuccessful()) {
232-
auto response_result =
233-
catalog_response.GetResult();
231+
const auto& response_result = catalog_response.GetResult();
234232
// Handle success
235233
} else {
236234
auto api_error = catalog_response.GetError();
@@ -300,9 +298,10 @@ Partition metadata consists of the following information about the partition:
300298
301299
```cpp
302300
auto request =
303-
olp::dataservice::read::PartitionsRequest()
304-
.WithBillingTag("MyBillingTag")
305-
.WithFetchOption(FetchOptions::OnlineIfNotFound);
301+
olp::dataservice::read::PartitionsRequest()
302+
.WithBillingTag("MyBillingTag")
303+
.WithFetchOption(
304+
olp::dataservice::read::FetchOptions::OnlineIfNotFound);
306305
```
307306

308307
4. Call `GetPartitions` method with the `PartitionRequest` parameter.
@@ -384,7 +383,7 @@ You can request any data version from a [versioned layer](https://developer.here
384383
auto request = olp::dataservice::read::DataRequest()
385384
.WithPartitionId(partition_id)
386385
.WithBillingTag("MyBillingTag")
387-
.WithFetchOption(FetchOptions::OnlineIfNotFound);
386+
.WithFetchOption(olp::dataservice::read::FetchOptions::OnlineIfNotFound);
388387
```
389388

390389
4. Call the `GetRequest` method with the `DataRequest` parameter.
@@ -396,8 +395,7 @@ You can request any data version from a [versioned layer](https://developer.here
396395
5. Wait for the `DataResponse` future.
397396

398397
```cpp
399-
olp::dataservice::read::DataResponse data_response =
400-
future.GetFuture().get();
398+
olp::dataservice::read::DataResponse data_response = future.GetFuture().get();
401399
```
402400

403401
The `DataResponse` object holds details of the completed operation and is used to determine operation success and access resultant data:
@@ -408,7 +406,7 @@ The `DataResponse` object holds details of the completed operation and is used t
408406

409407
```cpp
410408
if (data_response.IsSuccessful()) {
411-
auto response_result = data_response.GetResult();
409+
const auto& response_result = data_response.GetResult();
412410
// Handle success
413411
} else {
414412
auto api_error = data_response.GetError();

examples/ProtectedCacheExample.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2021 HERE Europe B.V.
2+
* Copyright (C) 2020-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ constexpr auto kLogTag = "protected-cache-example";
3636
#ifdef _WIN32
3737
constexpr auto kClientCacheDir = "\\catalog_client_example\\cache";
3838
#else
39-
constexpr auto kClientCacheDir = "/cata.log_client_example/cache";
39+
constexpr auto kClientCacheDir = "/catalog_client_example/cache";
4040
#endif
4141

4242
std::string first_layer_id("versioned-world-layer");

0 commit comments

Comments
 (0)