-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathOlpClient.cpp
More file actions
891 lines (747 loc) · 32.7 KB
/
Copy pathOlpClient.cpp
File metadata and controls
891 lines (747 loc) · 32.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
/*
* Copyright (C) 2019-2026 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
#include "olp/core/client/OlpClient.h"
#include <cctype>
#include <chrono>
#include <future>
#ifdef OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
#include <list>
#endif // OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
#include <sstream>
#include <thread>
#include "PendingUrlRequests.h"
#include "olp/core/client/Condition.h"
#include "olp/core/client/ErrorCode.h"
#include "olp/core/http/HttpStatusCode.h"
#include "olp/core/http/NetworkConstants.h"
#include "olp/core/logging/Log.h"
#include "olp/core/porting/shared_mutex.h"
#include "olp/core/thread/Atomic.h"
#include "olp/core/utils/Url.h"
#ifdef OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
#include "context/ContextInternal.h"
#include "olp/core/context/EnterBackgroundSubscriber.h"
#endif // OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
namespace {
constexpr auto kLogTag = "OlpClient";
constexpr auto kApiKeyParam = "apiKey=";
constexpr auto kHttpPrefix = "http://";
constexpr auto kHttpsPrefix = "https://";
struct RequestSettings {
explicit RequestSettings(const int initial_backdown_period_ms,
const int timeout_s)
: current_backdown_period{initial_backdown_period_ms},
max_wait_time{std::chrono::seconds(timeout_s)} {}
int current_try{0};
std::chrono::milliseconds accumulated_wait_time{0};
std::chrono::milliseconds current_backdown_period{0};
const std::chrono::milliseconds max_wait_time{0};
};
#ifdef OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
class EnterBackgroundSubscriberImpl
: public olp::context::EnterBackgroundSubscriber {
public:
struct Timeouts {
std::chrono::seconds foreground_timeout{0};
std::chrono::seconds background_timeout{0};
};
void OnEnterBackground() override {
std::lock_guard<std::mutex> lock(mutex_);
in_background_ = true;
std::for_each(states_.begin(), states_.end(),
[&](bool value) { value = true; });
}
void OnExitBackground() override {
std::lock_guard<std::mutex> lock(mutex_);
in_background_ = false;
}
bool Wait(olp::client::Condition& client_condition, Timeouts timeouts) {
std::list<bool>::iterator state_itr;
auto init_state = [&]() -> bool {
std::lock_guard<std::mutex> lock(mutex_);
state_itr = states_.insert(states_.end(), in_background_);
return in_background_;
};
auto pop_state = [&]() -> bool {
std::lock_guard<std::mutex> lock(mutex_);
bool state = *state_itr;
states_.erase(state_itr);
return state;
};
const bool started_in_background = init_state();
const auto condition_triggered = client_condition.Wait(
started_in_background ? timeouts.background_timeout
: timeouts.foreground_timeout);
const bool stored_state = pop_state();
if ((condition_triggered &&
(started_in_background || started_in_background == stored_state)) ||
timeouts.background_timeout <= timeouts.foreground_timeout) {
return condition_triggered;
}
return client_condition.Wait(timeouts.background_timeout -
timeouts.foreground_timeout);
}
private:
std::mutex mutex_;
bool in_background_ = false;
std::list<bool> states_;
};
static std::shared_ptr<EnterBackgroundSubscriberImpl> gBackgroundSubscriber =
[]() {
auto impl = std::make_shared<EnterBackgroundSubscriberImpl>();
olp::context::ContextInternal::SubscribeEnterBackground(impl);
return impl;
}();
#endif // OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
} // namespace
namespace olp {
namespace client {
namespace {
using PendingUrlRequestsPtr = std::shared_ptr<PendingUrlRequests>;
using PendingUrlRequestPtr = PendingUrlRequests::PendingUrlRequestPtr;
using NetworkRequestPtr = std::shared_ptr<http::NetworkRequest>;
using RequestSettingsPtr = std::shared_ptr<RequestSettings>;
using NetworkCallbackType = std::function<void(const http::RequestId request_id,
HttpResponse response)>;
static const auto kCancelledErrorResponse =
http::NetworkResponse()
.WithStatus(static_cast<int>(http::ErrorCode::CANCELLED_ERROR))
.WithError("Operation Cancelled.");
static const auto kTimeoutErrorResponse =
http::NetworkResponse()
.WithStatus(static_cast<int>(http::ErrorCode::TIMEOUT_ERROR))
.WithError("Network request timed out.");
NetworkStatistics GetStatistics(const http::NetworkResponse& response) {
return NetworkStatistics(response.GetBytesUploaded(),
response.GetBytesDownloaded());
}
HttpResponse ToHttpResponse(const http::NetworkResponse& response) {
HttpResponse http_response{response.GetStatus(), response.GetError()};
http_response.SetNetworkStatistics(GetStatistics(response));
return http_response;
}
HttpResponse ToHttpResponse(const http::SendOutcome& outcome) {
return {static_cast<int>(outcome.GetErrorCode()),
http::ErrorCodeToString(outcome.GetErrorCode())};
}
bool StatusSuccess(int status) {
return status >= 0 && status < http::HttpStatusCode::BAD_REQUEST;
}
bool CaseInsensitiveCompare(const std::string& str1, const std::string& str2) {
return (str1.size() == str2.size()) &&
std::equal(str1.begin(), str1.end(), str2.begin(),
[](const char& c1, const char& c2) {
return (c1 == c2) ||
(std::tolower(c1) == std::tolower(c2));
});
}
RequestSettingsPtr GetRequestSettings(const RetrySettings& retry_settings) {
return std::make_shared<RequestSettings>(
retry_settings.initial_backdown_period, retry_settings.timeout);
}
bool CheckRetryCondition(const RequestSettings& request,
const RetrySettings& settings,
HttpResponse& response) {
return (request.current_try > settings.max_attempts ||
!settings.retry_condition(response) ||
request.accumulated_wait_time >= request.max_wait_time);
}
std::chrono::milliseconds CalculateNextWaitTime(const RetrySettings& settings,
size_t current_try) {
return settings.backdown_strategy
? settings.backdown_strategy(
std::chrono::milliseconds(settings.initial_backdown_period),
current_try)
: std::chrono::milliseconds::zero();
}
void ExecuteSingleRequest(const std::shared_ptr<http::Network>& network,
const PendingUrlRequestPtr& pending_request,
const http::NetworkRequest& request,
const NetworkCallbackType& callback) {
auto response_body = std::make_shared<std::stringstream>();
auto headers = std::make_shared<http::Headers>();
auto make_request = [&](http::RequestId& id) {
auto send_outcome = network->Send(
request, response_body,
[=](const http::NetworkResponse& response) {
auto status = response.GetStatus();
if (!StatusSuccess(status)) {
response_body->str(
response.GetError().empty()
? "Error occurred, please check HTTP status code"
: response.GetError());
}
callback(response.GetRequestId(),
{status, std::move(*response_body), std::move(*headers)});
},
[=](std::string key, std::string value) {
headers->emplace_back(std::move(key), std::move(value));
});
if (!send_outcome.IsSuccessful()) {
callback(PendingUrlRequest::kInvalidRequestId,
ToHttpResponse(send_outcome));
return CancellationToken();
}
id = send_outcome.GetRequestId();
return CancellationToken([=] { network->Cancel(id); });
};
auto cancelled_func = [&]() {
OLP_SDK_LOG_DEBUG_F(kLogTag,
"ExecuteSingleRequest - already cancelled, url='%s'",
request.GetUrl().c_str());
callback(PendingUrlRequest::kInvalidRequestId,
ToHttpResponse(kCancelledErrorResponse));
};
pending_request->ExecuteOrCancelled(make_request, cancelled_func);
}
NetworkCallbackType GetRetryCallback(
bool merge, const RequestSettingsPtr& settings,
const RetrySettings& retry_settings,
const std::shared_ptr<http::Network>& network,
const PendingUrlRequestsPtr& pending_requests,
const PendingUrlRequestPtr& pending_request,
const NetworkRequestPtr& request) {
return [=](const http::RequestId request_id, HttpResponse response) mutable {
++settings->current_try;
if (CheckRetryCondition(*settings, retry_settings, response)) {
// Response is either successull or retries count/time expired
if (pending_request->GetRequestId() != request_id) {
OLP_SDK_LOG_WARNING_F(
kLogTag,
"Wrong response received, pending_request=%" PRIu64
", request_id=%" PRIu64,
pending_request->GetRequestId(), request_id);
return;
}
if (merge) {
const auto& url = request->GetUrl();
pending_requests->OnRequestCompleted(request_id, url,
std::move(response));
} else {
pending_request->OnRequestCompleted(std::move(response));
}
return;
}
const auto start = std::chrono::steady_clock::now();
// TODO: Do not block thread but instead implement an event queue that will
// trigger next retry once the time expired!
const auto actual_wait_time =
std::min(settings->current_backdown_period,
settings->max_wait_time - settings->accumulated_wait_time);
std::this_thread::sleep_for(actual_wait_time);
settings->accumulated_wait_time += actual_wait_time;
settings->current_backdown_period =
CalculateNextWaitTime(retry_settings, settings->current_try);
OLP_SDK_LOG_DEBUG(
kLogTag, "retry_callback - retrigger after sleep, actual_wait_time="
<< actual_wait_time.count() << "ms, slept="
<< std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count()
<< "ms");
ExecuteSingleRequest(
network, pending_request, *request,
GetRetryCallback(merge, settings, retry_settings, network,
pending_requests, pending_request, request));
};
}
http::NetworkRequest::HttpVerb GetHttpVerb(const std::string& verb) {
if (verb.compare("GET") == 0) {
return http::NetworkRequest::HttpVerb::GET;
} else if (verb.compare("PUT") == 0) {
return http::NetworkRequest::HttpVerb::PUT;
} else if (verb.compare("POST") == 0) {
return http::NetworkRequest::HttpVerb::POST;
} else if (verb.compare("DELETE") == 0) {
return http::NetworkRequest::HttpVerb::DEL;
} else if (verb.compare("OPTIONS") == 0) {
return http::NetworkRequest::HttpVerb::OPTIONS;
} else if (verb.compare("HEAD") == 0) {
return http::NetworkRequest::HttpVerb::HEAD;
} else if (verb.compare("PATCH") == 0) {
return http::NetworkRequest::HttpVerb::PATCH;
}
return http::NetworkRequest::HttpVerb::GET;
}
HttpResponse SendRequest(const http::NetworkRequest& request,
const http::Network::DataCallback& data_callback,
const olp::client::OlpClientSettings& settings,
const olp::client::RetrySettings& retry_settings,
client::CancellationContext context) {
struct ResponseData {
Condition condition;
http::NetworkResponse response{kCancelledErrorResponse};
http::Headers headers;
std::mutex mutex;
bool can_call_data_callback{true};
};
auto response_data = std::make_shared<ResponseData>();
// We dont need a response body in case we want a stream
auto response_body =
data_callback ? nullptr : std::make_shared<std::stringstream>();
auto data_callback_proxy =
!data_callback
? http::Network::DataCallback{}
: [data_callback, response_data](const uint8_t* data, uint64_t offset,
size_t length) {
std::lock_guard<std::mutex> lock(response_data->mutex);
if (response_data->can_call_data_callback) {
data_callback(data, offset, length);
}
};
http::SendOutcome outcome{http::ErrorCode::CANCELLED_ERROR};
const auto timeout = std::chrono::seconds(retry_settings.timeout);
#ifdef OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
const auto background_timeout =
std::chrono::seconds(retry_settings.background_timeout);
#endif // OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
context.ExecuteOrCancelled(
[&]() {
outcome = settings.network_request_handler->Send(
request, response_body,
[response_data](http::NetworkResponse response) {
response_data->response = std::move(response);
response_data->condition.Notify();
},
[response_data](std::string key, std::string value) {
response_data->headers.emplace_back(std::move(key),
std::move(value));
},
data_callback_proxy);
if (!outcome.IsSuccessful()) {
OLP_SDK_LOG_WARNING_F(kLogTag,
"SendRequest: sending request failed, url=%s",
request.GetUrl().c_str());
return CancellationToken();
}
auto request_handler = settings.network_request_handler;
auto request_id = outcome.GetRequestId();
return CancellationToken([=]() {
request_handler->Cancel(request_id);
response_data->condition.Notify();
});
},
[&]() { response_data->condition.Notify(); });
if (!outcome.IsSuccessful()) {
return ToHttpResponse(outcome);
}
#ifdef OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
auto backgroundSubscriber = gBackgroundSubscriber;
const auto condition_triggered = backgroundSubscriber->Wait(
response_data->condition, {timeout, background_timeout});
#else
const auto condition_triggered = response_data->condition.Wait(timeout);
#endif // OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
if (!condition_triggered) {
OLP_SDK_LOG_WARNING_F(
kLogTag,
"Request timed out, request_id=%" PRIu64
", timeout=%i, retry_count=%i, url='%s'",
outcome.GetRequestId(), static_cast<int>(timeout.count()),
retry_settings.max_attempts, request.GetUrl().c_str());
context.CancelOperation();
}
{
std::lock_guard<std::mutex> lock(response_data->mutex);
response_data->can_call_data_callback = false;
}
if (context.IsCancelled() || !condition_triggered) {
return ToHttpResponse(condition_triggered ? kCancelledErrorResponse
: kTimeoutErrorResponse);
}
HttpResponse response = [&]() {
const auto status = response_data->response.GetStatus();
if (status < 0) {
return HttpResponse{status, response_data->response.GetError()};
} else if (response_body) {
return HttpResponse{status, std::move(*response_body),
std::move(response_data->headers)};
} else {
return HttpResponse{status, std::stringstream(),
std::move(response_data->headers)};
}
}();
response.SetNetworkStatistics(GetStatistics(response_data->response));
return response;
}
bool IsPending(const PendingUrlRequestPtr& request) {
// A request is pending when it already triggered a Network call
return request &&
request->GetRequestId() != PendingUrlRequest::kInvalidRequestId;
}
} // namespace
class OlpClient::OlpClientImpl {
public:
OlpClientImpl();
OlpClientImpl(const OlpClientSettings& settings, std::string base_url);
~OlpClientImpl() = default;
void SetBaseUrl(const std::string& base_url);
std::string GetBaseUrl() const;
ParametersType& GetMutableDefaultHeaders();
const OlpClientSettings& GetSettings() const { return settings_; }
CancellationToken CallApi(const std::string& path, const std::string& method,
const ParametersType& query_params,
const ParametersType& header_params,
const ParametersType& form_params,
const RequestBodyType& post_body,
const std::string& content_type,
const NetworkAsyncCallback& callback) const;
HttpResponse CallApi(std::string path, std::string method,
ParametersType query_params,
ParametersType header_params,
http::Network::DataCallback data_callback,
RequestBodyType post_body, std::string content_type,
CancellationContext context) const;
std::shared_ptr<http::NetworkRequest> CreateRequest(
const std::string& path, const std::string& method,
const ParametersType& query_params, const ParametersType& header_params,
const RequestBodyType& post_body, const std::string& content_type) const;
porting::optional<ApiError> AddBearer(bool query_empty,
http::NetworkRequest& request,
CancellationContext& context) const;
private:
using MutexType = std::shared_mutex;
using ReadLock = std::shared_lock<MutexType>;
thread::Atomic<std::string, MutexType, ReadLock> base_url_;
ParametersType default_headers_;
OlpClientSettings settings_;
PendingUrlRequestsPtr pending_requests_;
bool ValidateBaseUrl() const;
};
OlpClient::OlpClientImpl::OlpClientImpl()
: pending_requests_{std::make_shared<PendingUrlRequests>()} {}
OlpClient::OlpClientImpl::OlpClientImpl(const OlpClientSettings& settings,
std::string base_url)
: base_url_{std::move(base_url)},
settings_{settings},
pending_requests_{std::make_shared<PendingUrlRequests>()} {}
void OlpClient::OlpClientImpl::SetBaseUrl(const std::string& base_url) {
base_url_.lockedAssign(base_url);
}
std::string OlpClient::OlpClientImpl::GetBaseUrl() const {
return base_url_.lockedCopy();
}
OlpClient::ParametersType&
OlpClient::OlpClientImpl::GetMutableDefaultHeaders() {
return default_headers_;
}
porting::optional<client::ApiError> OlpClient::OlpClientImpl::AddBearer(
bool query_empty, http::NetworkRequest& request,
CancellationContext& context) const {
const auto& settings = settings_.authentication_settings;
if (!settings) {
return porting::none;
}
if (settings->api_key_provider) {
const auto& api_key = settings->api_key_provider();
request.WithUrl(request.GetUrl() + (query_empty ? "?" : "&") +
kApiKeyParam + api_key);
return porting::none;
}
std::string token;
if (settings->token_provider) {
auto response = settings->token_provider(context);
if (!response) {
return response.GetError();
}
token = response.GetResult().GetAccessToken();
} else {
// There is no token provider defined.
return porting::none;
}
if (token.empty()) {
return client::ApiError(
static_cast<int>(http::ErrorCode::AUTHORIZATION_ERROR),
"Invalid bearer token.");
}
request.WithHeader(http::kAuthorizationHeader,
http::kBearer + std::string(" ") + token);
return porting::none;
}
bool OlpClient::OlpClientImpl::ValidateBaseUrl() const {
return base_url_.locked([](const std::string& base_url) {
return base_url == "" || (base_url.find(kHttpPrefix) != std::string::npos ||
base_url.find(kHttpsPrefix) != std::string::npos);
});
}
std::shared_ptr<http::NetworkRequest> OlpClient::OlpClientImpl::CreateRequest(
const std::string& path, const std::string& method,
const OlpClient::ParametersType& query_params,
const OlpClient::ParametersType& header_params,
const RequestBodyType& post_body, const std::string& content_type) const {
auto network_request = std::make_shared<http::NetworkRequest>(
utils::Url::Construct(GetBaseUrl(), path, query_params));
network_request->WithVerb(GetHttpVerb(method));
for (const auto& header : default_headers_) {
network_request->WithHeader(header.first, header.second);
}
std::string custom_user_agent;
for (const auto& header : header_params) {
// Merge all User-Agent headers into one header.
// This is required for (at least) iOS network implementation,
// which uses headers dictionary without any duplicates.
// User agents entries are usually separated by a whitespace, e.g.
// Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Firefox/47.0
if (CaseInsensitiveCompare(header.first, http::kUserAgentHeader)) {
custom_user_agent += header.second + std::string(" ");
} else {
network_request->WithHeader(header.first, header.second);
}
}
custom_user_agent += http::kOlpSdkUserAgent;
network_request->WithHeader(http::kUserAgentHeader, custom_user_agent);
if (!content_type.empty()) {
network_request->WithHeader(http::kContentTypeHeader, content_type);
}
network_request->WithBody(post_body);
return network_request;
}
CancellationToken OlpClient::OlpClientImpl::CallApi(
const std::string& path, const std::string& method,
const OlpClient::ParametersType& query_params,
const OlpClient::ParametersType& header_params,
const OlpClient::ParametersType& /*form_params*/,
const OlpClient::RequestBodyType& post_body,
const std::string& content_type,
const NetworkAsyncCallback& callback) const {
if (!settings_.network_request_handler) {
callback({static_cast<int>(http::ErrorCode::OFFLINE_ERROR),
"Network layer offline or missing."});
return CancellationToken();
}
if (!ValidateBaseUrl()) {
callback({static_cast<int>(http::ErrorCode::INVALID_URL_ERROR),
"Base URI does not contain a protocol"});
return CancellationToken();
}
auto network_request = CreateRequest(path, method, query_params,
header_params, post_body, content_type);
CancellationContext context;
const auto optional_error =
AddBearer(query_params.empty(), *network_request, context);
if (optional_error) {
auto status = optional_error->GetHttpStatusCode();
if (status == static_cast<int>(http::ErrorCode::UNKNOWN_ERROR)) {
status = static_cast<int>(optional_error->GetErrorCode());
}
callback({status, optional_error->GetMessage()});
return CancellationToken();
}
PendingUrlRequestPtr request_ptr = nullptr;
auto& pending_requests = pending_requests_;
const auto& url = network_request->GetUrl();
CancellationToken cancellation_token;
// Only merge same request in case there is no body as a body can alter the
// outcome of the request and may not match the response of a request with a
// different body.
bool merge = (!post_body || post_body->empty());
OLP_SDK_LOG_DEBUG_F(kLogTag, "CallApi: url='%s', merge='%s'", url.c_str(),
merge ? "true" : "false");
if (merge) {
// Add callback and prepare CancellationToken
auto call_id = pending_requests->Append(url, callback, request_ptr);
cancellation_token =
CancellationToken([=] { pending_requests->Cancel(url, call_id); });
if (IsPending(request_ptr)) {
// Network call is already triggered, we only need to append our
// callback Cancels this callback; internally once all pending callbacks
// are cancelled the Network call will be automatically cancelled also
return cancellation_token;
}
} else {
// In case we do not merge same requests create a single PendingUrlRequest
// as it is handling the cancellation the same as we would do normally
request_ptr = std::make_shared<PendingUrlRequest>();
// Add callback and prepare CancellationToken
auto call_id = request_ptr->Append(callback);
cancellation_token =
CancellationToken([=] { request_ptr->Cancel(call_id); });
}
const auto& retry_settings = settings_.retry_settings;
auto proxy = settings_.proxy_settings.value_or(http::NetworkProxySettings());
network_request->WithSettings(
http::NetworkSettings()
.WithConnectionTimeout(retry_settings.connection_timeout)
#ifdef OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
.WithBackgroundConnectionTimeout(
retry_settings.background_connection_timeout)
#endif // OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
.WithTransferTimeout(retry_settings.transfer_timeout)
.WithProxySettings(std::move(proxy)));
auto network = settings_.network_request_handler;
auto request_settings = GetRequestSettings(retry_settings);
ExecuteSingleRequest(
network, request_ptr, *network_request,
GetRetryCallback(merge, request_settings, retry_settings, network,
pending_requests, request_ptr, network_request));
return cancellation_token;
}
HttpResponse OlpClient::OlpClientImpl::CallApi(
std::string path, std::string method,
OlpClient::ParametersType query_params,
OlpClient::ParametersType header_params,
http::Network::DataCallback data_callback,
OlpClient::RequestBodyType post_body, std::string content_type,
CancellationContext context) const {
if (!settings_.network_request_handler) {
return HttpResponse(static_cast<int>(olp::http::ErrorCode::OFFLINE_ERROR),
"Network request handler is empty.");
}
if (!ValidateBaseUrl()) {
return HttpResponse(
static_cast<int>(olp::http::ErrorCode::INVALID_URL_ERROR),
"Base URI does not contain a protocol");
}
const auto& retry_settings = settings_.retry_settings;
auto network_settings =
http::NetworkSettings()
.WithTransferTimeout(retry_settings.transfer_timeout)
.WithConnectionTimeout(retry_settings.connection_timeout)
#ifdef OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
.WithBackgroundConnectionTimeout(
retry_settings.background_connection_timeout)
#endif // OLP_SDK_NETWORK_IOS_BACKGROUND_DOWNLOAD
.WithProxySettings(
settings_.proxy_settings.value_or(http::NetworkProxySettings()));
auto network_request = http::NetworkRequest(
utils::Url::Construct(GetBaseUrl(), path, query_params));
network_request.WithVerb(GetHttpVerb(method))
.WithBody(std::move(post_body))
.WithSettings(std::move(network_settings));
for (const auto& header : default_headers_) {
network_request.WithHeader(header.first, header.second);
}
std::string custom_user_agent;
for (const auto& header : header_params) {
// Merge all User-Agent headers into one header.
// This is required for (at least) iOS network implementation,
// which uses headers dictionary without any duplicates.
// User agents entries are usually separated by a whitespace, e.g.
// Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Firefox/47.0
if (CaseInsensitiveCompare(header.first, http::kUserAgentHeader)) {
custom_user_agent += header.second + std::string(" ");
} else {
network_request.WithHeader(header.first, header.second);
}
}
custom_user_agent += http::kOlpSdkUserAgent;
network_request.WithHeader(http::kUserAgentHeader, custom_user_agent);
if (!content_type.empty()) {
network_request.WithHeader(http::kContentTypeHeader, content_type);
}
auto backdown_period =
std::chrono::milliseconds(retry_settings.initial_backdown_period);
const auto optional_error =
AddBearer(query_params.empty(), network_request, context);
if (optional_error) {
auto status = optional_error->GetHttpStatusCode();
if (status == static_cast<int>(http::ErrorCode::UNKNOWN_ERROR)) {
status = static_cast<int>(optional_error->GetErrorCode());
}
return {status, optional_error->GetMessage()};
}
auto response = SendRequest(network_request, data_callback, settings_,
retry_settings, context);
NetworkStatistics accumulated_statistics = response.GetNetworkStatistics();
// Make sure that we don't wait longer than `timeout` in retry settings
auto accumulated_wait_time = backdown_period;
const auto max_wait_time =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::seconds(retry_settings.timeout));
for (int i = 1; i <= retry_settings.max_attempts && !context.IsCancelled() &&
accumulated_wait_time < max_wait_time;
i++) {
if (StatusSuccess(response.GetStatus())) {
return response;
}
if (!retry_settings.retry_condition(response)) {
return response;
}
// do the periodical sleep and check for cancellation status in between.
auto duration_to_sleep =
std::min(backdown_period, max_wait_time - accumulated_wait_time);
accumulated_wait_time += duration_to_sleep;
while (duration_to_sleep.count() > 0 && !context.IsCancelled()) {
const auto sleep_ms =
std::min(std::chrono::milliseconds(1000), duration_to_sleep);
std::this_thread::sleep_for(sleep_ms);
duration_to_sleep -= sleep_ms;
}
backdown_period = CalculateNextWaitTime(retry_settings, i);
response = SendRequest(network_request, data_callback, settings_,
retry_settings, context);
// In case we retry, accumulate the stats
accumulated_statistics += response.GetNetworkStatistics();
}
response.SetNetworkStatistics(accumulated_statistics);
return response;
}
OlpClient::OlpClient() : impl_(std::make_shared<OlpClientImpl>()) {}
OlpClient::OlpClient(const OlpClientSettings& settings, std::string base_url)
: impl_(std::make_shared<OlpClientImpl>(settings, std::move(base_url))) {}
OlpClient::~OlpClient() = default;
OlpClient::OlpClient(const OlpClient&) = default;
OlpClient& OlpClient::operator=(const OlpClient&) = default;
OlpClient::OlpClient(OlpClient&&) noexcept = default;
OlpClient& OlpClient::operator=(OlpClient&&) noexcept = default;
void OlpClient::SetBaseUrl(const std::string& base_url) {
impl_->SetBaseUrl(base_url);
}
std::string OlpClient::GetBaseUrl() const { return impl_->GetBaseUrl(); }
OlpClient::ParametersType& OlpClient::GetMutableDefaultHeaders() {
return impl_->GetMutableDefaultHeaders();
}
const OlpClientSettings& OlpClient::GetSettings() const {
return impl_->GetSettings();
}
CancellationToken OlpClient::CallApi(
const std::string& path, const std::string& method,
const ParametersType& query_params, const ParametersType& header_params,
const ParametersType& form_params, const RequestBodyType& post_body,
const std::string& content_type,
const NetworkAsyncCallback& callback) const {
return impl_->CallApi(path, method, query_params, header_params, form_params,
post_body, content_type, callback);
}
HttpResponse OlpClient::CallApi(std::string path, std::string method,
ParametersType query_params,
ParametersType header_params,
ParametersType /*form_params*/,
RequestBodyType post_body,
std::string content_type,
CancellationContext context) const {
return impl_->CallApi(std::move(path), std::move(method),
std::move(query_params), std::move(header_params),
nullptr, std::move(post_body), std::move(content_type),
std::move(context));
}
HttpResponse OlpClient::CallApiStream(std::string path, std::string method,
ParametersType query_params,
ParametersType header_params,
http::Network::DataCallback data_callback,
RequestBodyType post_body,
std::string content_type,
CancellationContext context) const {
return impl_->CallApi(std::move(path), std::move(method),
std::move(query_params), std::move(header_params),
std::move(data_callback), std::move(post_body),
std::move(content_type), std::move(context));
}
} // namespace client
} // namespace olp