1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! # FeignHTTP
//!
//! FeignHTTP is a declarative HTTP client. Based on rust macros.
//!
//! Here are some features:
//!
//! * Easy to use
//! * Asynchronous request
//! * Supports form, plain text and JSON
//! * Configurable timeout settings
//! * Friendly error handling
//! * Selectable HTTP backends ([reqwest](https://docs.rs/reqwest) or [isahc](https://docs.rs/isahc))
//!
//! ## Table of contents
//!
//! * <a href="#usage">Usage</a>
//! * <a href="#making-a-post-request">Making a POST request</a>
//! * <a href="#paths">Paths</a>
//! * <a href="#url">URL</a>
//! * <a href="#query-parameters">Query Parameters</a>
//! * <a href="#headers">Headers</a>
//! * <a href="#form">Form</a>
//! * <a href="#json">JSON</a>
//! * <a href="#using-structure">Using Structure</a>
//! * <a href="#timeout-configuration">Timeout Configuration</a>
//! * <a href="#params">Params</a>
//! * <a href="#error-handling">Error Handling</a>
//! * <a href="#logs">Logs</a>
//! * <a href="#optional-features">Optional Features</a>
//!
//! ## Usage
//!
//! FeignHTTP mark macros on asynchronous functions, you need a runtime for support async/await. You can use [async-std](https://docs.rs/async-std) or [tokio](https://docs.rs/tokio).
//!
//! async-std:
//!
//! ```toml
//! [dependencies]
//! async-std = { version = "1", features = ["attributes", "tokio1"] }
//! ```
//!
//! The feature `tokio1` is need when use reqwest as the HTTP backend.
//!
//! tokio:
//!
//! ```toml
//! [dependencies]
//! tokio = { version = "1", features = ["full"] }
//! ```
//!
//! Add `feignhttp` in your `Cargo.toml` and use default feature:
//!
//! ```toml
//! feignhttp = { version = "0.5" }
//! ```
//!
//! Then add the following code:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("https://api.github.com")]
//! async fn github() -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let r = github().await?;
//!     println!("result: {}", r);
//!
//!     Ok(())
//! }
//! ```
//!
//! The `get` attribute macro specifies get request, `feignhttp::Result<String>` specifies the return result.
//! It will send get request to `https://api.github.com` and receive a plain text body.
//!
//! Using non-default HTTP backend:
//!
//! ```toml
//! feignhttp = { version = "0.5", default-features = false, features = ["isahc-client"] }
//! ```
//!
//! The `default-features = false` option disable default reqwest.
//!
//! ## Making a POST request
//!
//! For a post request, you should use the `post` attribute macro to specify request method and use a `body` attribute to specify
//! a request body.
//!
//! ```rust, no_run
//! use feignhttp::post;
//!
//! #[post("https://httpbin.org/anything")]
//! async fn post_data(#[body] text: String) -> feignhttp::Result<String> {}
//! ```
//!
//! The `#[body]` mark a request body. Function parameter `text` is a String type, it will put in the request body as plain text.
//! String and &str will be put as plain text into the request body. Before send request, a header `content-type: text/plain` will be added automatically.
//!
//! ## Paths
//!
//! Using `path` to specify path value:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("https://api.github.com/repos/{owner}/{repo}")]
//! async fn repository(
//!     #[path("owner")] user: &str,
//!     #[path] repo: String,
//! ) -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let r = repository("dxx", "feignhttp".to_string()).await?;
//!     println!("repository result: {}", r);
//!
//!     Ok(())
//! }
//! ```
//!
//! `dxx` will replace `{owner}` and `feignhttp` will replace `{repo}` , the url to be send will be
//! `https://api.github.com/repos/dxx/feignhttp`. You can specify a path name like `#[path("owner")]`.
//!
//! ## URL
//!
//! You can use constant to maintain all urls of request:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! const GITHUB_URL: &str = "https://api.github.com";
//!
//! #[get(GITHUB_URL, path = "/repos/{owner}/{repo}/languages")]
//! async fn languages(
//!     #[path] owner: &str,
//!     #[path] repo: &str,
//! ) -> feignhttp::Result<String> {}
//! ```
//!
//! Url constant must be the first metadata in get attribute macro. You also can specify metadata key:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! const GITHUB_URL: &str = "https://api.github.com";
//!
//! #[get(url = GITHUB_URL, path = "/repos/{owner}/{repo}/languages")]
//! async fn languages(
//!     #[path] owner: &str,
//!     #[path] repo: &str,
//! ) -> feignhttp::Result<String> {}
//! ```
//!
//! See [here](https://github.com/dxx/feignhttp/blob/HEAD/examples/url.rs) for more examples.
//!
//! ## Query Parameters
//!
//! Using `query` to specify query parameter:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("https://api.github.com/repos/{owner}/{repo}/contributors")]
//! async fn contributors(
//!     #[path] owner: &str,
//!     #[path] repo: &str,
//!     #[query] page: u32,
//! ) -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let r = contributors("dxx", "feignhttp", 1).await?;
//!     println!("contributors result: {}", r);
//!
//!     Ok(())
//! }
//! ```
//!
//! The `page` parameter will as query parameter in the url. An url which will be send is `https://api.github.com/repos/dxx/feignhttp?page=1`.
//!
//! **Note**: A function parameter without `query` attribute will as a query parameter by default.
//!
//! ## Headers
//!
//! Using `header` to specify request header:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("https://api.github.com/repos/dxx/feignhttp/commits")]
//! async fn commits(
//!     #[header] accept: &str,
//!     #[query] page: u32,
//!     #[query] per_page: u32,
//! ) -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let r = commits("application/vnd.github.v3+json", 1, 5).await?;
//!     println!("commits result: {}", r);
//!
//!     Ok(())
//! }
//! ```
//!
//! A header `accept: application/vnd.github.v3+json` will be added.
//!
//! You also can use `headers` key to specify one or more headers in `get` attribute:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("https://httpbin.org/headers", headers = "key1: value1; key2: value2")]
//! async fn headers() -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let r = headers().await?;
//!     println!("headers result: {}", r);
//!
//!     Ok(())
//! }
//! ```
//!
//! The format of `headers` must be `header-key1: header-value1; header-key2: header-value2;...`.
//!
//! ## Form
//!
//! Using `form` to specify form parameter:
//!
//! ```rust, no_run
//! use feignhttp::post;
//!
//! #[post(url = "https://httpbin.org/anything")]
//! async fn post_user(
//!     #[form] id: i32,
//!     #[form] name: &str,
//! ) -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let r = post_user(1, "jack").await?;
//!     println!("{}", r);
//!
//!     Ok(())
//! }
//! ```
//!
//! Before send request, a header `content-type: application/x-www-form-urlencoded` will be added automatically.
//! See [here](https://github.com/dxx/feignhttp/blob/HEAD/examples/form.rs) for more examples.
//!
//! ## JSON
//!
//! [Serde](https://docs.rs/serde) is a framework for serializing and deserializing Rust data structures. When use json, you should add serde in `Cargo.toml`:
//!
//! ```toml
//! serde = { version = "1", features = ["derive"] }
//! ```
//!
//! You also need enable `json` feature:
//! ```toml
//! feignhttp = { version = "<version>", features = ["json"] }
//! ```
//!
//! Here is an example of getting json:
//!
//! ```rust, no_run
//! use feignhttp::get;
//! use serde::Deserialize;
//!
//! // Deserialize: Specifies deserialization
//! #[derive(Debug, Deserialize)]
//! struct IssueItem {
//!     pub id: u32,
//!     pub number: u32,
//!     pub title: String,
//!     pub url: String,
//!     pub repository_url: String,
//!     pub state: String,
//!     pub body: Option<String>,
//! }
//!
//! const GITHUB_URL: &str = "https://api.github.com";
//!
//! # #[cfg(feature = "json")]
//! #[get(url = GITHUB_URL, path = "/repos/{owner}/{repo}/issues")]
//! async fn issues(
//!     #[path] owner: &str,
//!     #[path] repo: &str,
//!     page: u32,
//!     per_page: u32,
//! ) -> feignhttp::Result<Vec<IssueItem>> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     # #[cfg(feature = "json")]
//!     # {
//!     let r = issues("octocat", "hello-world", 1, 2).await?;
//!     println!("issues: {:#?}", r);
//!     # }
//!
//!     Ok(())
//! }
//! ```
//!
//! This issues function return `Vec<IssueItem>`, it is deserialized according to the content of the response.
//!
//! Send a json request:
//!
//! ```rust, no_run
//! use feignhttp::post;
//! use serde::Serialize;
//!
//! #[derive(Debug, Serialize)]
//! struct User {
//!     id: i32,
//!     name: String,
//! }
//!
//! # #[cfg(feature = "json")]
//! #[post(url = "https://httpbin.org/anything")]
//! async fn post_user(#[body] user: User) -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     # #[cfg(feature = "json")]
//!     # {
//!     let user = User {
//!         id: 1,
//!         name: "jack".to_string(),
//!     };
//!     let r = post_user(user).await?;
//!     println!("{}", r);
//!     # }
//!
//!     Ok(())
//! }
//! ```
//! Before send request, a header `content-type: application/json` will be added automatically.
//! See [here](https://github.com/dxx/feignhttp/blob/HEAD/examples/json.rs) for a complete example.
//!
//! ## Using Structure
//!
//! Structure is a good way to manage requests. Define a structure and then define a large number of request methods:
//!
//! ```rust, no_run
//! use feignhttp::{feign, Feign};
//!
//! #[derive(Feign)]
//! struct Github {
//!     // `url_path` and `param` are used to set the shared data.
//!     // The other two for share data are `header` and `query`.
//!     #[url_path("owner")]
//!     user: &'static str,
//!     #[url_path]
//!     repo: &'static str,
//!     #[param]
//!     accept: &'static str,
//! }
//! 
//! #[feign(
//!     url = "https://api.github.com/repos/{owner}/{repo}",
//!     headers = "Accept: {accept}"
//! )]
//! impl Github {
//!     // The method must have a self argument.
//!     #[get]
//!     async fn home(&self) -> feignhttp::Result<String> {}
//! 
//!     #[get(path = "", headers = "Accept: application/json")]
//!     async fn repository(&self) -> feignhttp::Result<String> {}
//! 
//!     #[get("/commits")]
//!     async fn commits(
//!         &self,
//!         #[header] accept: &str,
//!         #[query] page: u32,
//!         #[query] per_page: u32,
//!     ) -> feignhttp::Result<String> {}
//! 
//! }
//! ```
//!
//! See [here](https://github.com/dxx/feignhttp/blob/HEAD/examples/struct.rs) for a complete example.
//!
//! ## Timeout Configuration
//!
//! If you need to configure the timeout, use `connect_timeout` and `timeout` to specify connect timeout and read timeout.
//!
//! Connect timeout:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get(url = "http://site_dne.com", connect_timeout = 3000)]
//! async fn connect_timeout() -> feignhttp::Result<String> {}
//! ```
//!
//! Read timeout:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get(url = "https://httpbin.org/delay/5", timeout = 3000)]
//! async fn timeout() -> feignhttp::Result<String> {}
//! ```
//!
//! ## Params
//!
//! Sometimes you need dynamic values, like config or others. `param` is designed to support such ability. You can use
//! `param` to specify a value that used as a dynamic replacement.
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get(url = "https://httpbin.org/delay/5", timeout = "{time}")]
//! async fn timeout(#[param] time: u16) -> feignhttp::Result<String> {}
//! ```
//!
//! When call `timeout` function, the time's value will replace the `{time}`.
//!
//! Another example is replace headers:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("https://httpbin.org/headers", headers = "token: {token}")]
//! async fn headers(#[param] token: &str) -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // A reqeut with a header `token: ZmVpZ25odHRw`.
//!     let res = headers("ZmVpZ25odHRw").await?;
//!     println!("headers: {}", res);
//!
//!     Ok(())
//! }
//! ```
//!
//! **Note**: `param` can't replace placeholder in url or path.
//!
//! ## Error Handling
//!
//! FeignHTTP use [`feignhttp::Result`](https://docs.rs/feignhttp/latest/feignhttp/type.Result.html) to receive return result. The error is
//! [`Error`](https://docs.rs/feignhttp/latest/feignhttp/struct.Error.html) struct which has some error kinds and some useful methods.
//! [`ErrorKind`](https://docs.rs/feignhttp/latest/feignhttp/enum.ErrorKind.html) is used to indicate an error type.
//!
//! Url is incorrect:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("httpbin.org/anything")]
//! async fn url_error() -> feignhttp::Result<()> {}
//!
//! #[tokio::main]
//! async fn main() {
//!     match url_error().await {
//!         Err(err) => {
//!             // Build client error.
//!             if err.is_build_error() {
//!                 println!("url_error: {}", err);
//!             }
//!         }
//!         _ => {}
//!     }
//! }
//! ```
//!
//! Parse config error:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get(url = "https://httpbin.org/delay/3", timeout = "abc")]
//! async fn config_error() -> feignhttp::Result<()> {}
//!
//! #[tokio::main]
//! async fn main() {
//!     match config_error().await {
//!         Err(err) => {
//!             // Parse config error.
//!             if err.is_config_error() {
//!                 println!("config_error: {}", err);
//!             }
//!         }
//!         _ => {}
//!     }
//! }
//! ```
//! When parsing the configuration, an error is thrown if the value is incorrect. `timeout` is an integer type, when parse `abc` to integer will throw an error.
//!
//! HTTP status is an importmant info about response. The status code can tell whether the client or server is abnormal.
//! The following is an example of handling through HTTP status:
//!
//! ```rust, no_run
//! use feignhttp::{get, ErrorKind};
//!
//! #[get(url = "https://httpbin.org/123")]
//! async fn status_error() -> feignhttp::Result<()> {}
//!
//! #[tokio::main]
//! async fn main() {
//!     match status_error().await {
//!         Err(err) => {
//!             // Status error.
//!             if err.is_status_error() {
//!                 println!("status_error: {}", err);
//!             }
//!             if let ErrorKind::Status(status) = err.error_kind() {
//!             
//!                 println!("status error code: {}", status.as_u16());
//!
//!                 if status.is_client_error() {
//!                     // Handle error.
//!                 }
//!                 if status.is_server_error() {
//!                     // Handle error.
//!                 }
//!             }
//!         }
//!         _ => {}
//!     }
//! }
//! ```
//! The status is [StatusCode](https://docs.rs/http/latest/http/status/struct.StatusCode.html) struct that supply by [http](https://crates.io/crates/http) crate.
//! For more examples, see [here](https://github.com/dxx/feignhttp/blob/HEAD/examples/error.rs).
//!
//! ## Logs
//!
//! FeignHTTP logs some useful information about requests and responses with the [log](https://crates.io/crates/log) crate.
//! To enable the log information, specify `log` feature in `Cargo.toml`, then set the log level to debug.
//!
//! ```toml
//! features = ["log"]
//! ```
//!
//! ## Optional Features
//!
//! The following features are available. The default features are `reqwest-client`
//! * **reqwest-client** *(default)*: Use `reqwest` as the HTTP backend
//! * **isahc-client**: Use `isahc` as the HTTP backend
//! * **json**: Enable json serialization and deserialization
//! * **log**: Enable request and response logs

mod error;
mod http;
mod macros;

#[cfg(feature = "reqwest-client")]
mod reqwest;
#[cfg(feature = "reqwest-client")]
pub use crate::reqwest::*;

#[cfg(feature = "isahc-client")]
mod isahc;
#[cfg(feature = "isahc-client")]
pub use crate::isahc::*;

#[doc(hidden)]
pub mod util;

pub use feignhttp_codegen::*;
use std::collections::HashMap;

pub use crate::error::{Error, ErrorKind, Result};
pub use crate::http::*;

pub trait FeignClient {
    fn param_map(&self) -> HashMap<&str, String>;
    fn header_map(&self) -> HashMap<std::borrow::Cow<str>, String>;
    fn path_map(&self) -> HashMap<&str, String>;
    fn query_map(&self) -> Vec<(&str, String)>;
}