Skip to content

Commit

Permalink
apply review comments and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbtp committed Apr 29, 2024
1 parent d6a3209 commit cb094aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
26 changes: 14 additions & 12 deletions src/cmd/src/cli/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use base64::engine::general_purpose;
use base64::Engine as _;
use base64::Engine;
use clap::{Parser, ValueEnum};
use client::DEFAULT_SCHEMA_NAME;
use common_telemetry::{debug, error, info, warn};
Expand Down Expand Up @@ -121,29 +121,31 @@ impl Export {
sql
);

let client = reqwest::Client::new();
let mut request = client
let mut request = reqwest::Client::new()
.get(&url)
.header("Content-Type", "application/x-www-form-urlencoded");
if let Some(ref auth) = self.auth_header {
request = request.header("Authorization", auth);
}

let response = request
.send()
.await
.with_context(|_| HttpQuerySqlSnafu { reason: url })?;
snafu::ensure!(response.status() == 200, EmptyResultSnafu);
let response = request.send().await.with_context(|_| HttpQuerySqlSnafu {
reason: format!("bad url: {}", url),
})?;
let response = response
.error_for_status()
.with_context(|_| HttpQuerySqlSnafu {
reason: format!("query failed: {}", sql),
})?;

let text = response.text().await.with_context(|_| HttpQuerySqlSnafu {
reason: "cannot get response text".to_string(),
})?;

let body = serde_json::from_str::<GreptimedbV1Response>(&text).context(SerdeJsonSnafu)?;
match &body.output()[0] {
GreptimeQueryOutput::Records(records) => Ok(Some(records.rows().clone())),
GreptimeQueryOutput::AffectedRows(_) => Ok(None),
}
Ok(body.output().first().and_then(|output| match output {
GreptimeQueryOutput::Records(records) => Some(records.rows().clone()),
GreptimeQueryOutput::AffectedRows(_) => None,
}))
}

/// Iterate over all db names.
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ impl ErrorExt for Error {
Error::StartProcedureManager { source, .. }
| Error::StopProcedureManager { source, .. } => source.status_code(),
Error::StartWalOptionsAllocator { source, .. } => source.status_code(),
Error::ReplCreation { .. } | Error::Readline { .. } => StatusCode::Internal,
Error::ReplCreation { .. } | Error::Readline { .. } | Error::HttpQuerySql { .. } => {
StatusCode::Internal
}
Error::CollectRecordBatches { source, .. }
| Error::PrettyPrintRecordBatches { source, .. } => source.status_code(),
Error::StartMetaClient { source, .. } => source.status_code(),
Expand All @@ -301,9 +303,7 @@ impl ErrorExt for Error {
Error::SubstraitEncodeLogicalPlan { source, .. } => source.status_code(),
Error::StartCatalogManager { source, .. } => source.status_code(),

Error::SerdeJson { .. } | Error::FileIo { .. } | Error::HttpQuerySql { .. } => {
StatusCode::Unexpected
}
Error::SerdeJson { .. } | Error::FileIo { .. } => StatusCode::Unexpected,

Error::Other { source, .. } => source.status_code(),

Expand Down

0 comments on commit cb094aa

Please sign in to comment.