Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set user agent in webdav requests #2284

Merged
merged 14 commits into from
Dec 3, 2024
Merged
9 changes: 9 additions & 0 deletions src/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ use crate::config::Config;
use crate::config::{self, CacheType};
use async_trait::async_trait;
use fs_err as fs;
use opendal::raw::HttpClient;
use reqwest::ClientBuilder;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::io::{self, Cursor, Read, Seek, Write};
Expand Down Expand Up @@ -553,6 +555,13 @@ pub(in crate::cache) fn normalize_key(key: &str) -> String {
format!("{}/{}/{}/{}", &key[0..1], &key[1..2], &key[2..3], &key)
}

/// Set the user agent (helps with monitoring on the server side)
pub fn set_user_agent() -> HttpClient {
let user_agent = format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
let client_builder = ClientBuilder::new().user_agent(user_agent);
HttpClient::build(client_builder).unwrap()
}

/// Get a suitable `Storage` implementation from configuration.
#[allow(clippy::cognitive_complexity)] // TODO simplify!
pub fn storage_from_config(
Expand Down
10 changes: 1 addition & 9 deletions src/cache/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::cache::cache::set_user_agent;
use opendal::layers::LoggingLayer;
use opendal::raw::HttpClient;
use opendal::services::S3;
use opendal::Operator;
use reqwest::ClientBuilder;

use crate::errors::*;

Expand Down Expand Up @@ -67,13 +66,6 @@ impl S3Cache {
}
}

/// Set the user agent (helps with monitoring on the server side)
fn set_user_agent() -> HttpClient {
let user_agent = format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
let client_builder = ClientBuilder::new().user_agent(user_agent);
HttpClient::build(client_builder).unwrap()
}

/// Resolve given endpoint along with use_ssl settings.
fn endpoint_resolver(endpoint: &str, use_ssl: Option<bool>) -> Result<String> {
let endpoint_uri: http::Uri = endpoint
Expand Down
4 changes: 3 additions & 1 deletion src/cache/webdav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::cache::cache::set_user_agent;
use crate::errors::*;
use opendal::layers::LoggingLayer;
use opendal::services::Webdav;
Expand All @@ -32,7 +33,8 @@ impl WebdavCache {
.root(key_prefix)
.username(username.unwrap_or_default())
.password(password.unwrap_or_default())
.token(token.unwrap_or_default());
.token(token.unwrap_or_default())
.http_client(set_user_agent());

let op = Operator::new(builder)?
.layer(LoggingLayer::default())
Expand Down
Loading