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
36 .execute($url)
37 .await?
38 .body_as::<$res_body_ty, $res_ty>($res_body_ty)
39 .await?
40 };
41
42 ($url:expr, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
43 $client
44 .execute($url)
45 .await?
46 .body_as::<$res_body_ty, $res_ty>($res_body_ty)
47 .await?
48 };
49
50 ($url:expr, &mut $client:ident) => {
51 $client.execute($url).await?.text()
52 };
53}
54
55#[macro_export]
56/// Make a POST request to the specified URL.
57///
58/// The `post!` macro is used to make a POST request to the specified URL.
59///
60/// To help understand the macro arguments, here is an example:
61///
62/// post!(input, req_body_ty, url, &mut client, res_body_ty, res_ty)
63///
64/// # Arguments
65///
66/// * `input` - The input to send with the request.
67/// * `req_body_ty` - The body serialization type of the request.
68/// * `url` - The URL to make the POST request to.
69/// * `client` - The client variable to use for the request.
70/// * `res_body_ty` - The body type of the response.
71/// * `res_ty` - The type of the response.
72///
73/// Please note url can be a string literal or a variable.
74///
75/// # Example
76///
77/// ## Without response body deserialization
78///
79/// ```compile_fail
80/// let mut client = Deboa::new();
81/// let response = post!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts", &mut client);
82/// assert_eq!(response.id, 1);
83/// ```
84///
85/// ## With response body deserialization
86///
87/// ```compile_fail
88/// let mut client = Deboa::new();
89/// let response = post!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts", &mut client, JsonBody, Post);
90/// assert_eq!(response.id, 1);
91/// ```
92macro_rules! post {
93 ($input:ident, $req_body_ty:ident, $url:literal, &mut $client:ident) => {
94 $client
95 .execute(
96 deboa::request::DeboaRequest::post($url)?
97 .body_as($req_body_ty, $input)?
98 .build()?,
99 )
100 .await?
101 };
102
103 ($input:ident, $req_body_ty:ident, $url:expr, &mut $client:ident) => {
104 $client
105 .execute(
106 deboa::request::DeboaRequest::post($url)?
107 .body_as($req_body_ty, $input)?
108 .build()?,
109 )
110 .await?
111 };
112
113 ($input:ident, $req_body_ty:ident, $url:expr, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
114 $client
115 .execute(
116 deboa::request::DeboaRequest::post($url)?
117 .body_as($req_body_ty, $input)?
118 .build()?,
119 )
120 .await?
121 .body_as::<$res_body_ty, $res_ty>($res_body_ty)?
122 };
123}
124
125#[macro_export]
126/// Make a PUT request to the specified URL.
127///
128/// The `put!` macro is used to make a PUT request to the specified URL
129/// Its first argument is a string literal or a variable.
130///
131/// To help understand the macro arguments, here is an example:
132///
133/// put!(input, req_body_ty, url, &mut client)
134///
135/// # Arguments
136///
137/// * `input` - The input to send with the request.
138/// * `req_body_ty` - The body serialization type of the request.
139/// * `url` - The URL to make the PUT request to.
140/// * `client` - The client variable to use for the request.
141///
142/// Please note url can be a string literal or a variable.
143///
144/// # Example
145///
146/// ```compile_fail
147/// let mut client = Deboa::new();
148/// let response = put!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts/1", &mut client);
149/// assert_eq!(response.id, 1);
150/// ```
151macro_rules! put {
152 ($input:ident, $req_body_ty:ident, $url:literal, &mut $client:ident) => {
153 $client
154 .execute(
155 deboa::request::DeboaRequest::put($url)?
156 .body_as($req_body_ty, $input)?
157 .build()?,
158 )
159 .await?
160 };
161
162 ($input:ident, $req_body_ty:ident, $url:expr, &mut $client:ident) => {
163 $client
164 .execute(
165 deboa::request::DeboaRequest::put($url)?
166 .body_as($req_body_ty, $input)?
167 .build()?,
168 )
169 .await?
170 };
171
172 ($input:ident, $req_body_ty:ident, $url:expr, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
173 $client
174 .execute(
175 deboa::request::DeboaRequest::put($url)?
176 .body_as($req_body_ty, $input)?
177 .build()?,
178 )
179 .await?
180 .body_as::<$res_body_ty, $res_ty>($res_body_ty)?
181 };
182}
183
184#[macro_export]
185/// Make a PATCH request to the specified URL.
186///
187/// The `patch!` macro is used to make a PATCH request to the specified URL
188/// Its first argument is a string literal or a variable.
189///
190/// To help understand the macro arguments, here is an example:
191///
192/// patch!(input, req_body_ty, url, &mut client)
193///
194/// # Arguments
195///
196/// * `input` - The input to send with the request.
197/// * `req_body_ty` - The body serialization type of the request.
198/// * `url` - The URL to make the PATCH request to.
199/// * `client` - The client variable to use for the request.
200///
201/// Please note url can be a string literal or a variable.
202///
203/// # Example
204///
205/// ```compile_fail
206/// let mut client = Deboa::new();
207/// let response = patch!(data, JsonBody, "https://jsonplaceholder.typicode.com/posts/1", &mut client);
208/// assert_eq!(response.id, 1);
209/// ```
210macro_rules! patch {
211 ($input:ident, $req_body_ty:ident, $url:literal, &mut $client:ident) => {
212 $client
213 .execute(
214 deboa::request::DeboaRequest::patch($url)?
215 .body_as($req_body_ty, $input)?
216 .build()?,
217 )
218 .await?
219 };
220
221 ($input:ident, $req_body_ty:ident, $url:expr, &mut $client:ident) => {
222 $client
223 .execute(
224 deboa::request::DeboaRequest::patch($url)?
225 .body_as($req_body_ty, $input)?
226 .build()?,
227 )
228 .await?
229 };
230
231 ($input:ident, $req_body_ty:ident, $url:expr, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
232 $client
233 .execute(
234 deboa::request::DeboaRequest::patch($url)?
235 .body_as($req_body_ty, $input)?
236 .build()?,
237 )
238 .await?
239 .body_as::<$res_body_ty, $res_ty>($res_body_ty)?
240 };
241}
242
243#[macro_export]
244/// Make a DELETE request to the specified URL.
245///
246/// The `delete!` macro is used to make a DELETE request to the specified URL
247/// Its first argument is a string literal or a variable.
248///
249/// To help understand the macro arguments, here is an example:
250///
251/// delete!(url, &mut client)
252///
253/// # Arguments
254///
255/// * `url` - The URL to make the DELETE request to.
256/// * `client` - The client variable to use for the request.
257///
258/// Please note url can be a string literal or a variable.
259///
260/// # Example
261///
262/// ```compile_fail
263/// let mut client = Deboa::new();
264/// let response = delete!("https://jsonplaceholder.typicode.com/posts/1", &mut client);
265/// assert_eq!(response.id, 1);
266/// ```
267macro_rules! delete {
268 ($url:literal, &mut $client:ident) => {
269 $client
270 .execute(deboa::request::DeboaRequest::delete($url)?.build()?)
271 .await?
272 };
273
274 ($url:expr, &mut $client:ident) => {
275 $client
276 .execute(deboa::request::DeboaRequest::delete($url)?.build()?)
277 .await?
278 };
279}
280
281#[macro_export]
282/// Make a GET request to the specified URL.
283///
284/// The `fetch!` macro is a more generic version of the `get!` macro.
285/// Its first argument is a string literal or a variable. Arrows are
286/// used to specify the body serialization type and the output type.
287///
288/// You can use the `JsonBody`, `XmlBody`, `MsgPack` type for JSON, XML
289/// and MessagePack serialization.
290///
291/// To help understand the macro arguments, here is an example:
292///
293/// fetch!(url, &mut client, body, ty)
294///
295/// # Arguments
296///
297/// * `url` - The URL to make the GET request to.
298/// * `client` - The client variable to use for the request.
299/// * `res_body_ty` - The body type of the response.
300/// * `res_ty` - The type of the response.
301///
302/// Please note url can be a string literal or a variable.
303///
304/// # Example
305///
306/// ```compile_fail
307/// let mut client = Deboa::new();
308/// let response = fetch!("https://jsonplaceholder.typicode.com/posts", &mut client, JsonBody, Post);
309/// assert_eq!(response.id, 1);
310/// ```
311macro_rules! fetch {
312 ($url:literal, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
313 $client
314 .execute($url)
315 .await?
316 .body_as::<$res_body_ty, $res_ty>($res_body_ty)
317 .await?
318 };
319
320 ($url:expr, &mut $client:ident, $res_body_ty:ident, $res_ty:ty) => {
321 $client
322 .execute($url)
323 .await?
324 .body_as::<$res_body_ty, $res_ty>($res_body_ty)
325 .await?
326 };
327
328 ($url:expr, &mut $client:ident) => {
329 $client.execute($url).await?
330 };
331}
332#[macro_export]
333/// Submit a request to the specified URL.
334///
335/// The `submit!` macro is a more generic version of the `get!` macro.
336/// Its first argument is a string literal or a variable. Arrows are
337/// used to specify the body serialization type and the output type.
338///
339/// You can use the `JsonBody`, `XmlBody`, `MsgPack` type for JSON, XML
340/// and MessagePack serialization.
341///
342/// To help understand the macro arguments, here is an example:
343///
344/// fetch!(url, &mut client, body, ty)
345///
346/// # Arguments
347///
348/// * `method` - The HTTP method to use.
349/// * `input` - The input to send with the request.
350/// * `url` - The URL to make the GET request to.
351/// * `client` - The client variable to use for the request.
352/// * `res_body_ty` - The body type of the response.
353/// * `res_ty` - The type of the response.
354///
355/// Please note url can be a string literal or a variable.
356///
357/// # Example
358///
359/// ```compile_fail
360/// let mut client = Deboa::new();
361/// let response = submit!("POST", "user=deboa", "https://jsonplaceholder.typicode.com/posts", &mut client);
362/// assert_eq!(response.id, 1);
363/// ```
364macro_rules! submit {
365 ($method:expr, $input:expr, $url:expr, &mut $client:ident) => {
366 $client
367 .execute(
368 deboa::request::DeboaRequest::at($url, $method)?
369 .text($input)
370 .build()?,
371 )
372 .await?
373 };
374}