ZENOTME commented on code in PR #78:
URL: https://github.com/apache/iceberg-rust/pull/78#discussion_r1357818399


##########
crates/iceberg/src/catalog/rest.rs:
##########
@@ -0,0 +1,900 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! This module contains rest catalog implementation.
+
+use std::collections::HashMap;
+
+use async_trait::async_trait;
+use reqwest::header::{self, HeaderMap, HeaderName, HeaderValue};
+use reqwest::{Client, Request};
+use serde::de::DeserializeOwned;
+use urlencoding::encode;
+
+use crate::error::Result;
+use crate::table::Table;
+use crate::{
+    Catalog, Error, ErrorKind, Namespace, NamespaceIdent, TableCommit, 
TableCreation, TableIdent,
+};
+
+use self::_serde::{
+    CatalogConfig, ErrorModel, ErrorResponse, ListNamespaceResponse, 
ListTableResponse,
+    NamespaceSerde, RenameTableRequest, NO_CONTENT, OK,
+};
+
+const ICEBERG_REST_SPEC_VERSION: &str = "0.14.1";
+const PATH_V1: &str = "v1";
+
+#[derive(Debug, Builder)]
+pub struct RestCatalogConfig {
+    uri: String,
+    #[builder(default)]
+    warehouse: Option<String>,
+
+    #[builder(default)]
+    props: HashMap<String, String>,
+}
+
+impl RestCatalogConfig {
+    fn config_endpoint(&self) -> String {
+        [&self.uri, PATH_V1, "config"].join("/")
+    }
+
+    fn namespaces_endpoint(&self) -> String {
+        [&self.uri, PATH_V1, "namespaces"].join("/")
+    }
+
+    fn namespace_endpoint(&self, ns: &NamespaceIdent) -> String {
+        [&self.uri, PATH_V1, "namespaces", &ns.encode_in_url()].join("/")
+    }
+
+    fn tables_endpoint(&self, ns: &NamespaceIdent) -> String {
+        [
+            &self.uri,
+            PATH_V1,
+            "namespaces",
+            &ns.encode_in_url(),
+            "tables",
+        ]
+        .join("/")
+    }
+
+    fn rename_table_endpoint(&self) -> String {
+        [&self.uri, PATH_V1, "tables", "rename"].join("/")
+    }
+
+    fn table_endpoint(&self, table: &TableIdent) -> String {
+        [
+            &self.uri,
+            PATH_V1,
+            "namespaces",
+            &table.namespace.encode_in_url(),
+            "tables",
+            encode(&table.name).as_ref(),
+        ]
+        .join("/")
+    }
+
+    fn try_create_rest_client(&self) -> Result<HttpClient> {
+        //TODO: We will add oauth, ssl config, sigv4 later
+        let mut headers = HeaderMap::new();
+        headers.insert(
+            header::CONTENT_TYPE,
+            HeaderValue::from_static("application/json"),
+        );
+        headers.insert(
+            HeaderName::from_static("x-client-version"),
+            HeaderValue::from_static(ICEBERG_REST_SPEC_VERSION),
+        );
+        headers.insert(
+            header::USER_AGENT,
+            HeaderValue::from_str(&format!("iceberg-rs/{}", 
env!("CARGO_PKG_VERSION"))).unwrap(),
+        );
+
+        Ok(HttpClient(
+            Client::builder().default_headers(headers).build()?,
+        ))
+    }
+}
+
+impl NamespaceIdent {
+    /// Returns url encoded format.
+    pub fn encode_in_url(&self) -> String {
+        encode(&self.0.join("\u{1F}")).to_string()
+    }
+}
+
+struct HttpClient(Client);
+
+impl HttpClient {
+    async fn execute<
+        R: DeserializeOwned,
+        E: DeserializeOwned + Into<Error>,
+        const SUCCESS_CODE: u16,
+    >(
+        &self,
+        request: Request,
+    ) -> Result<R> {
+        let resp = self.0.execute(request).await?;
+
+        if resp.status().as_u16() == SUCCESS_CODE {
+            let text = resp.bytes().await?;
+            Ok(serde_json::from_slice::<R>(&text).map_err(|e| {
+                Error::new(
+                    ErrorKind::Unexpected,
+                    "Failed to parse response from rest catalog server!",
+                )
+                .with_context("json", String::from_utf8_lossy(&text))
+                .with_source(e)
+            })?)
+        } else {
+            let text = resp.bytes().await?;
+            let e = serde_json::from_slice::<E>(&text).map_err(|e| {
+                Error::new(
+                    ErrorKind::Unexpected,
+                    "Failed to parse response from rest catalog server!",
+                )
+                .with_context("json", String::from_utf8_lossy(&text))
+                .with_source(e)
+            })?;
+            Err(e.into())
+        }
+    }
+
+    async fn execute2<E: DeserializeOwned + Into<Error>, const SUCCESS_CODE: 
u16>(

Review Comment:
   Is the difference between execute2 and execute is response? Can we give a 
more clear name to distinguish them🤔, such as:
   - query - means that there is response
   - execute - means that there is no response



##########
crates/iceberg/src/catalog/rest.rs:
##########
@@ -0,0 +1,900 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! This module contains rest catalog implementation.
+
+use std::collections::HashMap;
+
+use async_trait::async_trait;
+use reqwest::header::{self, HeaderMap, HeaderName, HeaderValue};
+use reqwest::{Client, Request};
+use serde::de::DeserializeOwned;
+use urlencoding::encode;
+
+use crate::error::Result;
+use crate::table::Table;
+use crate::{
+    Catalog, Error, ErrorKind, Namespace, NamespaceIdent, TableCommit, 
TableCreation, TableIdent,
+};
+
+use self::_serde::{
+    CatalogConfig, ErrorModel, ErrorResponse, ListNamespaceResponse, 
ListTableResponse,
+    NamespaceSerde, RenameTableRequest, NO_CONTENT, OK,
+};
+
+const ICEBERG_REST_SPEC_VERSION: &str = "0.14.1";
+const PATH_V1: &str = "v1";
+
+#[derive(Debug, Builder)]
+pub struct RestCatalogConfig {
+    uri: String,
+    #[builder(default)]
+    warehouse: Option<String>,
+
+    #[builder(default)]
+    props: HashMap<String, String>,
+}
+
+impl RestCatalogConfig {
+    fn config_endpoint(&self) -> String {
+        [&self.uri, PATH_V1, "config"].join("/")
+    }
+
+    fn namespaces_endpoint(&self) -> String {
+        [&self.uri, PATH_V1, "namespaces"].join("/")
+    }
+
+    fn namespace_endpoint(&self, ns: &NamespaceIdent) -> String {
+        [&self.uri, PATH_V1, "namespaces", &ns.encode_in_url()].join("/")
+    }
+
+    fn tables_endpoint(&self, ns: &NamespaceIdent) -> String {
+        [
+            &self.uri,
+            PATH_V1,
+            "namespaces",
+            &ns.encode_in_url(),
+            "tables",
+        ]
+        .join("/")
+    }
+
+    fn rename_table_endpoint(&self) -> String {
+        [&self.uri, PATH_V1, "tables", "rename"].join("/")
+    }
+
+    fn table_endpoint(&self, table: &TableIdent) -> String {
+        [
+            &self.uri,
+            PATH_V1,
+            "namespaces",
+            &table.namespace.encode_in_url(),
+            "tables",
+            encode(&table.name).as_ref(),
+        ]
+        .join("/")
+    }
+
+    fn try_create_rest_client(&self) -> Result<HttpClient> {
+        //TODO: We will add oauth, ssl config, sigv4 later
+        let mut headers = HeaderMap::new();
+        headers.insert(
+            header::CONTENT_TYPE,
+            HeaderValue::from_static("application/json"),
+        );
+        headers.insert(
+            HeaderName::from_static("x-client-version"),
+            HeaderValue::from_static(ICEBERG_REST_SPEC_VERSION),
+        );
+        headers.insert(
+            header::USER_AGENT,
+            HeaderValue::from_str(&format!("iceberg-rs/{}", 
env!("CARGO_PKG_VERSION"))).unwrap(),
+        );
+
+        Ok(HttpClient(
+            Client::builder().default_headers(headers).build()?,
+        ))
+    }
+}
+
+impl NamespaceIdent {
+    /// Returns url encoded format.
+    pub fn encode_in_url(&self) -> String {
+        encode(&self.0.join("\u{1F}")).to_string()
+    }
+}
+
+struct HttpClient(Client);
+
+impl HttpClient {
+    async fn execute<
+        R: DeserializeOwned,
+        E: DeserializeOwned + Into<Error>,
+        const SUCCESS_CODE: u16,
+    >(
+        &self,
+        request: Request,
+    ) -> Result<R> {
+        let resp = self.0.execute(request).await?;
+
+        if resp.status().as_u16() == SUCCESS_CODE {
+            let text = resp.bytes().await?;
+            Ok(serde_json::from_slice::<R>(&text).map_err(|e| {
+                Error::new(
+                    ErrorKind::Unexpected,
+                    "Failed to parse response from rest catalog server!",
+                )
+                .with_context("json", String::from_utf8_lossy(&text))
+                .with_source(e)
+            })?)
+        } else {
+            let text = resp.bytes().await?;
+            let e = serde_json::from_slice::<E>(&text).map_err(|e| {
+                Error::new(
+                    ErrorKind::Unexpected,
+                    "Failed to parse response from rest catalog server!",
+                )
+                .with_context("json", String::from_utf8_lossy(&text))
+                .with_source(e)
+            })?;
+            Err(e.into())
+        }
+    }
+
+    async fn execute2<E: DeserializeOwned + Into<Error>, const SUCCESS_CODE: 
u16>(

Review Comment:
   Is the difference between execute2 and execute is response? Can we give a 
more clear name to distinguish them🤔, such as:
   - query - means that there is response
   - execute - means that there is no response



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to