|
41 | 41 |
|
42 | 42 | namespace iceberg::rest { |
43 | 43 |
|
44 | | -Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { |
45 | | - // Create ResourcePaths and validate URI |
46 | | - ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); |
47 | | - |
| 44 | +Result<std::unique_ptr<RestCatalogConfig>> RestCatalog::FetchAndMergeConfig( |
| 45 | + const RestCatalogConfig& config, const ResourcePaths& paths) { |
| 46 | + // Fetch server configuration |
48 | 47 | auto tmp_client = std::make_unique<HttpClient>(config); |
49 | | - const std::string endpoint = paths->Config(); |
50 | | - ICEBERG_ASSIGN_OR_RAISE(const HttpResponse& response, |
| 48 | + const std::string endpoint = paths.Config(); |
| 49 | + ICEBERG_ASSIGN_OR_RAISE(const auto response, |
51 | 50 | tmp_client->Get(endpoint, {}, {}, DefaultErrorHandler())); |
52 | 51 | ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.body())); |
53 | 52 | ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); |
| 53 | + |
54 | 54 | // Merge server config into client config, server config overrides > client config |
55 | 55 | // properties > server config defaults |
56 | | - auto final_props = std::move(server_config.defaults); |
57 | | - for (const auto& kv : config.configs()) { |
58 | | - final_props.insert_or_assign(kv.first, kv.second); |
59 | | - } |
| 56 | + auto final_props = |
| 57 | + MergeConfigs(server_config.defaults, config.configs(), server_config.overrides); |
| 58 | + return RestCatalogConfig::FromMap(final_props); |
| 59 | +} |
| 60 | + |
| 61 | +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { |
| 62 | + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); |
| 63 | + // Fetch and merge server configuration |
| 64 | + ICEBERG_ASSIGN_OR_RAISE(auto final_config, FetchAndMergeConfig(config, *paths)); |
60 | 65 |
|
61 | | - for (const auto& kv : server_config.overrides) { |
62 | | - final_props.insert_or_assign(kv.first, kv.second); |
63 | | - } |
64 | | - auto final_config = RestCatalogConfig::FromMap(final_props); |
65 | 66 | auto client = std::make_unique<HttpClient>(*final_config); |
66 | 67 | ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); |
67 | | - return std::unique_ptr<RestCatalog>(new RestCatalog( |
68 | | - std::move(final_config), std::move(client), std::move(*final_paths))); |
| 68 | + |
| 69 | + std::string catalog_name = final_config->Get(RestCatalogConfig::kName); |
| 70 | + return std::unique_ptr<RestCatalog>( |
| 71 | + new RestCatalog(std::move(final_config), std::move(client), std::move(*final_paths), |
| 72 | + std::move(catalog_name))); |
69 | 73 | } |
70 | 74 |
|
71 | 75 | RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, |
72 | | - std::unique_ptr<HttpClient> client, ResourcePaths paths) |
73 | | - : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} |
74 | | - |
75 | | -std::string_view RestCatalog::name() const { |
76 | | - auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); |
77 | | - if (it == config_->configs().end() || it->second.empty()) { |
78 | | - return {""}; |
79 | | - } |
80 | | - return std::string_view(it->second); |
81 | | -} |
| 76 | + std::unique_ptr<HttpClient> client, ResourcePaths paths, |
| 77 | + std::string name) |
| 78 | + : config_(std::move(config)), |
| 79 | + client_(std::move(client)), |
| 80 | + paths_(std::move(paths)), |
| 81 | + name_(std::move(name)) {} |
| 82 | + |
| 83 | +std::string_view RestCatalog::name() const { return name_; } |
82 | 84 |
|
83 | 85 | Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { |
84 | 86 | const std::string endpoint = paths_.Namespaces(); |
|
0 commit comments