deboa_macros/lib.rs
1pub use deboa_bora::bora;
2
3#[macro_export]
4/// Make a GET request to the specified URL.
5///
6/// The `get!` macro is used to make a GET request to the specified URL.
7/// Its first argument is a string literal or a variable. Arrows are
8/// used to specify the body serialization type and the output type.
9///
10/// You can use the `JsonBody`, `XmlBody`, `MsgPack` type for JSON, XML
11/// and MessagePack serialization.
12///
13/// To help understand the macro arguments, here is an example:
14///
15/// get!(url, &mut client, JsonBody, ty)
16///
17/// # Arguments
18///
19/// * `url` - The URL to make the GET request to.
20/// * `client` - The client variable to use for the request.
21/// * `res_body_ty` - The body type of the response.
22/// * `res_ty` - The type of the response.
23///
24/// Please note url can be a string literal or a variable.
25///
26/// # Example
27///
28/// ```compile_fail
29/// let mut client = Deboa::new();
30/// let response = get!("https://jsonplaceholder.typicode.com/posts", &mut client, JsonBody, Vec<Post>);
31/// assert_eq!(response.len(), 100);
32/// ```
33macro_rules! get {
34 ($url:literal, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
35 $client.execute($url).await?.body_as::<$res_body_ty, $res_ty>($res_body_ty)?
36 };
37
38 ($url:ident, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
39 $client.execute($url).await?.body_as::<$res_body_ty, $res_ty>($res_body_ty)?
40 };
41}
42
43#[macro_export]
44/// Make a POST request to the specified URL.
45///
46/// The `post!` macro is used to make a POST request to the specified URL.
47///
48/// To help understand the macro arguments, here is an example:
49///
50/// post!(input, req_body_ty, url, &mut client, res_body_ty, res_ty)
51///
52/// # Arguments
53///
54/// * `input` - The input to send with the request.
55/// * `req_body_ty` - The body serialization type of the request.
56/// * `url` - The URL to make the POST request to.
57/// * `client` - The client variable to use for the request.
58/// * `res_body_ty` - The body type of the response.
59/// * `res_ty` - The type of the response.
60///
61/// Please note url can be a string literal or a variable.
62///
63/// # Example
64///
65/// ## Without response body deserialization
66///
67/// ```compile_fail
68/// let mut client = Deboa::new();
69/// let response = post!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts", &mut client);
70/// assert_eq!(response.id, 1);
71/// ```
72///
73/// ## With response body deserialization
74///
75/// ```compile_fail
76/// let mut client = Deboa::new();
77/// let response = post!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts", &mut client, JsonBody, Post);
78/// assert_eq!(response.id, 1);
79/// ```
80macro_rules! post {
81 ($input:ident, $req_body_ty:ident, $url:literal, &mut $client:ident) => {
82 $client
83 .execute(deboa::request::DeboaRequest::post($url)?.body_as($req_body_ty, $input)?.build()?)
84 .await?
85 };
86
87 ($input:ident, $req_body_ty:ident, $url:ident, &mut $client:ident) => {
88 $client
89 .execute(deboa::request::DeboaRequest::post($url)?.body_as($req_body_ty, $input)?.build()?)
90 .await?
91 };
92
93 ($input:ident, $req_body_ty:ident, $url:literal, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
94 $client
95 .execute(deboa::request::DeboaRequest::post($url)?.body_as($req_body_ty, $input)?.build()?)
96 .await?
97 .body_as::<$res_body_ty, $res_ty>($res_body_ty)?
98 };
99}
100
101#[macro_export]
102/// Make a PUT request to the specified URL.
103///
104/// The `put!` macro is used to make a PUT request to the specified URL
105/// Its first argument is a string literal or a variable.
106///
107/// To help understand the macro arguments, here is an example:
108///
109/// put!(input, req_body_ty, url, &mut client)
110///
111/// # Arguments
112///
113/// * `input` - The input to send with the request.
114/// * `req_body_ty` - The body serialization type of the request.
115/// * `url` - The URL to make the PUT request to.
116/// * `client` - The client variable to use for the request.
117///
118/// Please note url can be a string literal or a variable.
119///
120/// # Example
121///
122/// ```compile_fail
123/// let mut client = Deboa::new();
124/// let response = put!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts/1", &mut client);
125/// assert_eq!(response.id, 1);
126/// ```
127macro_rules! put {
128 ($input:ident, $req_body_ty:ident, $url:literal, &mut $client:ident) => {
129 $client
130 .execute(deboa::request::DeboaRequest::put($url)?.body_as($req_body_ty, $input)?.build()?)
131 .await?
132 };
133
134 ($input:ident, $req_body_ty:ident, $url:ident, &mut $client:ident) => {
135 $client
136 .execute(deboa::request::DeboaRequest::put($url)?.body_as($req_body_ty, $input)?.build()?)
137 .await?
138 };
139
140 ($input:ident, $req_body_ty:ident, $url:ident, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
141 $client
142 .execute(deboa::request::DeboaRequest::put($url)?.body_as($req_body_ty, $input)?.build()?)
143 .await?
144 .body_as::<$res_body_ty, $res_ty>($res_body_ty)?
145 };
146}
147
148#[macro_export]
149/// Make a PATCH request to the specified URL.
150///
151/// The `patch!` macro is used to make a PATCH request to the specified URL
152/// Its first argument is a string literal or a variable.
153///
154/// To help understand the macro arguments, here is an example:
155///
156/// patch!(input, req_body_ty, url, &mut client)
157///
158/// # Arguments
159///
160/// * `input` - The input to send with the request.
161/// * `req_body_ty` - The body serialization type of the request.
162/// * `url` - The URL to make the PATCH request to.
163/// * `client` - The client variable to use for the request.
164///
165/// Please note url can be a string literal or a variable.
166///
167/// # Example
168///
169/// ```compile_fail
170/// let mut client = Deboa::new();
171/// let response = patch!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts/1", &mut client);
172/// assert_eq!(response.id, 1);
173/// ```
174macro_rules! patch {
175 ($input:ident, $req_body_ty:ident, $url:literal, &mut $client:ident) => {
176 $client
177 .execute(deboa::request::DeboaRequest::patch($url)?.body_as($req_body_ty, $input)?.build()?)
178 .await?
179 };
180
181 ($input:ident, $req_body_ty:ident, $url:ident, &mut $client:ident) => {
182 $client
183 .execute(deboa::request::DeboaRequest::patch($url)?.body_as($req_body_ty, $input)?.build()?)
184 .await?
185 };
186
187 ($input:ident, $req_body_ty:ident, $url:ident, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
188 $client
189 .execute(deboa::request::DeboaRequest::patch($url)?.body_as($req_body_ty, $input)?.build()?)
190 .await?
191 .body_as::<$res_body_ty, $res_ty>($res_body_ty)?
192 };
193}
194
195#[macro_export]
196/// Make a DELETE request to the specified URL.
197///
198/// The `delete!` macro is used to make a DELETE request to the specified URL
199/// Its first argument is a string literal or a variable.
200///
201/// To help understand the macro arguments, here is an example:
202///
203/// delete!(url, &mut client)
204///
205/// # Arguments
206///
207/// * `url` - The URL to make the DELETE request to.
208/// * `client` - The client variable to use for the request.
209///
210/// Please note url can be a string literal or a variable.
211///
212/// # Example
213///
214/// ```compile_fail
215/// let mut client = Deboa::new();
216/// let response = delete!("https://jsonplaceholder.typicode.com/posts/1", &mut client);
217/// assert_eq!(response.id, 1);
218/// ```
219macro_rules! delete {
220 ($url:literal, &mut $client:ident) => {
221 $client.execute(deboa::request::DeboaRequest::delete($url)?.build()?).await?
222 };
223
224 ($url:ident, &mut $client:ident) => {
225 $client.execute(deboa::request::DeboaRequest::delete($url)?.build()?).await?
226 };
227}
228
229#[macro_export]
230/// Make a GET request to the specified URL.
231///
232/// The `fetch!` macro is a more generic version of the `get!` macro.
233/// Its first argument is a string literal or a variable. Arrows are
234/// used to specify the body serialization type and the output type.
235///
236/// You can use the `JsonBody`, `XmlBody`, `MsgPack` type for JSON, XML
237/// and MessagePack serialization.
238///
239/// To help understand the macro arguments, here is an example:
240///
241/// fetch!(url, &mut client, body, ty)
242///
243/// # Arguments
244///
245/// * `url` - The URL to make the GET request to.
246/// * `client` - The client variable to use for the request.
247/// * `res_body_ty` - The body type of the response.
248/// * `res_ty` - The type of the response.
249///
250/// Please note url can be a string literal or a variable.
251///
252/// # Example
253///
254/// ```compile_fail
255/// let mut client = Deboa::new();
256/// let response = fetch!("https://jsonplaceholder.typicode.com/posts", &mut client, JsonBody, Post);
257/// assert_eq!(response.id, 1);
258/// ```
259macro_rules! fetch {
260 ($url:literal, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
261 $client.execute($url).await?.body_as::<$res_body_ty, $res_ty>($res_body_ty)?
262 };
263
264 ($url:ident, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
265 $client.execute($url).await?.body_as::<$res_body_ty, $res_ty>($res_body_ty)?
266 };
267}