From 32ea19c04c87596168b355c4d2e0dbd7d4a3be2b Mon Sep 17 00:00:00 2001 From: everpcpc Date: Fri, 28 Jul 2023 10:21:46 +0800 Subject: [PATCH] feat(core): set custom request UA for client --- core/Cargo.toml | 4 ++++ core/build.rs | 21 +++++++++++++++++++++ core/src/client.rs | 12 +++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 core/build.rs diff --git a/core/Cargo.toml b/core/Cargo.toml index 9d29c1db..d85eb9ac 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -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"] } @@ -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"] } diff --git a/core/build.rs b/core/build.rs new file mode 100644 index 00000000..1b288266 --- /dev/null +++ b/core/build.rs @@ -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> { + let _ = EmitBuilder::builder().fail_on_error().git_sha(true).emit(); + Ok(()) +} diff --git a/core/src/client.rs b/core/src/client.rs index cedb7619..6d53b5a4 100644 --- a/core/src/client.rs +++ b/core/src/client.rs @@ -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}; @@ -33,6 +34,12 @@ use crate::{ response::{QueryError, QueryResponse}, }; +static VERSION: Lazy = 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, @@ -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));