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

Add Response::from_res #466

Merged
merged 1 commit into from
May 17, 2020
Merged

Add Response::from_res #466

merged 1 commit into from
May 17, 2020

Conversation

yoshuawuyts
Copy link
Member

This adds Response::from_res which enables us to bridge with Surf for authoring proxies conveniently. We couldn't add this as a generic From bound because it would conflict with existing implementations.

This was in response to this this example which proxies from surf -> tide, which would allow us to rewrite the core logic from ~10 lines to 2 lines.

let res = surf::get(...).await?;
Ok(Response::from_res(res))

@chrisdickinson
Copy link

Would it be possible to have a form of this where we could pass a HashMap to override headers on the way through?

Response::from_res(res, hashmap!{
    "ETag" => "a modified etag",
})

(Or possibly a lambda to modify them?)

Response::from_res(res, |headers: &mut Headers| {
  // edit the headers on the way through
})

@yoshuawuyts
Copy link
Member Author

yoshuawuyts commented Apr 25, 2020

The following would be possible with this patch:

let res = surf::get(...).await?;
let mut res = Response::from_res(res);
res.set_header("etag".parse()?, "<new etag>");
Ok(res)

In future patches well likely rename set_header to insert_header, and remove the need for calling parse()?. But the overall approach should remain the same.

@yoshuawuyts yoshuawuyts force-pushed the response_from_http_types branch from 44ef000 to fd555a6 Compare May 17, 2020 11:12
@yoshuawuyts yoshuawuyts merged commit 37c1762 into master May 17, 2020
@yoshuawuyts yoshuawuyts deleted the response_from_http_types branch May 17, 2020 11:35
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