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"
@@ -146,11 +147,21 @@ HttpClient::HttpClient(std::unordered_map<std::string, std::string> default_head
146147
147148HttpClient::~HttpClient () = default ;
148149
150+ void HttpClient::SetAuthSession (auth::AuthSession* session) { session_ = session; }
151+
152+ Result<std::unordered_map<std::string, std::string>> HttpClient::AuthHeaders () {
153+ std::unordered_map<std::string, std::string> headers;
154+ if (session_) {
155+ ICEBERG_RETURN_UNEXPECTED (session_->Authenticate (headers));
156+ }
157+ return headers;
158+ }
159+
149160Result<HttpResponse> HttpClient::Get (
150161 const std::string& path, const std::unordered_map<std::string, std::string>& params,
151- const std::unordered_map<std::string, std::string>& headers,
152162 const ErrorHandler& error_handler) {
153- auto final_headers = MergeHeaders (default_headers_, headers);
163+ ICEBERG_ASSIGN_OR_RAISE (auto auth_headers, AuthHeaders ());
164+ auto final_headers = MergeHeaders (default_headers_, auth_headers);
154165 cpr::Response response =
155166 cpr::Get (cpr::Url{path}, GetParameters (params), final_headers, *connection_pool_);
156167
@@ -160,11 +171,10 @@ Result<HttpResponse> HttpClient::Get(
160171 return http_response;
161172}
162173
163- Result<HttpResponse> HttpClient::Post (
164- const std::string& path, const std::string& body,
165- const std::unordered_map<std::string, std::string>& headers,
166- const ErrorHandler& error_handler) {
167- auto final_headers = MergeHeaders (default_headers_, headers);
174+ Result<HttpResponse> HttpClient::Post (const std::string& path, const std::string& body,
175+ const ErrorHandler& error_handler) {
176+ ICEBERG_ASSIGN_OR_RAISE (auto auth_headers, AuthHeaders ());
177+ auto final_headers = MergeHeaders (default_headers_, auth_headers);
168178 cpr::Response response =
169179 cpr::Post (cpr::Url{path}, cpr::Body{body}, final_headers, *connection_pool_);
170180
@@ -177,9 +187,9 @@ Result<HttpResponse> HttpClient::Post(
177187Result<HttpResponse> HttpClient::PostForm (
178188 const std::string& path,
179189 const std::unordered_map<std::string, std::string>& form_data,
180- const std::unordered_map<std::string, std::string>& headers,
181190 const ErrorHandler& error_handler) {
182- auto final_headers = MergeHeaders (default_headers_, headers);
191+ ICEBERG_ASSIGN_OR_RAISE (auto auth_headers, AuthHeaders ());
192+ auto final_headers = MergeHeaders (default_headers_, auth_headers);
183193 final_headers.insert_or_assign (kHeaderContentType , kMimeTypeFormUrlEncoded );
184194 std::vector<cpr::Pair> pair_list;
185195 pair_list.reserve (form_data.size ());
@@ -196,10 +206,10 @@ Result<HttpResponse> HttpClient::PostForm(
196206 return http_response;
197207}
198208
199- Result<HttpResponse> HttpClient::Head (
200- 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 );
209+ Result<HttpResponse> HttpClient::Head (const std::string& path,
210+ const ErrorHandler& error_handler) {
211+ ICEBERG_ASSIGN_OR_RAISE ( auto auth_headers, AuthHeaders ());
212+ auto final_headers = MergeHeaders (default_headers_, auth_headers );
203213 cpr::Response response = cpr::Head (cpr::Url{path}, final_headers, *connection_pool_);
204214
205215 ICEBERG_RETURN_UNEXPECTED (HandleFailureResponse (response, error_handler));
@@ -210,9 +220,9 @@ Result<HttpResponse> HttpClient::Head(
210220
211221Result<HttpResponse> HttpClient::Delete (
212222 const std::string& path, const std::unordered_map<std::string, std::string>& params,
213- const std::unordered_map<std::string, std::string>& headers,
214223 const ErrorHandler& error_handler) {
215- auto final_headers = MergeHeaders (default_headers_, headers);
224+ ICEBERG_ASSIGN_OR_RAISE (auto auth_headers, AuthHeaders ());
225+ auto final_headers = MergeHeaders (default_headers_, auth_headers);
216226 cpr::Response response = cpr::Delete (cpr::Url{path}, GetParameters (params),
217227 final_headers, *connection_pool_);
218228
0 commit comments