Struct Request

Source
pub struct Request<P = ()> { /* private fields */ }
Expand description

Resty Request wrapper.

Implementations§

Source§

impl<P> Request<P>

Source

pub fn new(request: Request, params: P) -> Self

Creates new instance of request

Source

pub fn params(&self) -> &P

Returns params reference.

Examples found in repository?
examples/api.rs (line 59)
47    fn into(self) -> resty::Router {
48        let mut router = resty::Router::new();
49        let self_ = ::std::sync::Arc::new(self);
50        let a = self_.clone();
51        // no params
52        router.get("/", move |_request| {
53            a.list()
54        });
55
56        let a = self_.clone();
57        // dynamic params
58        router.get("/{id}", move |request| {
59            a.single(request.params().get_usize("id")?)
60        });
61
62        let a = self_.clone();
63        // static params
64        router.get(url!(/test/{id:usize}), move |request| {
65            a.single(request.params().id)
66        });
67
68        let a = self_.clone();
69        router.put(url!(/{id:usize}), move |request| {
70            let a = a.clone();
71            let id = request.params().id;
72            request.json().map_err(Into::into).and_then(move |call: Call| {
73                a.update(id, call)
74            })
75        });
76
77        let a = self_.clone();
78        // post request
79        router.post("/", move |request| {
80            let a = a.clone();
81            request.json().map_err(Into::into).and_then(move |call: Call| {
82                a.add(call)
83            })
84        });
85
86        router
87    }
Source

pub fn take_params(&mut self) -> P

Consumes params.

Source

pub fn json<T>( self, ) -> Then<Concat2<Body>, Result<T, Error>, fn(Result<Chunk, Error>) -> Result<T, Error>>
where T: for<'a> Deserialize<'a>,

Read the body of this request and deserialize it from JSON. Returns error in case the request body cannot be read or deserialization fails.

Examples found in repository?
examples/api.rs (line 72)
47    fn into(self) -> resty::Router {
48        let mut router = resty::Router::new();
49        let self_ = ::std::sync::Arc::new(self);
50        let a = self_.clone();
51        // no params
52        router.get("/", move |_request| {
53            a.list()
54        });
55
56        let a = self_.clone();
57        // dynamic params
58        router.get("/{id}", move |request| {
59            a.single(request.params().get_usize("id")?)
60        });
61
62        let a = self_.clone();
63        // static params
64        router.get(url!(/test/{id:usize}), move |request| {
65            a.single(request.params().id)
66        });
67
68        let a = self_.clone();
69        router.put(url!(/{id:usize}), move |request| {
70            let a = a.clone();
71            let id = request.params().id;
72            request.json().map_err(Into::into).and_then(move |call: Call| {
73                a.update(id, call)
74            })
75        });
76
77        let a = self_.clone();
78        // post request
79        router.post("/", move |request| {
80            let a = a.clone();
81            request.json().map_err(Into::into).and_then(move |call: Call| {
82                a.add(call)
83            })
84        });
85
86        router
87    }
88}
89
90#[derive(Deserialize, Serialize, Clone)]
91struct Call {
92    pub test: u64,
93}
94
95fn main() {
96    let mut v1 = resty::Router::new();
97    v1.add("/products", Products {
98        calls: RwLock::new(vec![Call { test: 1 }, Call { test: 2}]),
99    }.into());
100
101    let mut server = resty::Router::new();
102    server.add("/v1", v1);
103    server.post("/test", |request| {
104        request.json().map(|mut call: Call| {
105            call.test += 1;
106            call
107        })
108    });
109
110    println!("{}", server.routes());
111    let listening = server.bind("localhost:3000").unwrap();
112    listening.wait()
113}

Trait Implementations§

Source§

impl<P: Debug> Debug for Request<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P = ()> !Freeze for Request<P>

§

impl<P = ()> !RefUnwindSafe for Request<P>

§

impl<P> Send for Request<P>
where P: Send,

§

impl<P = ()> !Sync for Request<P>

§

impl<P> Unpin for Request<P>
where P: Unpin,

§

impl<P = ()> !UnwindSafe for Request<P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.