Skip to content

Commit

Permalink
refactor: pub only necessary items in databend-client. (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
youngsofun authored Nov 29, 2024
1 parent 1e9a2d0 commit 3eb745e
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::collections::BTreeMap;

use anyhow::{anyhow, Result};
use databend_client::auth::SensitiveString;
use databend_client::SensitiveString;
use percent_encoding::{percent_decode_str, utf8_percent_encode, NON_ALPHANUMERIC};

#[derive(Debug, Clone, PartialEq, Default)]
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::{

use anyhow::{anyhow, Result};
use clap::{ArgAction, CommandFactory, Parser, ValueEnum};
use databend_client::auth::SensitiveString;
use databend_client::SensitiveString;
use log::info;
use once_cell::sync::Lazy;

Expand Down Expand Up @@ -373,7 +373,7 @@ pub async fn main() -> Result<()> {
// Exit client if user login failed.
if let Some(error) = err.downcast_ref::<databend_driver::Error>() {
match error {
databend_driver::Error::Api(databend_client::error::Error::AuthFailure(_)) => {
databend_driver::Error::Api(databend_client::Error::AuthFailure(_)) => {
println!("Authenticate failed wrong password user {}", user);
return Ok(());
}
Expand Down
6 changes: 2 additions & 4 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ impl Session {
Ok(version) => version,
Err(err) => {
match err {
databend_driver::Error::Api(
databend_client::error::Error::AuthFailure(_),
) => {
databend_driver::Error::Api(databend_client::Error::AuthFailure(_)) => {
return Err(err.into());
}
databend_driver::Error::Arrow(arrow::error::ArrowError::IpcError(
Expand All @@ -119,7 +117,7 @@ impl Session {
return Err(err.into());
}
}
databend_driver::Error::Api(databend_client::error::Error::Request(
databend_driver::Error::Api(databend_client::Error::Request(
ref resp_err,
)) => {
if resp_err.contains("error sending request for url") {
Expand Down
33 changes: 21 additions & 12 deletions core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ static VERSION: Lazy<String> = Lazy::new(|| {

#[derive(Clone)]
pub struct APIClient {
pub cli: HttpClient,
pub scheme: String,
pub host: String,
pub port: u16,
cli: HttpClient,
scheme: String,
host: String,
port: u16,

endpoint: Url,

Expand Down Expand Up @@ -231,6 +231,14 @@ impl APIClient {
Ok(client)
}

pub fn host(&self) -> &str {
self.host.as_str()
}

pub fn port(&self) -> u16 {
self.port
}

async fn build_client(&mut self, name: Option<String>) -> Result<()> {
let ua = match name {
Some(n) => n,
Expand Down Expand Up @@ -309,7 +317,7 @@ impl APIClient {
uuid::Uuid::new_v4().to_string()
}

pub async fn handle_session(&self, session: &Option<SessionState>) {
async fn handle_session(&self, session: &Option<SessionState>) {
let session = match session {
Some(session) => session,
None => return,
Expand All @@ -333,11 +341,11 @@ impl APIClient {
pub fn set_last_node_id(&self, node_id: String) {
*self.last_node_id.lock() = Some(node_id)
}
pub fn last_node_id(&self) -> Option<String> {
fn last_node_id(&self) -> Option<String> {
self.last_node_id.lock().clone()
}

pub fn handle_warnings(&self, resp: &QueryResponse) {
fn handle_warnings(&self, resp: &QueryResponse) {
if let Some(warnings) = &resp.warnings {
for w in warnings {
warn!(target: "server_warnings", "server warning: {}", w);
Expand Down Expand Up @@ -437,7 +445,8 @@ impl APIClient {
}
}

pub async fn kill_query(&self, query_id: &str, kill_uri: &str) -> Result<()> {
#[allow(dead_code)]
async fn kill_query(&self, query_id: &str, kill_uri: &str) -> Result<()> {
info!("kill query: {}", kill_uri);
let endpoint = self.endpoint.join(kill_uri)?;
let headers = self.make_headers(Some(query_id))?;
Expand All @@ -451,7 +460,7 @@ impl APIClient {
Ok(())
}

pub async fn wait_for_query(&self, resp: QueryResponse) -> Result<QueryResponse> {
async fn wait_for_query(&self, resp: QueryResponse) -> Result<QueryResponse> {
info!("wait for query: {}", resp.id);
let node_id = resp.node_id.clone();
if let Some(node_id) = self.last_node_id() {
Expand Down Expand Up @@ -624,7 +633,7 @@ impl APIClient {
Ok(())
}

pub async fn login(&mut self) -> Result<()> {
async fn login(&mut self) -> Result<()> {
let endpoint = self.endpoint.join("/v1/session/login")?;
let headers = self.make_headers(None)?;
let body = LoginRequest::from(&*self.session_state.lock());
Expand Down Expand Up @@ -661,7 +670,7 @@ impl APIClient {
Ok(())
}

pub fn build_log_out_request(&mut self) -> Result<Request> {
fn build_log_out_request(&mut self) -> Result<Request> {
let endpoint = self.endpoint.join("/v1/session/logout")?;

let session_state = self.session_state();
Expand All @@ -684,7 +693,7 @@ impl APIClient {
|| self.session_state.lock().need_keep_alive.unwrap_or(false)
}

pub async fn refresh_session_token(
async fn refresh_session_token(
&self,
self_login_info: Arc<parking_lot::Mutex<(SessionTokenInfo, Instant)>>,
) -> Result<()> {
Expand Down
27 changes: 19 additions & 8 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@

mod client;

pub mod auth;
pub mod error;
pub mod error_code;
mod auth;
mod error;
mod error_code;
mod global_cookie_store;
mod login;
pub mod presign;
pub mod request;
pub mod response;
pub mod session;
pub mod stage;
mod presign;
mod request;
mod response;

mod session;
mod stage;

pub use auth::SensitiveString;
pub use client::APIClient;
pub use error::Error;
pub use presign::presign_download_from_stage;
pub use presign::presign_upload_to_stage;
pub use presign::PresignedResponse;
pub use response::QueryResponse;
pub use response::QueryStats;
pub use response::SchemaField;
pub use stage::StageLocation;
4 changes: 2 additions & 2 deletions driver/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use url::Url;
#[cfg(feature = "flight-sql")]
use crate::flight_sql::FlightSQLConnection;

use databend_client::presign::{presign_download_from_stage, PresignedResponse};
use databend_client::stage::StageLocation;
use databend_client::StageLocation;
use databend_client::{presign_download_from_stage, PresignedResponse};
use databend_driver_core::error::{Error, Result};
use databend_driver_core::rows::{Row, RowIterator, RowStatsIterator, RowWithStats, ServerStats};
use databend_driver_core::schema::{DataType, Field, NumberDataType, Schema};
Expand Down
4 changes: 2 additions & 2 deletions driver/src/flight_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use tokio_stream::{Stream, StreamExt};
use tonic::transport::{Channel, ClientTlsConfig, Endpoint};
use url::Url;

use databend_client::auth::SensitiveString;
use databend_client::presign::{presign_upload_to_stage, PresignedResponse};
use databend_client::SensitiveString;
use databend_client::{presign_upload_to_stage, PresignedResponse};
use databend_driver_core::error::{Error, Result};
use databend_driver_core::rows::{
Row, RowIterator, RowStatsIterator, RowWithStats, Rows, ServerStats,
Expand Down
8 changes: 4 additions & 4 deletions driver/src/rest_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_stream::Stream;

use databend_client::presign::PresignedResponse;
use databend_client::response::QueryResponse;
use databend_client::APIClient;
use databend_client::PresignedResponse;
use databend_client::QueryResponse;
use databend_driver_core::error::{Error, Result};
use databend_driver_core::rows::{Row, RowIterator, RowStatsIterator, RowWithStats, ServerStats};
use databend_driver_core::schema::{Schema, SchemaRef};
Expand All @@ -46,8 +46,8 @@ impl Connection for RestAPIConnection {
async fn info(&self) -> ConnectionInfo {
ConnectionInfo {
handler: "RestAPI".to_string(),
host: self.client.host.clone(),
port: self.client.port,
host: self.client.host().to_string(),
port: self.client.port(),
user: self.client.username(),
database: self.client.current_database(),
warehouse: self.client.current_warehouse(),
Expand Down
6 changes: 3 additions & 3 deletions sql/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum Error {
IO(String),
BadArgument(String),
InvalidResponse(String),
Api(databend_client::error::Error),
Api(databend_client::Error),
#[cfg(feature = "flight-sql")]
Arrow(arrow_schema::ArrowError),
Convert(ConvertError),
Expand Down Expand Up @@ -183,8 +183,8 @@ impl From<hex::FromHexError> for Error {
}
}

impl From<databend_client::error::Error> for Error {
fn from(e: databend_client::error::Error) -> Self {
impl From<databend_client::Error> for Error {
fn from(e: databend_client::Error) -> Self {
Error::Api(e)
}
}
Expand Down
4 changes: 2 additions & 2 deletions sql/src/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl ServerStats {
}
}

impl From<databend_client::response::QueryStats> for ServerStats {
fn from(stats: databend_client::response::QueryStats) -> Self {
impl From<databend_client::QueryStats> for ServerStats {
fn from(stats: databend_client::QueryStats) -> Self {
let mut p = Self {
total_rows: 0,
total_bytes: 0,
Expand Down
2 changes: 1 addition & 1 deletion sql/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) const ARROW_EXT_TYPE_GEOMETRY: &str = "Geometry";
#[cfg(feature = "flight-sql")]
pub(crate) const ARROW_EXT_TYPE_GEOGRAPHY: &str = "Geography";

use databend_client::response::SchemaField as APISchemaField;
use databend_client::SchemaField as APISchemaField;

use crate::error::{Error, Result};

Expand Down

0 comments on commit 3eb745e

Please sign in to comment.