use crate::HttpClient;
use crate::HttpOptions;
-pub const DEFAULT_USER_AGENT_STRING: &str = "proxmox-sync-http-client/0.1";
-
#[derive(Default)]
/// Blocking HTTP client for usage with [`HttpClient`].
pub struct Client {
fn agent(&self) -> Result<ureq::Agent, Error> {
let mut builder = ureq::AgentBuilder::new();
- builder = builder.user_agent(
- self.options
- .user_agent
- .as_deref()
- .unwrap_or(DEFAULT_USER_AGENT_STRING),
- );
+ builder = builder.user_agent(self.options.user_agent.as_deref().unwrap_or(concat!(
+ "proxmox-sync-http-client/",
+ env!("CARGO_PKG_VERSION")
+ )));
if let Some(proxy_config) = &self.options.proxy_config {
builder = builder.proxy(ureq::Proxy::new(proxy_config.to_proxy_string()?)?);
Ok(builder.build())
}
- fn call(&self, req: ureq::Request) -> Result<ureq::Response, Error> {
+ fn call(req: ureq::Request) -> Result<ureq::Response, Error> {
req.call().map_err(Into::into)
}
- fn send<R>(&self, req: ureq::Request, body: R) -> Result<ureq::Response, Error>
+ fn send<R>(req: ureq::Request, body: R) -> Result<ureq::Response, Error>
where
R: Read,
{
let req = self.agent()?.get(uri);
let req = Self::add_headers(req, None, extra_headers);
- self.call(req).and_then(Self::convert_response_to_string)
+ Self::call(req).and_then(Self::convert_response_to_string)
}
fn post(
let req = Self::add_headers(req, content_type, extra_headers);
match body {
- Some(body) => self.send(req, body.as_bytes()),
- None => self.call(req),
+ Some(body) => Self::send(req, body.as_bytes()),
+ None => Self::call(req),
}
.and_then(Self::convert_response_to_string)
}
}
}
- self.send(req, request.body().as_bytes())
- .and_then(Self::convert_response_to_string)
+ Self::send(req, request.body().as_bytes()).and_then(Self::convert_response_to_string)
}
}
let req = self.agent()?.get(uri);
let req = Self::add_headers(req, None, extra_headers);
- self.call(req).and_then(Self::convert_response_to_vec)
+ Self::call(req).and_then(Self::convert_response_to_vec)
}
fn post(
let req = Self::add_headers(req, content_type, extra_headers);
match body {
- Some(body) => self.send(req, body),
- None => self.call(req),
+ Some(body) => Self::send(req, body),
+ None => Self::call(req),
}
.and_then(Self::convert_response_to_vec)
}
}
}
- self.send(req, *request.body())
- .and_then(Self::convert_response_to_vec)
+ Self::send(req, *request.body()).and_then(Self::convert_response_to_vec)
}
}
let req = self.agent()?.get(uri);
let req = Self::add_headers(req, None, extra_headers);
- self.call(req).and_then(Self::convert_response_to_reader)
+ Self::call(req).and_then(Self::convert_response_to_reader)
}
fn post(
let req = Self::add_headers(req, content_type, extra_headers);
match body {
- Some(body) => self.send(req, body),
- None => self.call(req),
+ Some(body) => Self::send(req, body),
+ None => Self::call(req),
}
.and_then(Self::convert_response_to_reader)
}
}
}
- self.send(req, Box::new(request.body_mut()))
- .and_then(Self::convert_response_to_reader)
+ Self::send(req, Box::new(request.body_mut())).and_then(Self::convert_response_to_reader)
}
}