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
0 commit comments