Skip to content

Commit 4f07bae

Browse files
Made deprecated HttpResponse fields private (#1558)
Methods should be used instead Relates-To: OCMAM-212 Signed-off-by: Andrey Kashcheev <ext-andrey.kashcheev@here.com>
1 parent 4dbffdf commit 4f07bae

35 files changed

Lines changed: 301 additions & 278 deletions

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,12 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
322322
CallAuth(client, kOauthEndpoint, context, credentials, request_body,
323323
timer.GetRequestTime());
324324

325-
const auto status = auth_response.status;
325+
const auto status = auth_response.GetStatus();
326326
if (status < 0) {
327327
response = GetSignInResponse<SignInResult>(auth_response, context,
328328
credentials.GetKey());
329329
} else {
330-
response = ParseAuthResponse(status, auth_response.response);
330+
response = ParseAuthResponse(status, auth_response.GetRawResponse());
331331
}
332332

333333
if (retry_settings.retry_condition(auth_response)) {
@@ -338,7 +338,7 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
338338
// In case we can't authorize with system time, retry with the server
339339
// time from response headers (if available).
340340
if (HasWrongTimestamp(response.GetResult())) {
341-
auto server_time = GetTimestampFromHeaders(auth_response.headers);
341+
auto server_time = GetTimestampFromHeaders(auth_response.GetHeaders());
342342
if (server_time) {
343343
timer = RequestTimer(*server_time);
344344
continue;
@@ -386,15 +386,15 @@ TimeResponse AuthenticationClientImpl::GetTimeFromServer(
386386
auto http_result = client.CallApi(kTimestampEndpoint, "GET", {}, {}, {},
387387
nullptr, {}, context);
388388

389-
if (http_result.status != http::HttpStatusCode::OK) {
390-
auto response = http_result.response.str();
389+
if (http_result.GetStatus() != http::HttpStatusCode::OK) {
390+
auto response = http_result.GetResponseAsString();
391391
OLP_SDK_LOG_WARNING_F(
392392
kLogTag, "Failed to get time from server, status=%d, response='%s'",
393-
http_result.status, response.c_str());
394-
return client::ApiError(http_result.status, http_result.response.str());
393+
http_result.GetStatus(), response.c_str());
394+
return client::ApiError(http_result.GetStatus(), response);
395395
}
396396

397-
auto server_time = ParseTimeResponse(http_result.response);
397+
auto server_time = ParseTimeResponse(http_result.GetRawResponse());
398398

399399
if (!server_time) {
400400
const auto& error = server_time.GetError();
@@ -447,7 +447,8 @@ client::CancellationToken AuthenticationClientImpl::SignInApple(
447447
properties.GetClientId());
448448
}
449449

450-
auto response = ParseUserAuthResponse(status, auth_response.response);
450+
auto response =
451+
ParseUserAuthResponse(status, auth_response.GetRawResponse());
451452

452453
if (status == http::HttpStatusCode::OK) {
453454
StoreInCache(properties.GetClientId(), response);
@@ -513,13 +514,13 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
513514
auto auth_response = CallAuth(client, endpoint, context, credentials,
514515
request_body, timer.GetRequestTime());
515516

516-
auto status = auth_response.status;
517+
auto status = auth_response.GetStatus();
517518
if (status < 0) {
518519
return GetSignInResponse<SignInUserResult>(auth_response, context,
519520
credentials.GetKey());
520521
}
521522

522-
response = ParseUserAuthResponse(status, auth_response.response);
523+
response = ParseUserAuthResponse(status, auth_response.GetRawResponse());
523524

524525
if (retry_settings.retry_condition(auth_response)) {
525526
RetryDelay(retry_settings, retry);
@@ -529,7 +530,7 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
529530
// In case we can't authorize with system time, retry with the server
530531
// time from response headers (if available).
531532
if (HasWrongTimestamp(response)) {
532-
auto server_time = GetTimestampFromHeaders(auth_response.headers);
533+
auto server_time = GetTimestampFromHeaders(auth_response.GetHeaders());
533534
if (server_time) {
534535
timer = RequestTimer(*server_time);
535536
continue;
@@ -664,15 +665,15 @@ client::CancellationToken AuthenticationClientImpl::IntrospectApp(
664665
nullptr, {}, context);
665666

666667
rapidjson::Document document;
667-
rapidjson::IStreamWrapper stream(http_result.response);
668+
rapidjson::IStreamWrapper stream(http_result.GetRawResponse());
668669
document.ParseStream(stream);
669-
if (http_result.status != http::HttpStatusCode::OK) {
670+
if (http_result.GetStatus() != http::HttpStatusCode::OK) {
670671
// HttpResult response can be error message or valid json with it.
671-
std::string msg = http_result.response.str();
672+
std::string msg = http_result.GetResponseAsString();
672673
if (!document.HasParseError() && document.HasMember(Constants::MESSAGE)) {
673674
msg = document[Constants::MESSAGE].GetString();
674675
}
675-
return client::ApiError({http_result.status, msg});
676+
return client::ApiError({http_result.GetStatus(), msg});
676677
}
677678

678679
if (document.HasParseError()) {
@@ -711,15 +712,15 @@ client::CancellationToken AuthenticationClientImpl::Authorize(
711712
kApplicationJson, context);
712713

713714
rapidjson::Document document;
714-
rapidjson::IStreamWrapper stream(http_result.response);
715+
rapidjson::IStreamWrapper stream(http_result.GetRawResponse());
715716
document.ParseStream(stream);
716-
if (http_result.status != http::HttpStatusCode::OK) {
717+
if (http_result.GetStatus() != http::HttpStatusCode::OK) {
717718
// HttpResult response can be error message or valid json with it.
718-
std::string msg = http_result.response.str();
719+
std::string msg = http_result.GetResponseAsString();
719720
if (!document.HasParseError() && document.HasMember(Constants::MESSAGE)) {
720721
msg = document[Constants::MESSAGE].GetString();
721722
}
722-
return client::ApiError({http_result.status, msg});
723+
return client::ApiError({http_result.GetStatus(), msg});
723724
} else if (!document.HasParseError() &&
724725
document.HasMember(Constants::ERROR_CODE) &&
725726
document[Constants::ERROR_CODE].IsInt()) {

olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp

Lines changed: 5 additions & 5 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.
@@ -71,16 +71,16 @@ std::string Base64Encode(const Crypto::Sha256Digest& digest) {
7171
}
7272

7373
Response<rapidjson::Document> Parse(client::HttpResponse& http_response) {
74-
rapidjson::IStreamWrapper stream(http_response.response);
74+
rapidjson::IStreamWrapper stream(http_response.GetRawResponse());
7575
rapidjson::Document document;
7676
document.ParseStream(stream);
77-
if (http_response.status != http::HttpStatusCode::OK) {
77+
if (http_response.GetStatus() != http::HttpStatusCode::OK) {
7878
// HttpResult response can be error message or valid json with it.
79-
std::string msg = http_response.response.str();
79+
std::string msg = http_response.GetResponseAsString();
8080
if (!document.HasParseError() && document.HasMember(Constants::MESSAGE)) {
8181
msg = document[Constants::MESSAGE].GetString();
8282
}
83-
return client::ApiError({http_response.status, msg});
83+
return client::ApiError({http_response.GetStatus(), msg});
8484
}
8585

8686
if (document.HasParseError()) {

olp-cpp-sdk-authentication/src/TokenEndpointImpl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ TimeResponse GetTimeFromServer(client::CancellationContext& context,
153153
std::string response;
154154
http_result.GetResponse(response);
155155

156-
if (http_result.status != http::HttpStatusCode::OK) {
156+
if (http_result.GetStatus() != http::HttpStatusCode::OK) {
157157
OLP_SDK_LOG_WARNING_F(
158158
kLogTag, "Failed to get time from server, status=%d, response='%s'",
159159
http_result.GetStatus(), response.c_str());
@@ -268,7 +268,7 @@ SignInResponse TokenEndpointImpl::SignInClient(
268268

269269
auto auth_response = CallAuth(client, kOauthEndpoint, context, request_body,
270270
timer.GetRequestTime());
271-
const auto status = auth_response.status;
271+
const auto status = auth_response.GetStatus();
272272
if (status < 0) {
273273
// If a timeout occurred, the cancellation id done through the context.
274274
// So this case needs to be handled independently of context state.
@@ -279,15 +279,15 @@ SignInResponse TokenEndpointImpl::SignInClient(
279279

280280
// Auth response message may be empty in case of unknown errors.
281281
// Fill in the message as a status string representation in this case.
282-
auto message = auth_response.response.str();
282+
auto message = auth_response.GetResponseAsString();
283283
if (message.empty()) {
284284
message = http::HttpErrorToString(status);
285285
}
286286

287287
return client::ApiError(status, message);
288288
}
289289

290-
response = ParseAuthResponse(status, auth_response.response);
290+
response = ParseAuthResponse(status, auth_response.GetRawResponse());
291291

292292
// The request ended up with `OK` status should not be retriggered even if
293293
// `retry_condition` is `true` for this `HttpResponse`.
@@ -303,7 +303,7 @@ SignInResponse TokenEndpointImpl::SignInClient(
303303
// In case we can't authorize with system time, retry with the server
304304
// time from response headers (if available).
305305
if (HasWrongTimestamp(response)) {
306-
auto server_time = GetTimestampFromHeaders(auth_response.headers);
306+
auto server_time = GetTimestampFromHeaders(auth_response.GetHeaders());
307307
if (server_time) {
308308
timer = RequestTimer(*server_time);
309309
continue;

olp-cpp-sdk-core/include/olp/core/client/HttpResponse.h

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2022 HERE Europe B.V.
2+
* Copyright (C) 2019-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.
@@ -98,7 +98,7 @@ class CORE_API HttpResponse {
9898
* @param response The response body.
9999
*/
100100
HttpResponse(int status, std::string response = {}) // NOLINT
101-
: status(status), response(std::move(response)) {}
101+
: status_(status), response_(std::move(response)) {}
102102

103103
/**
104104
* @brief Creates the `HttpResponse` instance.
@@ -108,7 +108,7 @@ class CORE_API HttpResponse {
108108
*
109109
*/
110110
HttpResponse(int status, std::stringstream&& response)
111-
: status(status), response(std::move(response)) {}
111+
: status_(status), response_(std::move(response)) {}
112112

113113
/**
114114
* @brief Creates the `HttpResponse` instance.
@@ -119,9 +119,9 @@ class CORE_API HttpResponse {
119119
*
120120
*/
121121
HttpResponse(int status, std::stringstream&& response, http::Headers headers)
122-
: status(status),
123-
response(std::move(response)),
124-
headers(std::move(headers)) {}
122+
: status_(status),
123+
response_(std::move(response)),
124+
headers_(std::move(headers)) {}
125125

126126
/**
127127
* @brief A copy constructor.
@@ -132,14 +132,14 @@ class CORE_API HttpResponse {
132132
* @param other The instance of `HttpStatus` to copy from.
133133
*/
134134
HttpResponse(const HttpResponse& other)
135-
: status(other.status), headers(other.headers) {
136-
response << other.response.rdbuf();
137-
if (!response.good()) {
135+
: status_(other.status_), headers_(other.headers_) {
136+
response_ << other.response_.rdbuf();
137+
if (!response_.good()) {
138138
// Depending on the users handling of the stringstream it might be that
139139
// the read position is already at the end and thus operator<< cannot
140140
// read anything, so lets try with the safer but more memory intensive
141141
// solution as a second step.
142-
response.str(other.response.str());
142+
response_.str(other.GetResponseAsString());
143143
}
144144
}
145145

@@ -153,10 +153,10 @@ class CORE_API HttpResponse {
153153
*/
154154
HttpResponse& operator=(const HttpResponse& other) {
155155
if (this != &other) {
156-
status = other.status;
157-
response = std::stringstream{};
158-
response << other.response.rdbuf();
159-
headers = other.headers;
156+
status_ = other.status_;
157+
response_ = std::stringstream{};
158+
response_ << other.response_.rdbuf();
159+
headers_ = other.headers_;
160160
}
161161

162162
return *this;
@@ -174,29 +174,58 @@ class CORE_API HttpResponse {
174174
* @param output Reference to a vector.
175175
*/
176176
void GetResponse(std::vector<unsigned char>& output) {
177-
response.seekg(0, std::ios::end);
178-
const auto pos = response.tellg();
177+
response_.seekg(0, std::ios::end);
178+
const auto pos = response_.tellg();
179179
if (pos > 0) {
180180
output.resize(pos);
181181
}
182-
response.seekg(0, std::ios::beg);
183-
response.read(reinterpret_cast<char*>(output.data()), output.size());
184-
response.seekg(0, std::ios::beg);
182+
response_.seekg(0, std::ios::beg);
183+
response_.read(reinterpret_cast<char*>(output.data()), output.size());
184+
response_.seekg(0, std::ios::beg);
185185
}
186186

187187
/**
188188
* @brief Copy `HttpResponse` content to a string.
189189
*
190190
* @param output Reference to a string.
191191
*/
192-
void GetResponse(std::string& output) const { output = response.str(); }
192+
void GetResponse(std::string& output) const { output = response_.str(); }
193+
194+
/**
195+
* @brief Get the response body as a vector of unsigned chars.
196+
*
197+
* @return The response body as a vector of unsigned chars.
198+
*/
199+
std::vector<unsigned char> GetResponseAsBytes() {
200+
std::vector<unsigned char> bytes;
201+
GetResponse(bytes);
202+
return bytes;
203+
}
204+
205+
/**
206+
* @brief Renders `HttpResponse` content to a string.
207+
*
208+
* @return String representation of the response.
209+
*/
210+
std::string GetResponseAsString() const {
211+
std::string result;
212+
GetResponse(result);
213+
return result;
214+
}
215+
216+
/**
217+
* @brief Return the reference to the response object.
218+
*
219+
* @return The reference to the response object.
220+
*/
221+
std::stringstream& GetRawResponse() { return response_; }
193222

194223
/**
195224
* @brief Return the const reference to the response headers.
196225
*
197226
* @return The const reference to the headers vector.
198227
*/
199-
const http::Headers& GetHeaders() const { return headers; }
228+
const http::Headers& GetHeaders() const { return headers_; }
200229

201230
/**
202231
* @brief Return the response status.
@@ -206,7 +235,7 @@ class CORE_API HttpResponse {
206235
*
207236
* @return The response status.
208237
*/
209-
int GetStatus() const { return status; }
238+
int GetStatus() const { return status_; }
210239

211240
/**
212241
* @brief Set `NetworkStatistics`.
@@ -226,21 +255,10 @@ class CORE_API HttpResponse {
226255
return network_statistics_;
227256
}
228257

229-
/// The HTTP Status. This can be either a `ErrorCode` if negative or a
230-
/// `HttpStatusCode` if positive.
231-
/// @deprecated: This field will be marked as private by 01.2021.
232-
/// Please do not use directly, use `GetStatus()` method instead.
233-
int status{static_cast<int>(olp::http::ErrorCode::UNKNOWN_ERROR)};
234-
/// The HTTP response.
235-
/// @deprecated: This field will be marked as private by 01.2021.
236-
/// Please do not use directly, use `GetResponse()` methods instead.
237-
std::stringstream response;
238-
/// HTTP headers.
239-
/// @deprecated: This field will be marked as private by 01.2021.
240-
/// Please do not use directly, use `GetHeaders()` method instead.
241-
http::Headers headers;
242-
243258
private:
259+
int status_{static_cast<int>(olp::http::ErrorCode::UNKNOWN_ERROR)};
260+
std::stringstream response_;
261+
http::Headers headers_;
244262
NetworkStatistics network_statistics_;
245263
};
246264

olp-cpp-sdk-core/src/client/OlpClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ HttpResponse OlpClient::OlpClientImpl::CallApi(
801801
for (int i = 1; i <= retry_settings.max_attempts && !context.IsCancelled() &&
802802
accumulated_wait_time < max_wait_time;
803803
i++) {
804-
if (StatusSuccess(response.status)) {
804+
if (StatusSuccess(response.GetStatus())) {
805805
return response;
806806
}
807807

olp-cpp-sdk-core/src/client/PendingUrlRequests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 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.
@@ -145,7 +145,7 @@ void PendingUrlRequest::OnRequestCompleted(HttpResponse response) {
145145
// We need to reset the stringstream position else the next copy
146146
// constructor will not be able to read anything because the read
147147
// position is at the end of the stream.
148-
response_out.response.seekg(0, std::ios::beg);
148+
response_out.GetRawResponse().seekg(0, std::ios::beg);
149149
}
150150

151151
if (!cancelled_callbacks.empty() &&
@@ -159,7 +159,7 @@ void PendingUrlRequest::OnRequestCompleted(HttpResponse response) {
159159
// We need to reset the stringstream position else the next copy
160160
// constructor will not be able to read anything because the read
161161
// position is at the end of the stream.
162-
response_out.response.seekg(0, std::ios::beg);
162+
response_out.GetRawResponse().seekg(0, std::ios::beg);
163163
}
164164
}
165165

0 commit comments

Comments
 (0)