|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "client.h" |
| 21 | + |
| 22 | +#include <cpr/cpr.h> |
| 23 | + |
| 24 | +namespace iceberg::catalog::rest { |
| 25 | + |
| 26 | +// PIMPL (Pointer to Implementation) 模式的实现 |
| 27 | +struct HttpClient::Impl { |
| 28 | + cpr::Url base_uri_; |
| 29 | + cpr::Header common_headers_; |
| 30 | + |
| 31 | + Impl(const std::string& base_uri, const HttpHeaders& common_headers) |
| 32 | + : base_uri_{base_uri}, |
| 33 | + common_headers_{common_headers.begin(), common_headers.end()} {} |
| 34 | + |
| 35 | + // 这个模板函数是 Rust 版本 `query` 和 `execute` 方法的核心逻辑的 C++ 对等实现。 |
| 36 | + // - `SuccessType` 对应 Rust 的 `R` (如果是 void,则对应 `execute` 的 `()`)。 |
| 37 | + // - `SuccessCode` 对应 Rust 的 `SUCCESS_CODE`。 |
| 38 | + template <typename SuccessType, int SuccessCode> |
| 39 | + SuccessType process_response(const cpr::Response& resp) { |
| 40 | + if (resp.status_code == SuccessCode) { |
| 41 | + // 对应 Rust 的 `if resp.status().as_u16() == SUCCESS_CODE` 分支 |
| 42 | + |
| 43 | + // `if constexpr` 允许我们在编译时根据 SuccessType 是否为 void |
| 44 | + // 来选择不同的代码路径。 这完美地模拟了 `query` (返回 body) 和 `execute` (返回 |
| 45 | + // void) 的区别。 |
| 46 | + if constexpr (std::is_same_v<SuccessType, void>) { |
| 47 | + return; // 成功且不需要返回 body |
| 48 | + } else { |
| 49 | + // 尝试解析成功的响应体 |
| 50 | + nlohmann::json result = nlohmann::json::parse(resp.text, false); |
| 51 | + if (result.is_discarded()) { |
| 52 | + // 对应 Rust 中解析成功响应体失败的错误路径 |
| 53 | + throw ResponseParseException("Failed to parse successful response from server!", |
| 54 | + resp.text); |
| 55 | + } |
| 56 | + return result; |
| 57 | + } |
| 58 | + } else { |
| 59 | + // 对应 Rust 的 `else` 分支,处理错误响应 |
| 60 | + nlohmann::json error_payload = nlohmann::json::parse(resp.text, false); |
| 61 | + if (error_payload.is_discarded()) { |
| 62 | + // 对应 Rust 中解析错误响应体失败的错误路径 |
| 63 | + throw ResponseParseException("Failed to parse error response from server!", |
| 64 | + resp.text); |
| 65 | + } |
| 66 | + // 抛出包含详细信息的异常,对应 `Err(e.into())` |
| 67 | + throw ServerErrorException(resp.status_code, resp.text, error_payload); |
| 68 | + } |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +HttpClient::HttpClient(const std::string& base_uri, const HttpHeaders& common_headers) |
| 73 | + : pimpl_(std::make_unique<Impl>(base_uri, common_headers)) {} |
| 74 | + |
| 75 | +HttpClient::~HttpClient() = default; |
| 76 | + |
| 77 | +nlohmann::json HttpClient::Get(const std::string& path, const HttpHeaders& headers) { |
| 78 | + cpr::Header final_headers = pimpl_->common_headers_; |
| 79 | + final_headers.insert(headers.begin(), headers.end()); |
| 80 | + |
| 81 | + cpr::Response r = cpr::Get(pimpl_->base_uri_ + cpr::Url{path}, final_headers); |
| 82 | + |
| 83 | + // 调用通用处理逻辑,期望返回 json,成功状态码为 200 (OK) |
| 84 | + return pimpl_->process_response<nlohmann::json, 200>(r); |
| 85 | +} |
| 86 | + |
| 87 | +nlohmann::json HttpClient::Post(const std::string& path, const HttpHeaders& headers, |
| 88 | + const nlohmann::json& body) { |
| 89 | + cpr::Header final_headers = pimpl_->common_headers_; |
| 90 | + final_headers.insert(headers.begin(), headers.end()); |
| 91 | + // 确保 Content-Type 设置为 application/json |
| 92 | + if (final_headers.find("Content-Type") == final_headers.end()) { |
| 93 | + final_headers["Content-Type"] = "application/json"; |
| 94 | + } |
| 95 | + |
| 96 | + cpr::Response r = cpr::Post(pimpl_->base_uri_ + cpr::Url{path}, cpr::Body{body.dump()}, |
| 97 | + final_headers); |
| 98 | + |
| 99 | + // 调用通用处理逻辑,期望返回 json,成功状态码通常为 201 (Created) 或 200 (OK),这里以 |
| 100 | + // 200 为例 |
| 101 | + return pimpl_->process_response<nlohmann::json, 200>(r); |
| 102 | +} |
| 103 | + |
| 104 | +void HttpClient::Delete(const std::string& path, const HttpHeaders& headers) { |
| 105 | + cpr::Header final_headers = pimpl_->common_headers_; |
| 106 | + final_headers.insert(headers.begin(), headers.end()); |
| 107 | + |
| 108 | + cpr::Response r = cpr::Delete(pimpl_->base_uri_ + cpr::Url{path}, final_headers); |
| 109 | + |
| 110 | + // 调用通用处理逻辑,期望返回 void,成功状态码为 204 (No Content) |
| 111 | + return pimpl_->process_response<void, 204>(r); |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace iceberg::catalog::rest |
0 commit comments