|
| 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 <set> |
| 23 | +#include <string> |
| 24 | +#include <string_view> |
| 25 | + |
| 26 | +#include "iceberg/catalog/rest/iceberg_rest_export.h" |
| 27 | +#include "iceberg/result.h" |
| 28 | + |
| 29 | +/// \file iceberg/catalog/rest/endpoint.h |
| 30 | +/// Endpoint definitions for Iceberg REST API operations. |
| 31 | + |
| 32 | +namespace iceberg::rest { |
| 33 | + |
| 34 | +/// \brief HTTP method enumeration. |
| 35 | +enum class HttpMethod : uint8_t { GET, POST, PUT, DELETE, HEAD }; |
| 36 | + |
| 37 | +/// \brief Convert HttpMethod to string representation. |
| 38 | +constexpr std::string_view ToString(HttpMethod method); |
| 39 | + |
| 40 | +/// \brief An Endpoint is an immutable value object identifying a specific REST API |
| 41 | +/// operation. It consists of: |
| 42 | +/// - HTTP method (GET, POST, DELETE, etc.) |
| 43 | +/// - Path template (e.g., "/v1/{prefix}/namespaces/{namespace}") |
| 44 | +class ICEBERG_REST_EXPORT Endpoint { |
| 45 | + public: |
| 46 | + /// \brief Create an endpoint with method and path template. |
| 47 | + /// |
| 48 | + /// \param method HTTP method (GET, POST, etc.) |
| 49 | + /// \param path_template Path template with placeholders (e.g., "/v1/{prefix}/tables") |
| 50 | + /// \return Endpoint instance or error if invalid |
| 51 | + static Result<Endpoint> Create(HttpMethod method, std::string path_template); |
| 52 | + |
| 53 | + /// \brief Parse endpoint from string representation. |
| 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_template() const { return path_template_; } |
| 64 | + |
| 65 | + /// \brief Serialize to "METHOD /path" format. |
| 66 | + std::string ToString() const; |
| 67 | + |
| 68 | + /// \brief Equality comparison operator. |
| 69 | + constexpr bool operator==(const Endpoint& other) const { |
| 70 | + return method_ == other.method_ && path_template_ == other.path_template_; |
| 71 | + } |
| 72 | + |
| 73 | + /// \brief Three-way comparison operator (C++20). |
| 74 | + constexpr auto operator<=>(const Endpoint& other) const { |
| 75 | + if (auto cmp = method_ <=> other.method_; cmp != 0) { |
| 76 | + return cmp; |
| 77 | + } |
| 78 | + return path_template_ <=> other.path_template_; |
| 79 | + } |
| 80 | + |
| 81 | + // Namespace endpoints |
| 82 | + static constexpr Endpoint ListNamespaces() { |
| 83 | + return {HttpMethod::GET, "/v1/{prefix}/namespaces"}; |
| 84 | + } |
| 85 | + static constexpr Endpoint GetNamespaceProperties() { |
| 86 | + return {HttpMethod::GET, "/v1/{prefix}/namespaces/{namespace}"}; |
| 87 | + } |
| 88 | + static constexpr Endpoint NamespaceExists() { |
| 89 | + return {HttpMethod::HEAD, "/v1/{prefix}/namespaces/{namespace}"}; |
| 90 | + } |
| 91 | + static constexpr Endpoint CreateNamespace() { |
| 92 | + return {HttpMethod::POST, "/v1/{prefix}/namespaces"}; |
| 93 | + } |
| 94 | + static constexpr Endpoint UpdateNamespace() { |
| 95 | + return {HttpMethod::POST, "/v1/{prefix}/namespaces/{namespace}/properties"}; |
| 96 | + } |
| 97 | + static constexpr Endpoint DropNamespace() { |
| 98 | + return {HttpMethod::DELETE, "/v1/{prefix}/namespaces/{namespace}"}; |
| 99 | + } |
| 100 | + |
| 101 | + // Table endpoints |
| 102 | + static constexpr Endpoint ListTables() { |
| 103 | + return {HttpMethod::GET, "/v1/{prefix}/namespaces/{namespace}/tables"}; |
| 104 | + } |
| 105 | + static constexpr Endpoint LoadTable() { |
| 106 | + return {HttpMethod::GET, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 107 | + } |
| 108 | + static constexpr Endpoint TableExists() { |
| 109 | + return {HttpMethod::HEAD, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 110 | + } |
| 111 | + static constexpr Endpoint CreateTable() { |
| 112 | + return {HttpMethod::POST, "/v1/{prefix}/namespaces/{namespace}/tables"}; |
| 113 | + } |
| 114 | + static constexpr Endpoint UpdateTable() { |
| 115 | + return {HttpMethod::POST, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 116 | + } |
| 117 | + static constexpr Endpoint DeleteTable() { |
| 118 | + return {HttpMethod::DELETE, "/v1/{prefix}/namespaces/{namespace}/tables/{table}"}; |
| 119 | + } |
| 120 | + static constexpr Endpoint RenameTable() { |
| 121 | + return {HttpMethod::POST, "/v1/{prefix}/tables/rename"}; |
| 122 | + } |
| 123 | + static constexpr Endpoint RegisterTable() { |
| 124 | + return {HttpMethod::POST, "/v1/{prefix}/namespaces/{namespace}/register"}; |
| 125 | + } |
| 126 | + static constexpr Endpoint ReportMetrics() { |
| 127 | + return {HttpMethod::POST, |
| 128 | + "/v1/{prefix}/namespaces/{namespace}/tables/{table}/metrics"}; |
| 129 | + } |
| 130 | + static constexpr Endpoint TableCredentials() { |
| 131 | + return {HttpMethod::GET, |
| 132 | + "/v1/{prefix}/namespaces/{namespace}/tables/{table}/credentials"}; |
| 133 | + } |
| 134 | + |
| 135 | + // Transaction endpoints |
| 136 | + static constexpr Endpoint CommitTransaction() { |
| 137 | + return {HttpMethod::POST, "/v1/{prefix}/transactions/commit"}; |
| 138 | + } |
| 139 | + |
| 140 | + private: |
| 141 | + Endpoint(HttpMethod method, std::string_view path_template) |
| 142 | + : method_(method), path_template_(path_template) {} |
| 143 | + |
| 144 | + HttpMethod method_; |
| 145 | + std::string path_template_; |
| 146 | +}; |
| 147 | + |
| 148 | +} // namespace iceberg::rest |
0 commit comments