|
| 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 | +#pragma once |
| 21 | + |
| 22 | +#include <string> |
| 23 | +#include <string_view> |
| 24 | + |
| 25 | +#include "iceberg/catalog/rest/iceberg_rest_export.h" |
| 26 | +#include "iceberg/result.h" |
| 27 | + |
| 28 | +/// \file iceberg/catalog/rest/endpoint.h |
| 29 | +/// Endpoint definitions for Iceberg REST API operations. |
| 30 | + |
| 31 | +namespace iceberg::rest { |
| 32 | + |
| 33 | +/// \brief HTTP method enumeration. |
| 34 | +enum class HttpMethod : uint8_t { kGet, kPost, kPut, kDelete, kHead }; |
| 35 | + |
| 36 | +/// \brief Convert HttpMethod to string representation. |
| 37 | +constexpr std::string_view ToString(HttpMethod method); |
| 38 | + |
| 39 | +/// \brief An Endpoint is an immutable value object identifying a specific REST API |
| 40 | +/// operation. It consists of: |
| 41 | +/// - HTTP method (GET, POST, DELETE, etc.) |
| 42 | +/// - Path template (e.g., "/v1/{prefix}/namespaces/{namespace}") |
| 43 | +class ICEBERG_REST_EXPORT Endpoint { |
| 44 | + public: |
| 45 | + /// \brief Make an endpoint with method and path template. |
| 46 | + /// |
| 47 | + /// \param method HTTP method (GET, POST, etc.) |
| 48 | + /// \param path Path template with placeholders (e.g., "/v1/{prefix}/tables") |
| 49 | + /// \return Endpoint instance or error if invalid |
| 50 | + static Result<Endpoint> Make(HttpMethod method, std::string_view path); |
| 51 | + |
| 52 | + /// \brief Parse endpoint from string representation. "METHOD" have to be all |
| 53 | + /// upper-cased. |
| 54 | + /// |
| 55 | + /// \param str String in format "METHOD /path/template" (e.g., "GET /v1/namespaces") |
| 56 | + /// \return Endpoint instance or error if malformed. |
| 57 | + static Result<Endpoint> FromString(std::string_view str); |
| 58 | + |
| 59 | + /// \brief Get the HTTP method. |
| 60 | + constexpr HttpMethod method() const { return method_; } |
| 61 | + |
| 62 | + /// \brief Get the path template. |
| 63 | + std::string_view path() const { return path_; } |
| 64 | + |
| 65 | + /// \brief Serialize to "METHOD /path" format. |
| 66 | + std::string ToString() const; |
| 67 | + |
| 68 | + constexpr bool operator==(const Endpoint& other) const { |
| 69 | + return method_ == other.method_ && path_ == other.path_; |
| 70 | + } |
| 71 | + |
| 72 | + // Namespace endpoints |
| 73 | + static Endpoint ListNamespaces() { |
| 74 | + return {HttpMethod::kGet, "/v1/{prefix}/namespaces"}; |
| 75 | + } |
| 76 | + static Endpoint GetNamespaceProperties() { |
| 77 | + return {HttpMethod::kGet, "/v1/{prefix}/namespaces/{namespace}"}; |
| 78 | + } |
| 79 | + static Endpoint NamespaceExists() { |
| 80 | + return {HttpMethod::kHead, "/v1/{prefix}/namespaces/{namespace}"}; |
| 81 | + } |
| 82 | + static Endpoint CreateNamespace() { |
| 83 | + return {HttpMethod::kPost, "/v1/{prefix}/namespaces"}; |
| 84 | + } |
| 85 | + static Endpoint UpdateNamespace() { |
| 86 | + return {HttpMethod::kPost, "/v1/{prefix}/namespaces/{namespace}/properties"}; |
| 87 | + } |
| 88 | + static Endpoint DropNamespace() { |
| 89 | + return {HttpMethod::kDelete, "/v1/{prefix}/namespaces/{namespace}"}; |
| 90 | + } |
| 91 | + |
| 92 | + // Table endpoints |
| 93 | + static Endpoint ListTables() { |
| 94 | + return {HttpMethod::kGet, "/v1/{prefix}/namespaces/{namespace}/tables"}; |
| 95 | + } |
| 96 | + static Endpoint LoadTable() { |
| 97 | + return {HttpMethod::kGet, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 98 | + } |
| 99 | + static Endpoint TableExists() { |
| 100 | + return {HttpMethod::kHead, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 101 | + } |
| 102 | + static Endpoint CreateTable() { |
| 103 | + return {HttpMethod::kPost, "/v1/{prefix}/namespaces/{namespace}/tables"}; |
| 104 | + } |
| 105 | + static Endpoint UpdateTable() { |
| 106 | + return {HttpMethod::kPost, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 107 | + } |
| 108 | + static Endpoint DeleteTable() { |
| 109 | + return {HttpMethod::kDelete, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 110 | + } |
| 111 | + static Endpoint RenameTable() { |
| 112 | + return {HttpMethod::kPost, "/v1/{prefix}/tables/rename"}; |
| 113 | + } |
| 114 | + static Endpoint RegisterTable() { |
| 115 | + return {HttpMethod::kPost, "/v1/{prefix}/namespaces/{namespace}/register"}; |
| 116 | + } |
| 117 | + static Endpoint ReportMetrics() { |
| 118 | + return {HttpMethod::kPost, |
| 119 | + "/v1/{prefix}/namespaces/{namespace}/tables/{table}/metrics"}; |
| 120 | + } |
| 121 | + static Endpoint TableCredentials() { |
| 122 | + return {HttpMethod::kGet, |
| 123 | + "/v1/{prefix}/namespaces/{namespace}/tables/{table}/credentials"}; |
| 124 | + } |
| 125 | + |
| 126 | + // Transaction endpoints |
| 127 | + static Endpoint CommitTransaction() { |
| 128 | + return {HttpMethod::kPost, "/v1/{prefix}/transactions/commit"}; |
| 129 | + } |
| 130 | + |
| 131 | + private: |
| 132 | + Endpoint(HttpMethod method, std::string_view path) : method_(method), path_(path) {} |
| 133 | + |
| 134 | + HttpMethod method_; |
| 135 | + std::string path_; |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace iceberg::rest |
| 139 | + |
| 140 | +// Specialize std::hash for Endpoint |
| 141 | +namespace std { |
| 142 | +template <> |
| 143 | +struct hash<iceberg::rest::Endpoint> { |
| 144 | + std::size_t operator()(const iceberg::rest::Endpoint& endpoint) const noexcept { |
| 145 | + std::size_t h1 = std::hash<int32_t>{}(static_cast<int32_t>(endpoint.method())); |
| 146 | + std::size_t h2 = std::hash<std::string_view>{}(endpoint.path()); |
| 147 | + return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2)); |
| 148 | + } |
| 149 | +}; |
| 150 | +} // namespace std |
0 commit comments