Skip to content

Commit 9ae1873

Browse files
weihangloseanmonstar
authored andcommitted
docs(examples): formatting and refactoring
1 parent da187b5 commit 9ae1873

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

examples/web_api.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
#![feature(async_await)]
22
#![deny(warnings)]
3-
extern crate hyper;
4-
extern crate pretty_env_logger;
5-
extern crate serde_json;
63

74
use hyper::{Body, Chunk, Client, Method, Request, Response, Server, StatusCode, header};
85
use hyper::client::HttpConnector;
9-
use hyper::service::{service_fn, make_service_fn};
6+
use hyper::service::{make_service_fn, service_fn};
107
use futures_util::{TryStreamExt};
118

129
type GenericError = Box<dyn std::error::Error + Send + Sync>;
10+
type Result<T> = std::result::Result<T, GenericError>;
1311

14-
static NOTFOUND: &[u8] = b"Not Found";
15-
static URL: &str = "http://127.0.0.1:1337/json_api";
1612
static INDEX: &[u8] = b"<a href=\"test.html\">test.html</a>";
13+
static INTERNAL_SERVER_ERROR: &[u8] = b"Internal Server Error";
14+
static NOTFOUND: &[u8] = b"Not Found";
1715
static POST_DATA: &str = r#"{"original": "data"}"#;
16+
static URL: &str = "http://127.0.0.1:1337/json_api";
17+
18+
async fn client_request_response(
19+
client: &Client<HttpConnector>
20+
) -> Result<Response<Body>> {
21+
let req = Request::builder()
22+
.method(Method::POST)
23+
.uri(URL)
24+
.header(header::CONTENT_TYPE, "application/json")
25+
.body(POST_DATA.into())
26+
.unwrap();
1827

19-
async fn client_request_response(client: &Client<HttpConnector>)
20-
-> Result<Response<Body>, GenericError>
21-
{
22-
let req = Request::builder()
23-
.method(Method::POST)
24-
.uri(URL)
25-
.header(header::CONTENT_TYPE, "application/json")
26-
.body(POST_DATA.into())
27-
.unwrap();
28-
29-
let web_res = client.request(req).await?;
28+
let web_res = client.request(req).await?;
3029
// Compare the JSON we sent (before) with what we received (after):
3130
let body = Body::wrap_stream(web_res.into_body().map_ok(|b| {
3231
Chunk::from(format!("<b>POST request body</b>: {}<br><b>Response</b>: {}",
@@ -37,9 +36,7 @@ async fn client_request_response(client: &Client<HttpConnector>)
3736
Ok(Response::new(body))
3837
}
3938

40-
async fn api_post_response(req: Request<Body>)
41-
-> Result<Response<Body>, GenericError>
42-
{
39+
async fn api_post_response(req: Request<Body>) -> Result<Response<Body>> {
4340
// A web api to run against
4441
let entire_body = req.into_body().try_concat().await?;
4542
// TODO: Replace all unwraps with proper error handling
@@ -54,7 +51,7 @@ async fn api_post_response(req: Request<Body>)
5451
Ok(response)
5552
}
5653

57-
async fn api_get_response() -> Result<Response<Body>, GenericError> {
54+
async fn api_get_response() -> Result<Response<Body>> {
5855
let data = vec!["foo", "bar"];
5956
let res = match serde_json::to_string(&data) {
6057
Ok(json) => {
@@ -66,20 +63,21 @@ async fn api_get_response() -> Result<Response<Body>, GenericError> {
6663
Err(_) => {
6764
Response::builder()
6865
.status(StatusCode::INTERNAL_SERVER_ERROR)
69-
.body(Body::from("Internal Server Error"))
66+
.body(INTERNAL_SERVER_ERROR.into())
7067
.unwrap()
7168
}
7269
};
7370
Ok(res)
7471
}
7572

76-
async fn response_examples(req: Request<Body>, client: Client<HttpConnector>)
77-
-> Result<Response<Body>, GenericError>
78-
{
73+
async fn response_examples(
74+
req: Request<Body>,
75+
client: Client<HttpConnector>
76+
) -> Result<Response<Body>> {
7977
match (req.method(), req.uri().path()) {
80-
(&Method::GET, "/") | (&Method::GET, "/index.html") => {
81-
let body = Body::from(INDEX);
82-
Ok(Response::new(body))
78+
(&Method::GET, "/") |
79+
(&Method::GET, "/index.html") => {
80+
Ok(Response::new(INDEX.into()))
8381
},
8482
(&Method::GET, "/test.html") => {
8583
client_request_response(&client).await
@@ -92,17 +90,16 @@ async fn response_examples(req: Request<Body>, client: Client<HttpConnector>)
9290
}
9391
_ => {
9492
// Return 404 not found response.
95-
let body = Body::from(NOTFOUND);
9693
Ok(Response::builder()
97-
.status(StatusCode::NOT_FOUND)
98-
.body(body)
99-
.unwrap())
94+
.status(StatusCode::NOT_FOUND)
95+
.body(NOTFOUND.into())
96+
.unwrap())
10097
}
10198
}
10299
}
103100

104101
#[hyper::rt::main]
105-
async fn main() -> Result<(), GenericError> {
102+
async fn main() -> Result<()> {
106103
pretty_env_logger::init();
107104

108105
let addr = "127.0.0.1:1337".parse().unwrap();

0 commit comments

Comments
 (0)