forked from apache/iceberg-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.cc
More file actions
225 lines (190 loc) · 8.65 KB
/
http_client.cc
File metadata and controls
225 lines (190 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "iceberg/catalog/rest/http_client.h"
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include "iceberg/catalog/rest/constant.h"
#include "iceberg/catalog/rest/error_handlers.h"
#include "iceberg/catalog/rest/json_internal.h"
#include "iceberg/catalog/rest/rest_util.h"
#include "iceberg/json_internal.h"
#include "iceberg/result.h"
#include "iceberg/util/macros.h"
namespace iceberg::rest {
class HttpResponse::Impl {
public:
explicit Impl(cpr::Response&& response) : response_(std::move(response)) {}
~Impl() = default;
int32_t status_code() const { return static_cast<int32_t>(response_.status_code); }
std::string body() const { return response_.text; }
std::unordered_map<std::string, std::string> headers() const {
return {response_.header.begin(), response_.header.end()};
}
private:
cpr::Response response_;
};
HttpResponse::HttpResponse() = default;
HttpResponse::~HttpResponse() = default;
HttpResponse::HttpResponse(HttpResponse&&) noexcept = default;
HttpResponse& HttpResponse::operator=(HttpResponse&&) noexcept = default;
int32_t HttpResponse::status_code() const { return impl_->status_code(); }
std::string HttpResponse::body() const { return impl_->body(); }
std::unordered_map<std::string, std::string> HttpResponse::headers() const {
return impl_->headers();
}
namespace {
/// \brief Default error type for unparseable REST responses.
constexpr std::string_view kRestExceptionType = "RESTException";
/// \brief Merges global default headers with request-specific headers.
///
/// Combines the global headers derived from RestCatalogProperties with the headers
/// passed in the specific request. Request-specific headers have higher priority
/// and will override global defaults if the keys conflict (e.g., overriding
/// the default "Content-Type").
cpr::Header MergeHeaders(const std::unordered_map<std::string, std::string>& defaults,
const std::unordered_map<std::string, std::string>& overrides) {
cpr::Header combined_headers = {defaults.begin(), defaults.end()};
for (const auto& [key, val] : overrides) {
combined_headers.insert_or_assign(key, val);
}
return combined_headers;
}
/// \brief Converts a map of string key-value pairs to cpr::Parameters.
cpr::Parameters GetParameters(
const std::unordered_map<std::string, std::string>& params) {
cpr::Parameters cpr_params;
for (const auto& [key, val] : params) {
cpr_params.Add({key, val});
}
return cpr_params;
}
/// \brief Checks if the HTTP status code indicates a successful response.
bool IsSuccessful(int32_t status_code) {
return status_code == 200 // OK
|| status_code == 202 // Accepted
|| status_code == 204 // No Content
|| status_code == 304; // Not Modified
}
/// \brief Builds a default ErrorResponse when the response body cannot be parsed.
ErrorResponse BuildDefaultErrorResponse(const cpr::Response& response) {
return {
.code = static_cast<uint32_t>(response.status_code),
.type = std::string(kRestExceptionType),
.message = !response.reason.empty() ? response.reason
: GetStandardReasonPhrase(response.status_code),
};
}
/// \brief Tries to parse the response body as an ErrorResponse.
Result<ErrorResponse> TryParseErrorResponse(const std::string& text) {
if (text.empty()) {
return InvalidArgument("Empty response body");
}
ICEBERG_ASSIGN_OR_RAISE(auto json_result, FromJsonString(text));
ICEBERG_ASSIGN_OR_RAISE(auto error_result, ErrorResponseFromJson(json_result));
return error_result;
}
/// \brief Handles failure responses by invoking the provided error handler.
Status HandleFailureResponse(const cpr::Response& response,
const ErrorHandler& error_handler) {
if (IsSuccessful(response.status_code)) {
return {};
}
auto parse_result = TryParseErrorResponse(response.text);
const ErrorResponse final_error =
parse_result.value_or(BuildDefaultErrorResponse(response));
return error_handler.Accept(final_error);
}
} // namespace
HttpClient::HttpClient(std::unordered_map<std::string, std::string> default_headers)
: default_headers_{std::move(default_headers)},
connection_pool_{std::make_unique<cpr::ConnectionPool>()} {
// Set default Content-Type for all requests (including GET/HEAD/DELETE).
// Many systems require that content type is set regardless and will fail,
// even on an empty bodied request.
default_headers_[kHeaderContentType] = kMimeTypeApplicationJson;
default_headers_[kHeaderUserAgent] = kUserAgent;
}
HttpClient::~HttpClient() = default;
Result<HttpResponse> HttpClient::Get(
const std::string& path, const std::unordered_map<std::string, std::string>& params,
const std::unordered_map<std::string, std::string>& headers,
const ErrorHandler& error_handler) {
auto final_headers = MergeHeaders(default_headers_, headers);
cpr::Response response =
cpr::Get(cpr::Url{path}, GetParameters(params), final_headers, *connection_pool_);
ICEBERG_RETURN_UNEXPECTED(HandleFailureResponse(response, error_handler));
HttpResponse http_response;
http_response.impl_ = std::make_unique<HttpResponse::Impl>(std::move(response));
return http_response;
}
Result<HttpResponse> HttpClient::Post(
const std::string& path, const std::string& body,
const std::unordered_map<std::string, std::string>& headers,
const ErrorHandler& error_handler) {
auto final_headers = MergeHeaders(default_headers_, headers);
cpr::Response response =
cpr::Post(cpr::Url{path}, cpr::Body{body}, final_headers, *connection_pool_);
ICEBERG_RETURN_UNEXPECTED(HandleFailureResponse(response, error_handler));
HttpResponse http_response;
http_response.impl_ = std::make_unique<HttpResponse::Impl>(std::move(response));
return http_response;
}
Result<HttpResponse> HttpClient::PostForm(
const std::string& path,
const std::unordered_map<std::string, std::string>& form_data,
const std::unordered_map<std::string, std::string>& headers,
const ErrorHandler& error_handler) {
auto final_headers = MergeHeaders(default_headers_, headers);
final_headers.insert_or_assign(kHeaderContentType, kMimeTypeFormUrlEncoded);
std::vector<cpr::Pair> pair_list;
pair_list.reserve(form_data.size());
for (const auto& [key, val] : form_data) {
pair_list.emplace_back(key, val);
}
cpr::Response response =
cpr::Post(cpr::Url{path}, cpr::Payload(pair_list.begin(), pair_list.end()),
final_headers, *connection_pool_);
ICEBERG_RETURN_UNEXPECTED(HandleFailureResponse(response, error_handler));
HttpResponse http_response;
http_response.impl_ = std::make_unique<HttpResponse::Impl>(std::move(response));
return http_response;
}
Result<HttpResponse> HttpClient::Head(
const std::string& path, const std::unordered_map<std::string, std::string>& headers,
const ErrorHandler& error_handler) {
auto final_headers = MergeHeaders(default_headers_, headers);
cpr::Response response = cpr::Head(cpr::Url{path}, final_headers, *connection_pool_);
ICEBERG_RETURN_UNEXPECTED(HandleFailureResponse(response, error_handler));
HttpResponse http_response;
http_response.impl_ = std::make_unique<HttpResponse::Impl>(std::move(response));
return http_response;
}
Result<HttpResponse> HttpClient::Delete(
const std::string& path, const std::unordered_map<std::string, std::string>& params,
const std::unordered_map<std::string, std::string>& headers,
const ErrorHandler& error_handler) {
auto final_headers = MergeHeaders(default_headers_, headers);
cpr::Response response = cpr::Delete(cpr::Url{path}, GetParameters(params),
final_headers, *connection_pool_);
ICEBERG_RETURN_UNEXPECTED(HandleFailureResponse(response, error_handler));
HttpResponse http_response;
http_response.impl_ = std::make_unique<HttpResponse::Impl>(std::move(response));
return http_response;
}
} // namespace iceberg::rest