-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparam.rs
More file actions
96 lines (84 loc) · 2.85 KB
/
param.rs
File metadata and controls
96 lines (84 loc) · 2.85 KB
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
use feignhttp::{feign, get, Feign};
#[get("https://httpbin.org/headers", headers = "token: {token}")]
async fn headers(#[param] token: &str) -> feignhttp::Result<String> {}
#[get(url = "https://httpbin.org/delay/5", timeout = "{time}")]
async fn timeout(#[param] time: u16) -> feignhttp::Result<String> {}
#[get(url = "https://httpbin.org/delay/{delay_time}", timeout = "{time}")]
async fn dynamic_timeout(
#[path] delay_time: u8, // Replace `{delay_time}` in url.
#[param] time: u16, // Replace `{time}`.
) -> feignhttp::Result<String> {
}
#[derive(Feign)]
struct Http;
#[feign(url = "https://httpbin.org/delay/5", timeout = "{timeout}")]
impl Http {
#[get]
async fn timeout(
&self,
#[param("timeout")] time: u16, // Replace `{timeout}` in feign attribute.
) -> feignhttp::Result<String> {
}
#[get(path = "", timeout = "{time}")] // Override timeout in feign attribute.
async fn override_timeout(
&self,
#[param(name = "time")] time: &str, // Must be a type that can be converted to timeout.
) -> feignhttp::Result<String> {
}
}
#[tokio::main]
async fn main() {
// A request with a header `token: ZmVpZ25odHRw`.
let res = headers("ZmVpZ25odHRw").await.unwrap();
println!("headers: {}", res);
// Request timeout is 3000ms.
match timeout(3000).await {
Ok(res) => {
println!("timeout(3000) ok: {}\n", res);
}
Err(err) => {
// Execute here.
println!("timeout(3000) err: {:?}\n", err);
}
}
// The request url is https://httpbin.org/delay/5 and the request timeout is 3000ms.
match dynamic_timeout(5, 3000).await {
Ok(res) => {
println!("dynamic_timeout(5, 3000) ok: {}\n", res);
}
Err(err) => {
// Execute here.
println!("dynamic_timeout(5, 3000) err: {:?}\n", err);
}
}
// The request url is https://httpbin.org/delay/1 and the request timeout is 3000ms.
match dynamic_timeout(1, 3000).await {
Ok(res) => {
// Execute here.
println!("dynamic_timeout(1, 3000) ok: {}\n", res);
}
Err(err) => {
println!("dynamic_timeout(1, 3000) err: {:?}\n", err);
}
}
// The request timeout is 3000ms.
match Http.timeout(3000).await {
Ok(res) => {
println!("Http::timeout(3000) ok: {}\n", res);
}
Err(err) => {
// Execute here.
println!("Http::timeout(3000) err: {:?}\n", err);
}
}
// The request timeout is 7000ms.
match Http.override_timeout("7000").await {
Ok(res) => {
// Execute here.
println!("Http::override_timeout(\"7000\") ok: {}\n", res);
}
Err(err) => {
println!("Http::override_timeout(\"7000\") err: {:?}\n", err);
}
}
}