2222#include < cpr/cpr.h>
2323#include < nlohmann/json.hpp>
2424
25+ #include " iceberg/catalog/rest/auth/auth_session.h"
2526#include " iceberg/catalog/rest/constant.h"
2627#include " iceberg/catalog/rest/error_handlers.h"
2728#include " iceberg/catalog/rest/json_serde_internal.h"
@@ -67,19 +68,17 @@ namespace {
6768// / \brief Default error type for unparseable REST responses.
6869constexpr std::string_view kRestExceptionType = " RESTException" ;
6970
70- // / \brief Merges global default headers with request-specific headers.
71- // /
72- // / Combines the global headers derived from RestCatalogProperties with the headers
73- // / passed in the specific request. Request-specific headers have higher priority
74- // / and will override global defaults if the keys conflict (e.g., overriding
75- // / the default "Content-Type").
76- cpr::Header MergeHeaders (const std::unordered_map<std::string, std::string>& defaults,
77- const std::unordered_map<std::string, std::string>& overrides) {
78- cpr::Header combined_headers = {defaults.begin (), defaults.end ()};
79- for (const auto & [key, val] : overrides) {
80- combined_headers.insert_or_assign (key, val);
71+ // / \brief Prepare headers for an HTTP request.
72+ Result<cpr::Header> BuildHeaders (
73+ const std::unordered_map<std::string, std::string>& request_headers,
74+ const std::unordered_map<std::string, std::string>& default_headers,
75+ auth::AuthSession& session) {
76+ std::unordered_map<std::string, std::string> headers (default_headers);
77+ for (const auto & [key, val] : request_headers) {
78+ headers.emplace (key, val);
8179 }
82- return combined_headers;
80+ ICEBERG_RETURN_UNEXPECTED (session.Authenticate (headers));
81+ return cpr::Header (headers.begin (), headers.end ());
8382}
8483
8584// / \brief Converts a map of string key-value pairs to cpr::Parameters.
@@ -149,10 +148,11 @@ HttpClient::~HttpClient() = default;
149148Result<HttpResponse> HttpClient::Get (
150149 const std::string& path, const std::unordered_map<std::string, std::string>& params,
151150 const std::unordered_map<std::string, std::string>& headers,
152- const ErrorHandler& error_handler) {
153- auto final_headers = MergeHeaders (default_headers_, headers);
151+ const ErrorHandler& error_handler, auth::AuthSession& session) {
152+ ICEBERG_ASSIGN_OR_RAISE (auto all_headers,
153+ BuildHeaders (headers, default_headers_, session));
154154 cpr::Response response =
155- cpr::Get (cpr::Url{path}, GetParameters (params), final_headers , *connection_pool_);
155+ cpr::Get (cpr::Url{path}, GetParameters (params), all_headers , *connection_pool_);
156156
157157 ICEBERG_RETURN_UNEXPECTED (HandleFailureResponse (response, error_handler));
158158 HttpResponse http_response;
@@ -163,10 +163,11 @@ Result<HttpResponse> HttpClient::Get(
163163Result<HttpResponse> HttpClient::Post (
164164 const std::string& path, const std::string& body,
165165 const std::unordered_map<std::string, std::string>& headers,
166- const ErrorHandler& error_handler) {
167- auto final_headers = MergeHeaders (default_headers_, headers);
166+ const ErrorHandler& error_handler, auth::AuthSession& session) {
167+ ICEBERG_ASSIGN_OR_RAISE (auto all_headers,
168+ BuildHeaders (headers, default_headers_, session));
168169 cpr::Response response =
169- cpr::Post (cpr::Url{path}, cpr::Body{body}, final_headers , *connection_pool_);
170+ cpr::Post (cpr::Url{path}, cpr::Body{body}, all_headers , *connection_pool_);
170171
171172 ICEBERG_RETURN_UNEXPECTED (HandleFailureResponse (response, error_handler));
172173 HttpResponse http_response;
@@ -178,17 +179,19 @@ Result<HttpResponse> HttpClient::PostForm(
178179 const std::string& path,
179180 const std::unordered_map<std::string, std::string>& form_data,
180181 const std::unordered_map<std::string, std::string>& headers,
181- const ErrorHandler& error_handler) {
182- auto final_headers = MergeHeaders (default_headers_, headers);
183- final_headers.insert_or_assign (kHeaderContentType , kMimeTypeFormUrlEncoded );
182+ const ErrorHandler& error_handler, auth::AuthSession& session) {
183+ std::unordered_map<std::string, std::string> form_headers (headers);
184+ form_headers.insert_or_assign (kHeaderContentType , kMimeTypeFormUrlEncoded );
185+ ICEBERG_ASSIGN_OR_RAISE (auto all_headers,
186+ BuildHeaders (form_headers, default_headers_, session));
184187 std::vector<cpr::Pair> pair_list;
185188 pair_list.reserve (form_data.size ());
186189 for (const auto & [key, val] : form_data) {
187190 pair_list.emplace_back (key, val);
188191 }
189192 cpr::Response response =
190193 cpr::Post (cpr::Url{path}, cpr::Payload (pair_list.begin (), pair_list.end ()),
191- final_headers , *connection_pool_);
194+ all_headers , *connection_pool_);
192195
193196 ICEBERG_RETURN_UNEXPECTED (HandleFailureResponse (response, error_handler));
194197 HttpResponse http_response;
@@ -198,9 +201,10 @@ Result<HttpResponse> HttpClient::PostForm(
198201
199202Result<HttpResponse> HttpClient::Head (
200203 const std::string& path, const std::unordered_map<std::string, std::string>& headers,
201- const ErrorHandler& error_handler) {
202- auto final_headers = MergeHeaders (default_headers_, headers);
203- cpr::Response response = cpr::Head (cpr::Url{path}, final_headers, *connection_pool_);
204+ const ErrorHandler& error_handler, auth::AuthSession& session) {
205+ ICEBERG_ASSIGN_OR_RAISE (auto all_headers,
206+ BuildHeaders (headers, default_headers_, session));
207+ cpr::Response response = cpr::Head (cpr::Url{path}, all_headers, *connection_pool_);
204208
205209 ICEBERG_RETURN_UNEXPECTED (HandleFailureResponse (response, error_handler));
206210 HttpResponse http_response;
@@ -211,10 +215,11 @@ Result<HttpResponse> HttpClient::Head(
211215Result<HttpResponse> HttpClient::Delete (
212216 const std::string& path, const std::unordered_map<std::string, std::string>& params,
213217 const std::unordered_map<std::string, std::string>& headers,
214- const ErrorHandler& error_handler) {
215- auto final_headers = MergeHeaders (default_headers_, headers);
216- cpr::Response response = cpr::Delete (cpr::Url{path}, GetParameters (params),
217- final_headers, *connection_pool_);
218+ const ErrorHandler& error_handler, auth::AuthSession& session) {
219+ ICEBERG_ASSIGN_OR_RAISE (auto all_headers,
220+ BuildHeaders (headers, default_headers_, session));
221+ cpr::Response response =
222+ cpr::Delete (cpr::Url{path}, GetParameters (params), all_headers, *connection_pool_);
218223
219224 ICEBERG_RETURN_UNEXPECTED (HandleFailureResponse (response, error_handler));
220225 HttpResponse http_response;
0 commit comments