olp-cpp-sdk  1.22.0
NetworkResponse.h
1 /*
2  * Copyright (C) 2019-2025 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 <array>
23 #include <bitset>
24 #include <chrono>
25 #include <string>
26 
27 #include <boost/optional/optional.hpp>
28 
29 #include <olp/core/CoreApi.h>
30 #include <olp/core/http/NetworkTypes.h>
31 
32 namespace olp {
33 namespace http {
34 
38 struct Diagnostics {
39  using MicroSeconds = std::chrono::duration<uint32_t, std::micro>;
40 
41  enum Timings {
42  Queue = 0, // Delay until the request is processed
43  NameLookup, // Time taken for DNS name lookup
44  Connect, // Time taken to establish connection
45  SSL_Handshake, // Time taken to establish a secured connection
46  Send, // Time taken to send the request
47  Wait, // Time delay until server starts responding
48  Receive, // Time taken to receive the response
49  Total, // Total time taken for reqeust
50  Count
51  };
52 
54  std::array<MicroSeconds, Count> timings{};
55 
57  std::bitset<Count> available_timings{};
58 };
59 
63 class CORE_API NetworkResponse final {
64  public:
70  bool IsCancelled() const;
71 
77  int GetStatus() const;
78 
87 
94  const std::string& GetError() const;
95 
105  NetworkResponse& WithError(std::string error);
106 
113 
122 
129  uint64_t GetBytesUploaded() const;
130 
139  NetworkResponse& WithBytesUploaded(uint64_t bytes_uploaded);
140 
148  uint64_t GetBytesDownloaded() const;
149 
158  NetworkResponse& WithBytesDownloaded(uint64_t bytes_downloaded);
159 
165  const boost::optional<Diagnostics>& GetDiagnostics() const;
166 
175 
176  private:
178  RequestId request_id_{0};
180  int status_{0};
182  std::string error_;
184  uint64_t bytes_uploaded_{0};
186  uint64_t bytes_downloaded_{0};
188  boost::optional<Diagnostics> diagnostics_;
189 };
190 
191 } // namespace http
192 } // namespace olp
A network response abstraction for the HTTP request.
Definition: NetworkResponse.h:63
int GetStatus() const
Gets the HTTP response code.
NetworkResponse & WithBytesDownloaded(uint64_t bytes_downloaded)
Sets the number of bytes downloaded during the associated network request.
const std::string & GetError() const
Gets the human-readable error message if the associated request failed.
uint64_t GetBytesUploaded() const
Gets the number of bytes uploaded during the associated network request.
NetworkResponse & WithStatus(int status)
Sets the HTTP response code.
NetworkResponse & WithRequestId(RequestId id)
Sets the ID of the associated network request.
NetworkResponse & WithError(std::string error)
Sets the human-readable error message if the associated request failed.
bool IsCancelled() const
Checks if the associated request was canceled.
RequestId GetRequestId() const
Gets the ID of the associated network request.
NetworkResponse & WithBytesUploaded(uint64_t bytes_uploaded)
Sets the number of bytes uploaded during the associated network request.
uint64_t GetBytesDownloaded() const
Gets the number of bytes downloaded during the associated network request.
const boost::optional< Diagnostics > & GetDiagnostics() const
Gets the optional diagnostics if set.
NetworkResponse & WithDiagnostics(Diagnostics diagnostics)
Sets the request diagnostics.
std::uint64_t RequestId
A unique request ID.
Definition: NetworkTypes.h:41
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24
Network request timings.
Definition: NetworkResponse.h:38
std::bitset< Count > available_timings
Availability flag, specify which timing is available.
Definition: NetworkResponse.h:57
std::array< MicroSeconds, Count > timings
Timing values.
Definition: NetworkResponse.h:54