Skip to content

Commit 95f4d06

Browse files
author
jizhuozhi.george
committed
Add Promise support for http callout
simplify http_parallel_call example Signed-off-by: jizhuozhi.george <[email protected]>
1 parent 5c95bb4 commit 95f4d06

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

examples/http_parallel_call/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ $ docker compose up
1919

2020
#### Access granted.
2121

22-
Send HTTP request to `localhost:10000/headers`:
22+
Send HTTP request to `localhost:10000/`:
2323

2424
```sh
25-
$ curl localhost:10000/headers
25+
$ curl localhost:10000/
2626
Hello, World!\n
2727
```

examples/http_parallel_call/src/lib.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414

1515
use proxy_wasm::callout::http::HttpClient;
16-
use proxy_wasm::hostcalls;
1716
use proxy_wasm::callout::promise::Promise;
17+
use proxy_wasm::hostcalls;
1818
use proxy_wasm::traits::*;
1919
use proxy_wasm::types::*;
2020
use std::time::Duration;
@@ -32,40 +32,40 @@ struct HttpParallelCall {
3232
impl HttpContext for HttpParallelCall {
3333
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
3434
// "Hello, "
35-
let promise1 = self.client.dispatch(
36-
"httpbin",
37-
vec![
38-
(":method", "GET"),
39-
(":path", "/base64/SGVsbG8sIA=="),
40-
(":authority", "httpbin.org"),
41-
],
42-
None,
43-
vec![],
44-
Duration::from_secs(1),
45-
);
35+
let promise1 = self
36+
.client
37+
.dispatch(
38+
"httpbin",
39+
vec![
40+
(":method", "GET"),
41+
(":path", "/base64/SGVsbG8sIA=="),
42+
(":authority", "httpbin.org"),
43+
],
44+
None,
45+
vec![],
46+
Duration::from_secs(1),
47+
)
48+
.then(|(_, _, body_size, _)| get_http_call_response_body_string(0, body_size))
49+
.then(|body| body.unwrap_or_default());
4650

4751
// "World!"
48-
let promise2 = self.client.dispatch(
49-
"httpbin",
50-
vec![
51-
(":method", "GET"),
52-
(":path", "/base64/V29ybGQh"),
53-
(":authority", "httpbin.org"),
54-
],
55-
None,
56-
vec![],
57-
Duration::from_secs(1),
58-
);
52+
let promise2 = self
53+
.client
54+
.dispatch(
55+
"httpbin",
56+
vec![
57+
(":method", "GET"),
58+
(":path", "/base64/V29ybGQh"),
59+
(":authority", "httpbin.org"),
60+
],
61+
None,
62+
vec![],
63+
Duration::from_secs(1),
64+
)
65+
.then(|(_, _, body_size, _)| get_http_call_response_body_string(0, body_size))
66+
.then(|body| body.unwrap_or_default());
5967

60-
Promise::all_of(vec![
61-
promise1
62-
.then(|(_, _, body_size, _)| get_http_call_response_body_string(0, body_size))
63-
.then(|body| body.unwrap_or_default()),
64-
promise2
65-
.then(|(_, _, body_size, _)| get_http_call_response_body_string(0, body_size))
66-
.then(|body| body.unwrap_or_default()),
67-
])
68-
.then(|results| {
68+
Promise::all_of(vec![promise1, promise2]).then(|results| {
6969
send_http_response(
7070
200,
7171
vec![],

0 commit comments

Comments
 (0)