Skip to content

Commit

Permalink
feat(core): set custom request UA for client
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed Jul 28, 2023
1 parent 12bcae0 commit 32ea19c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ native-tls = ["reqwest/native-tls"]
[dependencies]
bytes = "1.4.0"
http = "0.2.9"
once_cell = "1.18.0"
percent-encoding = "2.3.0"
reqwest = { version = "0.11.18", default-features = false, features = ["json", "multipart", "stream"] }
serde = { version = "1.0.164", default-features = false, features = ["derive"] }
Expand All @@ -32,5 +33,8 @@ tower = { version = "0.4.13", default-features = false }
tower-http = { version = "0.4.1", default-features = false }
url = { version = "2.4.0", default-features = false }

[build-dependencies]
vergen = { version = "8.2.1", features = ["build", "git", "gitcl"] }

[dev-dependencies]
chrono = { version = "0.4.26", default-features = false, features = ["clock"] }
21 changes: 21 additions & 0 deletions core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021 Datafuse Labs
//
// Licensed 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.

use std::error::Error;
use vergen::EmitBuilder;

fn main() -> Result<(), Box<dyn Error>> {
let _ = EmitBuilder::builder().fail_on_error().git_sha(true).emit();
Ok(())
}
12 changes: 11 additions & 1 deletion core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::sync::Arc;
use std::{collections::BTreeMap, fmt};

use http::StatusCode;
use once_cell::sync::Lazy;
use percent_encoding::percent_decode_str;
use reqwest::header::HeaderMap;
use reqwest::multipart::{Form, Part};
Expand All @@ -33,6 +34,12 @@ use crate::{
response::{QueryError, QueryResponse},
};

static VERSION: Lazy<String> = Lazy::new(|| {
let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
let sha = option_env!("VERGEN_GIT_SHA").unwrap_or("dev");
format!("{}-{}", version, sha)
});

pub struct PresignedResponse {
pub method: String,
pub headers: BTreeMap<String, String>,
Expand Down Expand Up @@ -163,14 +170,17 @@ impl APIClient {
},
};

let mut cli_builder =
HttpClient::builder().user_agent(format!("databend-client-rust/{}", VERSION.as_str()));
#[cfg(any(feature = "rustls", feature = "native-tls"))]
if scheme == "https" {
if let Some(ref ca_file) = client.tls_ca_file {
let cert_pem = tokio::fs::read(ca_file).await?;
let cert = reqwest::Certificate::from_pem(&cert_pem)?;
client.cli = HttpClient::builder().add_root_certificate(cert).build()?;
cli_builder = cli_builder.add_root_certificate(cert);
}
}
client.cli = cli_builder.build()?;
client.endpoint = Url::parse(&format!("{}://{}:{}", scheme, client.host, client.port))?;
client.session_settings = Arc::new(Mutex::new(session_settings));

Expand Down

0 comments on commit 32ea19c

Please sign in to comment.