http icon indicating copy to clipboard operation
http copied to clipboard

Implement `Response` builder helper fns

Open carllerche opened this issue 8 years ago • 12 comments

Just like Request has get, post, etc... helper fns, there should be equivalent helpers for Response.

carllerche avatar Oct 10 '17 15:10 carllerche

I was looking at this and was about to begin but it dawned on me that responses don't have verbs. What are the helpers that should be built?

choubacha avatar Dec 24 '17 00:12 choubacha

Response::ok() -> Builder etc... probably.

It's just a bit nicer than pulling in StatusCode.

carllerche avatar Dec 24 '17 00:12 carllerche

Ah, that makes sense!

choubacha avatar Dec 24 '17 01:12 choubacha

Is there a need for Response::ok()? The default status code is already OK, either with Response::new(body), or with Response::builder().

seanmonstar avatar Dec 26 '17 20:12 seanmonstar

Well, I think that there is definitely use for other common status code helpers, and having ok seems useful for consistency (or someone will be looking for a while through the docs wondering why it isn't there).

carllerche avatar Dec 27 '17 05:12 carllerche

It sounds like it's a question of explicitness vs implicitness. I am curious which other response codes make sense to add. My pr #154 has several, but not all, implemented.

choubacha avatar Dec 27 '17 15:12 choubacha

I still am leaning towards being defensive and careful about what we expose on integral types of the library. I'm less worried about methods that may be on the builders themselves. As such, what if these things were just that, methods on the builder, instead of Response?

Response::builder()
    .created()
Response::builder()
    .not_found()
// etc

seanmonstar avatar Jan 04 '18 19:01 seanmonstar

The other options are:

Builder::created()
    // ...
    .build(..)

response::created()
    // ...
    .build(..)

Of course, this doesn't mirror Request, which is part of what I was hoping to do.

carllerche avatar Jan 22 '18 16:01 carllerche

The options I lean towards, in order are:

  1. As methods on the builder: Response::builder().not_found()
  2. As constructors on the builder: Builder::not_found()

I do think that we should have a mirror for request builder, but I think that instead of following Request::get(), Request::post, that the request side should instead change to be either 1 or 2 above.

seanmonstar avatar Jan 22 '18 18:01 seanmonstar

Is there still strong desire for this? With 0.2 coming, I'd rather propose we don't do this, and remove the ones that exist for Request.

seanmonstar avatar May 30 '19 18:05 seanmonstar

@seanmonstar why remove?

carllerche avatar May 30 '19 20:05 carllerche

It would be useful to have From<StatusCode> for Response<Body>. I often need to create a response from a status code.

malthe avatar Mar 29 '22 20:03 malthe