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

Improve the builtin Request/Response types #1939

Merged
merged 5 commits into from
Oct 25, 2023

Conversation

rylev
Copy link
Collaborator

@rylev rylev commented Oct 25, 2023

This is meant as an alternative to #1931. It improves the Request/Response types with an API that is useable, flexible, and powerful and hopefully enough to cover 80% of use cases. For more advanced use cases, we would recommend using a third party crate like http.

Note: The one slightly awkward API choice is that RequestBuilder::uri is infallible so that even if the user passes a garbage URI it still "works" - they'll likely only see any error when the try to use the uri. I think this is the right balance of easy of use without sacrificing too much protection against misuse.

Copy link
Contributor

@tschneidereit tschneidereit left a comment

Choose a reason for hiding this comment

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

This looks great to me!

I left a couple of comments inline, but I agree that this is the right approach to take.

sdk/rust/src/http.rs Outdated Show resolved Hide resolved
/// Technically headers do not have to be utf8 encoded but this type assumes they are.
/// If you know you'll be dealing with non-utf8 encoded headers, reach more a more powerful
/// response type like that found in the `http` crate.
pub fn headers(&self) -> &[(String, String)] {
Copy link
Contributor

Choose a reason for hiding this comment

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

could we maybe instead introduce a headers_raw (and headers_raw_mut) that returns (String, Vec<u8>)?

I'm imagining that we'd internally store headers as something like (String, HeaderVal), where HeaderVal is something like

enum HeaderVal {
  Bytes(Vec<u8>),
  String(String),
  Both(Vec<u8>, String),
}

I bet there's some more optimal representation of this for the case where Bytes contains valid unicode, too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am somewhat worried about this. I think I've managed to push a change that supports this without being a maintenance burden, be we do have to get comfortable at some point saying that these types will never cover 100% of use cases and a 3rd party crate might be necessary.

sdk/rust/src/http.rs Show resolved Hide resolved
Copy link
Collaborator

@lann lann left a comment

Choose a reason for hiding this comment

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

A couple of suggestions, all of which could be handled in follow-up.

sdk/rust/src/http.rs Show resolved Hide resolved
sdk/rust/src/http/conversions.rs Outdated Show resolved Hide resolved
sdk/rust/src/http.rs Show resolved Hide resolved
sdk/rust/src/http.rs Outdated Show resolved Hide resolved
Signed-off-by: Ryan Levick <[email protected]>
@rylev rylev force-pushed the improve-builtin-request-response branch from 011576c to b363229 Compare October 25, 2023 13:53
Copy link
Collaborator

@lann lann left a comment

Choose a reason for hiding this comment

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

Looks good; thanks!

@rylev rylev enabled auto-merge October 25, 2023 14:00
@rylev rylev force-pushed the improve-builtin-request-response branch from 0d182a6 to 90e8c8f Compare October 25, 2023 16:12
Signed-off-by: Ryan Levick <[email protected]>
@rylev rylev force-pushed the improve-builtin-request-response branch from 90e8c8f to aa06684 Compare October 25, 2023 16:19
sdk/rust/src/http.rs Outdated Show resolved Hide resolved
Signed-off-by: Ryan Levick <[email protected]>
Copy link
Contributor

@tschneidereit tschneidereit left a comment

Choose a reason for hiding this comment

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

A slight nit and one comment of some substance—but almost there!

sdk/rust/src/http.rs Outdated Show resolved Hide resolved
sdk/rust/src/http.rs Outdated Show resolved Hide resolved
sdk/rust/src/http.rs Outdated Show resolved Hide resolved
sdk/rust/src/http.rs Show resolved Hide resolved
Copy link
Contributor

@tschneidereit tschneidereit left a comment

Choose a reason for hiding this comment

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

🚢🚢🚢

Signed-off-by: Ryan Levick <[email protected]>
@rylev rylev force-pushed the improve-builtin-request-response branch from ecdce33 to b337273 Compare October 25, 2023 17:11
@@ -99,6 +384,23 @@ impl std::fmt::Display for Method {
}

impl IncomingRequest {
/// The incoming request Uri
pub fn uri(&self) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the user get this as a Url object or would they need to parse the string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They would need to parse. The thought by behind it is to not make the http crate part of the public API for these types.

@rylev rylev merged commit 7a95022 into main Oct 25, 2023
9 checks passed
@rylev rylev deleted the improve-builtin-request-response branch October 26, 2023 07:28
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.

5 participants