Skip to content
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

Typed requests without needing the http crate #2216

Closed
wants to merge 1 commit into from

Conversation

itowlson
Copy link
Contributor

@itowlson itowlson commented Jan 9, 2024

This explores a possible approach to fermyon/spin-rust-sdk#23.

With this PR, a user can write a http_component entry point using an extractor, without needing the http crate (as this is not referenced in the template), by use-ing spin_sdk::http::typed::Request<T> instead of spin_sdk::http::Request. E.g.

// Note typed::Request rather than Request
use spin_sdk::http::{IntoResponse, typed::Request, Response, Json};

#[derive(Deserialize)]
struct Fie {
    thing: String,
    whatsit: i32,
}

#[http_component]
fn handle_test(req: Request<Json<Fie>>) -> anyhow::Result<impl IntoResponse> {
    let fie = req.body()?;
    let text = format!("The thing was {} and the whatsit was {}", fie.thing, fie.whatsit);
    Ok(Response::builder()
        .status(200)
        .header("content-type", "text/plain")
        .body(text)
        .build())
}

I don't love the duplication of methods - I don't know if this can be made more maintainable. But anyway putting this up for feedback and discussion.

@rylev
Copy link
Collaborator

rylev commented Jan 9, 2024

I'm personally not convinced that it's worth the trouble. Turning a request body into a type is not too difficult (the user just needs to know how to turn a Vec<u8> into their type of choice), and the http crate is ubiquitous and well supported. Is it worth expanding the API of the SDK to support this extractor style directly? I'm not sure...

@fibonacci1729
Copy link
Contributor

I'm similarly not entirely convinced this approach is worth it either ... I think I would be more in favor of an "extractor" as mentioned in the linked issue in the near term (which although is less aesthetic, provides reasonable convenience around having to go from serialized bytes to some user provided data structure).

Having said that, @itowlson I am amendable to what you're proposing here if you feel strongly about the DX.

@itowlson
Copy link
Contributor Author

itowlson commented Jan 9, 2024

The people have spoken! Thanks for the feedback - I'll close.

@itowlson itowlson closed this Jan 9, 2024
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.

3 participants