use std::{rc::Rc, time::Duration};
use actix_http::{error::HttpError, header::HeaderMap, Method, RequestHead, Uri};
use actix_rt::net::TcpStream;
use actix_service::Service;
pub use actix_tls::connect::{
ConnectError as TcpConnectError, ConnectInfo, Connection as TcpConnection,
};
use crate::{ws, BoxConnectorService, ClientBuilder, ClientRequest};
mod config;
mod connection;
mod connector;
mod error;
mod h1proto;
mod h2proto;
mod pool;
pub use self::{
connection::{Connection, ConnectionIo},
connector::{Connector, ConnectorService},
error::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError},
};
#[derive(Clone)]
pub struct Connect {
pub uri: Uri,
pub addr: Option<std::net::SocketAddr>,
}
#[derive(Clone)]
pub struct Client(pub(crate) ClientConfig);
#[derive(Clone)]
pub(crate) struct ClientConfig {
pub(crate) connector: BoxConnectorService,
pub(crate) default_headers: Rc<HeaderMap>,
pub(crate) timeout: Option<Duration>,
}
impl Default for Client {
fn default() -> Self {
ClientBuilder::new().finish()
}
}
impl Client {
pub fn new() -> Client {
Client::default()
}
pub fn builder() -> ClientBuilder<
impl Service<
ConnectInfo<Uri>,
Response = TcpConnection<Uri, TcpStream>,
Error = TcpConnectError,
> + Clone,
> {
ClientBuilder::new()
}
pub fn request<U>(&self, method: Method, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
let mut req = ClientRequest::new(method, url, self.0.clone());
for header in self.0.default_headers.iter() {
req = req.append_header(header);
}
req
}
pub fn request_from<U>(&self, url: U, head: &RequestHead) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
let mut req = self.request(head.method.clone(), url);
for header in head.headers.iter() {
req = req.insert_header_if_none(header);
}
req
}
pub fn get<U>(&self, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
self.request(Method::GET, url)
}
pub fn head<U>(&self, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
self.request(Method::HEAD, url)
}
pub fn put<U>(&self, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
self.request(Method::PUT, url)
}
pub fn post<U>(&self, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
self.request(Method::POST, url)
}
pub fn patch<U>(&self, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
self.request(Method::PATCH, url)
}
pub fn delete<U>(&self, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
self.request(Method::DELETE, url)
}
pub fn options<U>(&self, url: U) -> ClientRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
self.request(Method::OPTIONS, url)
}
pub fn ws<U>(&self, url: U) -> ws::WebsocketsRequest
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
{
let mut req = ws::WebsocketsRequest::new(url, self.0.clone());
for (key, value) in self.0.default_headers.iter() {
req.head.headers.insert(key.clone(), value.clone());
}
req
}
pub fn headers(&mut self) -> Option<&mut HeaderMap> {
Rc::get_mut(&mut self.0.default_headers)
}
}