use std::sync::RwLock;
use std::borrow::Cow;
use Rocket;
use local::LocalRequest;
use http::{Method, private::CookieJar};
use error::LaunchError;
pub struct Client {
rocket: Rocket,
crate cookies: Option<RwLock<CookieJar>>,
}
impl Client {
fn _new(rocket: Rocket, tracked: bool) -> Result<Client, LaunchError> {
let cookies = match tracked {
true => Some(RwLock::new(CookieJar::new())),
false => None
};
Ok(Client { rocket: rocket.prelaunch_check()?, cookies })
}
#[inline(always)]
pub fn new(rocket: Rocket) -> Result<Client, LaunchError> {
Client::_new(rocket, true)
}
#[inline(always)]
pub fn untracked(rocket: Rocket) -> Result<Client, LaunchError> {
Client::_new(rocket, false)
}
#[inline(always)]
pub fn rocket(&self) -> &Rocket {
&self.rocket
}
#[inline(always)]
pub fn get<'c, 'u: 'c, U: Into<Cow<'u, str>>>(&'c self, uri: U) -> LocalRequest<'c> {
self.req(Method::Get, uri)
}
#[inline(always)]
pub fn put<'c, 'u: 'c, U: Into<Cow<'u, str>>>(&'c self, uri: U) -> LocalRequest<'c> {
self.req(Method::Put, uri)
}
#[inline(always)]
pub fn post<'c, 'u: 'c, U: Into<Cow<'u, str>>>(&'c self, uri: U) -> LocalRequest<'c> {
self.req(Method::Post, uri)
}
#[inline(always)]
pub fn delete<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
where U: Into<Cow<'u, str>>
{
self.req(Method::Delete, uri)
}
#[inline(always)]
pub fn options<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
where U: Into<Cow<'u, str>>
{
self.req(Method::Options, uri)
}
#[inline(always)]
pub fn head<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
where U: Into<Cow<'u, str>>
{
self.req(Method::Head, uri)
}
#[inline(always)]
pub fn patch<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
where U: Into<Cow<'u, str>>
{
self.req(Method::Patch, uri)
}
#[inline(always)]
pub fn req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c>
where U: Into<Cow<'u, str>>
{
LocalRequest::new(self, method, uri.into())
}
}
#[cfg(test)]
mod test {
use super::Client;
fn assert_sync<T: Sync>() {}
#[test]
fn test_local_client_impl_sync() {
assert_sync::<Client>();
}
}