2.0.0-alpha.5
This is an alpha release in preparation of 2.0.0, so you can start using Surf with stable futures. There may be significant breaking changes before the final 2.0 release. Until thin, we recommend pinning to the particular alpha:
[dependencies]
surf = "= 2.0.0-alpha.5"This alpha release notably contains much more API parity with Tide, particularly for surf::Request, surf::Response, and surf::middleware::Middleware. Middleware also is now implemented using async-trait. Additionally, surf::Client is no longer generic and now instead holds the internal HttpClient as a dynamic trait object.
These changes mean that surf middleware must undergo the following changes:
Old middleware:
impl<C: HttpClient> Middleware<C> for Logger {
fn handle<'a>(
&'a self,
req: Request,
client: C,
next: Next<'a, C>,
) -> BoxFuture<'a, Result<Response, http_types::Error>> {
Box::pin(async move {
Ok(res)
})
}
}New middleware:
#[surf::utils::async_trait]
impl Middleware for Logger {
async fn handle(
&self,
req: Request,
client: Client,
next: Next<'_>,
) -> Result<Response> {
Ok(res)
}
}This alpha release also contains large changes to how the surf::Request and surf::Client APIs are structured, adding a surf::RequestBuilder which is now returned from methods such as surf::get(...). Overall usage structure was kept the same where possible and reasonable, however now a surf::Client must be used when using middleware.
let client = surf::client()
.with(some_middleware);
let req = surf::post(url) // Now returns a `surf::RequestBuilder`!
.header(a_header, a_value)
.body(a_body);
let res = client.send(req).await?;Additions
surf::Requestadded many methods that exist intide::Request.surf::Responseadded many methods that exist intide::Response.surf::http, an export ofhttp_types, similar totide::http.surf::middleware::Redirect, a middleware to handle redirect status codes.- All conversions for
RequestandResponsebetweenhttp_typesandsurfnow exist.
Changes
surf::Requestchanged many methods to be like those intide::Request.surf::Responsechanged many methods to be like those intide::Response.- Surf now uses
http_types::mimeinstead of themimecrate. TryFrom<http_types::Request> for Requestis nowFrom<http_types::Request> for Request.surf::Clientis no longer generic forC: HttpClient.- Middleware now receives
surf::Requestand returnsResult<surf::Response, E>, and no longer requires a generic bound. - Middleware now uses async-trait, which is exported as
surf::utils::async_trait. - The logger middleware is now exported as
surf::middleware::Logger. (Note: this middleware is used by default.) surf::{method}()e.g.surf::get()now returns asurf::RequestBuilderrather than asurf::Request.- Middleware can no longer be set for individual requests.
- Instead, use a
surf::Clientand register middleware viaclient.with(middleware). - Then, send the request from that client via
client.send(req)e.g.let res = client.send(request).await?;.
surf::Clientnow can set a "base url" for that client viaclient.set_base_url().
Fixes
From<http_types::Request> for Requestnow properly propagates all properties.- A cloned
surf::Clientno longer adds middleware onto its ancestor's middleware stack. - Some feature flags are now correct.
Internal
- Use Clippy in CI.
- Improved examples.
- Now only depends on
futures_utilrather than all offutures.