-
Notifications
You must be signed in to change notification settings - Fork 327
Response builder and related changes #580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
04e8640
09bef88
36c6fac
748f0f4
bc59e7f
eb89ef8
d4cb305
3de967e
43dfdc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| use crate::http::headers::{HeaderName, ToHeaderValues}; | ||
| use crate::http::{Body, Mime, StatusCode}; | ||
| use crate::Response; | ||
| use std::convert::TryInto; | ||
|
|
||
| #[derive(Debug)] | ||
|
|
||
| /// Response Builder | ||
| /// | ||
| /// Provides an ergonomic way to chain the creation of a response. This is generally accessed through [`Response::builder`](crate::Response::builder) | ||
| /// | ||
| /// # Example | ||
| /// ```rust | ||
| /// # use tide::{StatusCode, Response, http::mime}; | ||
| /// # async_std::task::block_on(async move { | ||
| /// let mut response = Response::builder(203) | ||
| /// .body("body") | ||
| /// .content_type(mime::HTML) | ||
| /// .header("custom-header", "value") | ||
| /// .build(); | ||
| /// | ||
| /// assert_eq!(response.take_body().into_string().await.unwrap(), "body"); | ||
| /// assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation); | ||
| /// assert_eq!(response["custom-header"], "value"); | ||
| /// assert_eq!(response.content_type(), Some(mime::HTML)); | ||
| /// # }); | ||
|
|
||
| pub struct ResponseBuilder(Response); | ||
|
|
||
| impl ResponseBuilder { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this include methods for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially added those, but I don't think they make a lot of sense on a builder. The purpose of the builder is to make expressing a one-off response easy. I think it's pretty reasonable to say if they need to make cookie or ext changes, that it's not a quick one-liner type response (before rustfmt). It's easy enough to do: let mut res = Response::build().status(203).body("hello").unwrap();
res.add_cookie(…);
res.insert_ext(…);
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not exactly seeing the advantage of that to writing this if it is only partial? let mut res = Response::new(203);
res.set_body("hello");
res.add_cookie(…);
res.insert_ext(…);
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, in that circumstance there isn't any advantage to using the builder |
||
| pub(crate) fn new<S>(status: S) -> Self | ||
| where | ||
| S: TryInto<StatusCode>, | ||
| S::Error: std::fmt::Debug, | ||
| { | ||
| Self(Response::new(status)) | ||
| } | ||
|
|
||
| /// Returns the inner Response | ||
| pub fn build(self) -> Response { | ||
| self.0 | ||
| } | ||
|
|
||
| /// Sets a header on the response. | ||
| /// ``` | ||
| /// # use tide::Response; | ||
| /// let response = Response::builder(200).header("header-name", "header-value").build(); | ||
| /// assert_eq!(response["header-name"], "header-value"); | ||
| /// ``` | ||
| pub fn header(mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) -> Self { | ||
| self.0.insert_header(key, value); | ||
| self | ||
| } | ||
|
|
||
| /// Sets the Content-Type header on the response. | ||
| /// ``` | ||
| /// # use tide::{http::mime, Response}; | ||
| /// let response = Response::builder(200).content_type(mime::HTML).build(); | ||
| /// assert_eq!(response["content-type"], "text/html;charset=utf-8"); | ||
| /// ``` | ||
| pub fn content_type(mut self, content_type: impl Into<Mime>) -> Self { | ||
| self.0.set_content_type(content_type); | ||
| self | ||
| } | ||
|
|
||
| /// Sets the body of the response. | ||
| /// ``` | ||
| /// # async_std::task::block_on(async move { | ||
| /// # use tide::{Response, convert::json}; | ||
| /// let mut response = Response::builder(200).body(json!({ "any": "Into<Body>"})).build(); | ||
| /// assert_eq!(response.take_body().into_string().await.unwrap(), "{\"any\":\"Into<Body>\"}"); | ||
| /// # }); | ||
| /// ``` | ||
| pub fn body(mut self, body: impl Into<Body>) -> Self { | ||
| self.0.set_body(body); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Into<Response> for ResponseBuilder { | ||
| fn into(self) -> Response { | ||
| self.build() | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.