olp-cpp-sdk  1.21.0
HttpResponse.h
1 /*
2  * Copyright (C) 2019-2024 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <sstream>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <olp/core/CoreApi.h>
28 #include <olp/core/http/NetworkTypes.h>
29 
30 namespace olp {
31 namespace client {
32 
38  public:
39  NetworkStatistics() = default;
40 
49  NetworkStatistics(uint64_t bytes_uploaded, uint64_t bytes_downloaded)
50  : bytes_uploaded_{bytes_uploaded}, bytes_downloaded_{bytes_downloaded} {}
51 
57  uint64_t GetBytesUploaded() const { return bytes_uploaded_; }
58 
64  uint64_t GetBytesDownloaded() const { return bytes_downloaded_; }
65 
68  bytes_uploaded_ += other.bytes_uploaded_;
69  bytes_downloaded_ += other.bytes_downloaded_;
70  return *this;
71  }
72 
75  NetworkStatistics statistics(*this);
76  statistics += other;
77  return statistics;
78  }
79 
80  private:
81  uint64_t bytes_uploaded_{0};
82  uint64_t bytes_downloaded_{0};
83 };
84 
89 class CORE_API HttpResponse {
90  public:
91  HttpResponse() = default;
92  virtual ~HttpResponse() = default;
93 
100  HttpResponse(int status, std::string response = {}) // NOLINT
101  : status_(status), response_(std::move(response)) {}
102 
110  HttpResponse(int status, std::stringstream&& response)
111  : status_(status), response_(std::move(response)) {}
112 
121  HttpResponse(int status, std::stringstream&& response, http::Headers headers)
122  : status_(status),
123  response_(std::move(response)),
124  headers_(std::move(headers)) {}
125 
135  : status_(other.status_), headers_(other.headers_) {
136  response_ << other.response_.rdbuf();
137  if (!response_.good()) {
138  // Depending on the users handling of the stringstream it might be that
139  // the read position is already at the end and thus operator<< cannot
140  // read anything, so lets try with the safer but more memory intensive
141  // solution as a second step.
142  response_.str(other.GetResponseAsString());
143  }
144  }
145 
155  if (this != &other) {
156  status_ = other.status_;
157  response_ = std::stringstream{};
158  response_ << other.response_.rdbuf();
159  headers_ = other.headers_;
160  }
161 
162  return *this;
163  }
164 
167 
170 
176  void GetResponse(std::vector<unsigned char>& output) {
177  response_.seekg(0, std::ios::end);
178  const auto pos = response_.tellg();
179  if (pos > 0) {
180  output.resize(pos);
181  }
182  response_.seekg(0, std::ios::beg);
183  response_.read(reinterpret_cast<char*>(output.data()), output.size());
184  response_.seekg(0, std::ios::beg);
185  }
186 
192  void GetResponse(std::string& output) const { output = response_.str(); }
193 
199  std::vector<unsigned char> GetResponseAsBytes() {
200  std::vector<unsigned char> bytes;
201  GetResponse(bytes);
202  return bytes;
203  }
204 
210  std::string GetResponseAsString() const {
211  std::string result;
212  GetResponse(result);
213  return result;
214  }
215 
221  std::stringstream& GetRawResponse() { return response_; }
222 
228  const http::Headers& GetHeaders() const { return headers_; }
229 
238  int GetStatus() const { return status_; }
239 
245  void SetNetworkStatistics(NetworkStatistics network_statistics) {
246  network_statistics_ = network_statistics;
247  }
248 
255  return network_statistics_;
256  }
257 
258  private:
259  int status_{static_cast<int>(olp::http::ErrorCode::UNKNOWN_ERROR)};
260  std::stringstream response_;
261  http::Headers headers_;
262  NetworkStatistics network_statistics_;
263 };
264 
265 } // namespace client
266 } // namespace olp
This class represents the HTTP response created from the NetworkResponse and the request body.
Definition: HttpResponse.h:89
HttpResponse(int status, std::stringstream &&response, http::Headers headers)
Creates the HttpResponse instance.
Definition: HttpResponse.h:121
HttpResponse(int status, std::string response={})
Creates the HttpResponse instance.
Definition: HttpResponse.h:100
std::stringstream & GetRawResponse()
Return the reference to the response object.
Definition: HttpResponse.h:221
const NetworkStatistics & GetNetworkStatistics() const
Get the NetworkStatistics.
Definition: HttpResponse.h:254
HttpResponse(int status, std::stringstream &&response)
Creates the HttpResponse instance.
Definition: HttpResponse.h:110
HttpResponse(const HttpResponse &other)
A copy constructor.
Definition: HttpResponse.h:134
int GetStatus() const
Return the response status.
Definition: HttpResponse.h:238
void GetResponse(std::string &output) const
Copy HttpResponse content to a string.
Definition: HttpResponse.h:192
std::string GetResponseAsString() const
Renders HttpResponse content to a string.
Definition: HttpResponse.h:210
HttpResponse & operator=(HttpResponse &&)=default
A default move assignment operator.
void GetResponse(std::vector< unsigned char > &output)
Copy HttpResponse content to a vector of unsigned chars.
Definition: HttpResponse.h:176
HttpResponse & operator=(const HttpResponse &other)
A copy assignment operator.
Definition: HttpResponse.h:154
const http::Headers & GetHeaders() const
Return the const reference to the response headers.
Definition: HttpResponse.h:228
std::vector< unsigned char > GetResponseAsBytes()
Get the response body as a vector of unsigned chars.
Definition: HttpResponse.h:199
void SetNetworkStatistics(NetworkStatistics network_statistics)
Set NetworkStatistics.
Definition: HttpResponse.h:245
HttpResponse(HttpResponse &&)=default
A default move constructor.
Network statistics with information on the outbound and inbound trafic during API calls.
Definition: HttpResponse.h:37
NetworkStatistics & operator+=(const NetworkStatistics &other)
An overloaded addition operator for accumulating statistics.
Definition: HttpResponse.h:67
NetworkStatistics(uint64_t bytes_uploaded, uint64_t bytes_downloaded)
Creates the NetworkStatistics instance.
Definition: HttpResponse.h:49
NetworkStatistics operator+(const NetworkStatistics &other) const
An overloaded addition operator for accumulating statistics.
Definition: HttpResponse.h:74
uint64_t GetBytesUploaded() const
Get the number of bytes of outbound traffic.
Definition: HttpResponse.h:57
uint64_t GetBytesDownloaded() const
Get the number of bytes of inbound traffic.
Definition: HttpResponse.h:64
@ UNKNOWN_ERROR
Internal error that can't be interpreted.
std::vector< Header > Headers
An alias for a vector of the HTTP headers.
Definition: NetworkTypes.h:140
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24