olp-cpp-sdk  1.21.0
NetworkTypes.h
1 /*
2  * Copyright (C) 2019-2021 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 <cstdint>
23 #include <limits>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include <olp/core/CoreApi.h>
29 
30 namespace olp {
31 namespace http {
32 
41 using RequestId = std::uint64_t;
42 
46  RequestIdInvalid = std::numeric_limits<RequestId>::min(),
48  RequestIdMin = std::numeric_limits<RequestId>::min() + 1,
50  RequestIdMax = std::numeric_limits<RequestId>::max(),
51 };
52 
54 enum class ErrorCode {
55  SUCCESS = 0,
56  IO_ERROR = -1,
57  AUTHORIZATION_ERROR = -2,
58  INVALID_URL_ERROR = -3,
59  OFFLINE_ERROR = -4,
60  CANCELLED_ERROR = -5,
61  AUTHENTICATION_ERROR = -6,
62  TIMEOUT_ERROR = -7,
66  UNKNOWN_ERROR = -9,
67 };
68 
76 class CORE_API SendOutcome final {
77  public:
79  static constexpr RequestId kInvalidRequestId =
81 
87  explicit SendOutcome(RequestId request_id) : request_id_(request_id) {}
88 
94  explicit SendOutcome(ErrorCode error_code) : error_code_(error_code) {}
95 
102  bool IsSuccessful() const {
103  return error_code_ == ErrorCode::SUCCESS &&
104  request_id_ != kInvalidRequestId;
105  }
106 
113  RequestId GetRequestId() const { return request_id_; }
114 
121  ErrorCode GetErrorCode() const { return error_code_; }
122 
123  private:
125  RequestId request_id_{kInvalidRequestId};
127  ErrorCode error_code_{ErrorCode::SUCCESS};
128 };
129 
134 CORE_API std::string ErrorCodeToString(ErrorCode code);
135 
137 using Header = std::pair<std::string, std::string>;
138 
140 using Headers = std::vector<Header>;
141 
142 } // namespace http
143 } // namespace olp
Rrepresents the outcome of a network request.
Definition: NetworkTypes.h:76
SendOutcome(RequestId request_id)
Sets a successful request outcome.
Definition: NetworkTypes.h:87
RequestId GetRequestId() const
Gets the request ID.
Definition: NetworkTypes.h:113
bool IsSuccessful() const
Checks if the network request push was successful.
Definition: NetworkTypes.h:102
ErrorCode GetErrorCode() const
Gets the error code.
Definition: NetworkTypes.h:121
SendOutcome(ErrorCode error_code)
Sets an unsuccessful request outcome.
Definition: NetworkTypes.h:94
RequestIdConstants
The list of special values for NetworkRequestId.
Definition: NetworkTypes.h:44
@ RequestIdMax
The maximum value of the valid request ID.
@ RequestIdMin
The minimum value of the valid request ID.
@ RequestIdInvalid
The value that indicates the invalid request ID.
ErrorCode
The common Network error codes.
Definition: NetworkTypes.h:54
@ UNKNOWN_ERROR
Internal error that can't be interpreted.
CORE_API std::string ErrorCodeToString(ErrorCode code)
The helper function that converts an error code to a human readable string.
std::uint64_t RequestId
A unique request ID.
Definition: NetworkTypes.h:41
std::vector< Header > Headers
An alias for a vector of the HTTP headers.
Definition: NetworkTypes.h:140
std::pair< std::string, std::string > Header
An alias for the HTTP header.
Definition: NetworkTypes.h:137
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24