Skip to content

Conversation

softprops
Copy link
Contributor

Issue #, if available:

Description of changes:

This lifts one more change from lando to lambda_http, the IntoResponse trait.

This was motivated by some DX experiences I've had with http::Response::builder() and common cases in practice. It essentially enables handlers to return an implementation of an interface rather than a concrete type which has the ability to conversed into an http::Response<Body>. This reduces ceremony and effort in constructing responses for common cases.

use serde_json::json;

fn json_handler(
  _: Request,
  _: Context
) -> Result<impl IntoResponse, HandlerError> {
  Ok(
    json!({
      "answer": 42
   })
  )
}
fn text_handler(
  _: Request,
  _: Context
) -> Result<impl IntoResponse, HandlerError> {
  Ok(
    "ok"
  )
}

It's also extensible for application specific types allowing your function to separate business logic from how that logic's result generates http response types

struct MyType;
impl IntoResponse for MyType {
   fn into_response(self) -> Response<Body> {
      // do your thing...
   }
}

fn custom_handler(
  _: Request,
  _: Context
) -> Result<impl IntoResponse, HandlerError> {
  Ok(
    MyType
  )
}

Let me know what you think.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@davidbarsky davidbarsky self-requested a review December 4, 2018 16:21
Copy link
Contributor

@davidbarsky davidbarsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Looks great.

@davidbarsky davidbarsky merged commit 771e598 into awslabs:master Dec 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants