fhttp_core/request/
mod.rs

1use reqwest::header::HeaderMap;
2use reqwest::Method;
3#[cfg(test)] use serde_json::Value;
4#[cfg(test)] use body::MultipartPart;
5
6use body::Body;
7use crate::postprocessing::response_handler::ResponseHandler;
8
9pub mod body;
10
11#[derive(Debug, PartialEq, Eq)]
12pub struct Request {
13    pub method: Method,
14    pub url: String,
15    pub headers: HeaderMap,
16    pub body: Body,
17    pub response_handler: Option<ResponseHandler>,
18}
19
20#[cfg(test)]
21impl Request {
22    pub fn basic(
23        method: &'static str,
24        url: &'static str
25    ) -> Self {
26        use std::str::FromStr;
27
28        Request {
29            method: Method::from_str(method).unwrap(),
30            url: url.to_owned(),
31            headers: HeaderMap::new(),
32            body: Body::Plain(String::new()),
33            response_handler: None,
34        }
35    }
36
37    pub fn add_header(
38        mut self,
39        name: &'static str,
40        value: &'static str,
41    ) -> Self {
42        use std::str::FromStr;
43        use reqwest::header::{HeaderName, HeaderValue};
44
45        self.headers.insert(
46            HeaderName::from_str(name).unwrap(),
47            HeaderValue::from_str(value).unwrap()
48        );
49
50        self
51    }
52
53    pub fn body(
54        mut self,
55        body: &'static str,
56    ) -> Self {
57        self.body = Body::Plain(body.to_owned());
58
59        self
60    }
61
62    pub fn gql_body(
63        mut self,
64        body: Value,
65    ) -> Self {
66        self.body = Body::Plain(
67            serde_json::to_string(&body).unwrap()
68        );
69
70        self
71    }
72
73    pub fn multipart(
74        mut self,
75        parts: &[MultipartPart],
76    ) -> Self {
77        self.body = Body::Multipart(parts.to_vec());
78
79        self
80    }
81
82    pub fn response_handler_json(
83        mut self,
84        handler: &'static str,
85    ) -> Self {
86        self.response_handler = Some(ResponseHandler::Json { json_path: handler.to_owned(), });
87
88        self
89    }
90
91    pub fn response_handler_deno(
92        mut self,
93        handler: &'static str,
94    ) -> Self {
95        self.response_handler = Some(ResponseHandler::Deno { program: handler.to_owned(), });
96
97        self
98    }
99}