use http::HeaderMap;
use wreq_proto::{http1::Http1Options, http2::Http2Options};
use crate::{group::Group, header::OrigHeaderMap, tls::TlsOptions};
pub trait IntoEmulation {
fn into_emulation(self) -> Emulation;
}
#[must_use]
#[derive(Debug)]
pub struct EmulationBuilder {
emulation: Emulation,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct Emulation {
pub(crate) group: Group,
pub headers: HeaderMap,
pub orig_headers: OrigHeaderMap,
pub tls_options: Option<TlsOptions>,
pub http1_options: Option<Http1Options>,
pub http2_options: Option<Http2Options>,
}
impl EmulationBuilder {
#[inline]
pub fn http1_options(mut self, opts: Http1Options) -> Self {
self.emulation.http1_options = Some(opts);
self
}
#[inline]
pub fn http2_options(mut self, opts: Http2Options) -> Self {
self.emulation.http2_options = Some(opts);
self
}
#[inline]
pub fn tls_options(mut self, opts: TlsOptions) -> Self {
self.emulation.tls_options = Some(opts);
self
}
#[inline]
pub fn headers(mut self, src: HeaderMap) -> Self {
crate::util::replace_headers(&mut self.emulation.headers, src);
self
}
#[inline]
pub fn orig_headers(mut self, src: OrigHeaderMap) -> Self {
self.emulation.orig_headers.extend(src);
self
}
#[inline]
pub fn build(mut self, group: Group) -> Emulation {
self.emulation.group.emulate(group);
self.emulation
}
}
impl Emulation {
#[inline]
pub fn builder() -> EmulationBuilder {
EmulationBuilder {
emulation: Emulation {
group: Group::default(),
headers: HeaderMap::new(),
orig_headers: OrigHeaderMap::new(),
tls_options: None,
http1_options: None,
http2_options: None,
},
}
}
}
impl<T: Into<Emulation>> IntoEmulation for T {
#[inline]
fn into_emulation(self) -> Emulation {
self.into()
}
}