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
//! # FeignHttp
//!
//! FeignHttp is a declarative HTTP client. Based on rust macros.
//!
//! ## Features
//!
//! * Easy to use
//! * Asynchronous request
//! * Supports `json` and `plain text`
//! * [Reqwest](https://docs.rs/reqwest) of internal use
//!
//! ## Usage
//!
//! FeignHttp mark macros on asynchronous functions, you need to add [Tokio](https://docs.rs/tokio) in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! tokio = { version = "1", features = ["full"] }
//! feignhttp = { version = "0.1.2" }
//! ```
//!
//! 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.
//!
//! ### 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(url = "http://localhost:8080/create")]
//! async fn create(#[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.
//!
//! ### 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: &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")]`.
//!
//! ### Query Parameters
//!
//! Using `param` to specify query parameter:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get("https://api.github.com/repos/{owner}/{repo}/contributors")]
//! async fn contributors(
//! #[path("owner")] user: &str,
//! #[path] repo: &str,
//! #[param] 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 `param` 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/{owner}/{repo}/commits")]
//! async fn commits(
//! #[header] accept: &str,
//! #[path] owner: &str,
//! #[path] repo: &str,
//! #[param] page: u32,
//! #[param] 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",
//! "dxx",
//! "feignhttp",
//! 1,
//! 5,
//! )
//! .await?;
//! println!("commits result: {}", r);
//!
//! Ok(())
//! }
//! ```
//!
//! A header `accept:application/vnd.github.v3+json ` will be send.
//!
//! ### URL constant
//!
//! We 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> {}
//! ```
//!
//! ### 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
//! [dependencies]
//! serde = { version = "1", features = ["derive"] }
//! ```
//!
//! 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";
//!
//! #[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>> {
//! 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,
//! }
//!
//! #[post(url = "http://localhost:8080/create")]
//! async fn create(#[body] user: User) -> feignhttp::Result<String> {}
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let user = User {
//! id: 1,
//! name: "jack".to_string(),
//! };
//! let _r = create(user).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! 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;
//!
//! const GITHUB_URL: &str = "https://api.github.com";
//!
//! struct Github {}
//!
//! #[feign(url = GITHUB_URL)]
//! impl Github {
//! #[get]
//! async fn home() -> feignhttp::Result<String> {}
//!
//! #[get("/repos/{owner}/{repo}")]
//! async fn repository(
//! #[path] owner: &str,
//! #[path] repo: &str,
//! ) -> feignhttp::Result<String> {}
//!
//! // ...
//!
//! // Structure method still send request
//! #[get(path = "/repos/{owner}/{repo}/languages")]
//! async fn languages(
//! &self,
//! #[path] owner: &str,
//! #[path] repo: &str,
//! ) -> 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://xxx.com", connect_timeout = 3000)]
//! async fn connect_timeout() -> feignhttp::Result<String> {}
//! ```
//!
//! Read timeout:
//!
//! ```rust, no_run
//! use feignhttp::get;
//!
//! #[get(url = "http://localhost:8080", timeout = 3000)]
//! async fn timeout() -> feignhttp::Result<String> {}
//! ```
pub use *;
pub use crate*;
pub use crateResult;