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

Update deps (mostly http-types and my patched verison of http-client) #358

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,30 @@ middleware-logger = []
encoding = ["encoding_rs", "web-sys"]

[dependencies]
futures-util = { version = "0.3.5", features = ["io"] }
log = { version = "0.4.7", features = ["kv_unstable"] }
mime_guess = "2.0.3"
serde = "1.0.97"
serde_json = "1.0.40"
http-client = { version = "6.5.0", default-features = false }
http-types = "2.5.0"
async-std = { version = "1.6.0", default-features = false, features = ["std"] }
async-trait = "0.1.36"
pin-project-lite = "0.2.0"
once_cell = { version = "1.4.1", optional = true }
futures-util = { version = "0.3.28", features = ["io"] }
log = { version = "0.4.17", features = ["kv_unstable"] }
mime_guess = "2.0.4"
serde = "1.0.163"
serde_json = "1.0.96"
http-client = { git = "https://github.com/expenses/http-client-patched", branch = "update-deps", default-features = false }
http-types = { git = "https://github.com/http-rs/http-types", rev = "ac5d645ce5294554b86ebd49233d3ec01665d1d7" }
async-std = { version = "1.12.0", default-features = false, features = ["std"] }
async-trait = "0.1.68"
pin-project-lite = "0.2.9"
once_cell = { version = "1.17.1", optional = true }
cfg-if = "1.0.0"
getrandom = "0.2.0"
encoding_rs = { version = "0.8.20", optional = true }
getrandom = "0.2.9"
encoding_rs = { version = "0.8.32", optional = true }
rustls_crate = { version = "0.18", optional = true, package = "rustls" }
async-native-tls = { version = "0.3.3", optional = true }

web-sys = { optional = true, version = "0.3.25", features = ["TextDecoder"] }
web-sys = { optional = true, version = "0.3.63", features = ["TextDecoder"] }


[dev-dependencies]
async-std = { version = "1.6.0", features = ["attributes"] }
femme = "1.1.0"
serde = { version = "1.0.97", features = ["derive"] }
async-std = { version = "1.12.0", features = ["attributes"] }
femme = "1.3.0"
serde = { version = "1.0.163", features = ["derive"] }
mockito = "0.23.3"
tide = "0.16.0"

Expand Down
2 changes: 1 addition & 1 deletion examples/next_reuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Middleware for Doubler {
let mut new_req: Request = new_req.into();

for (name, value) in &req {
new_req.insert_header(name, value);
new_req.insert_header(name, value)?;
}

let mut buf = Vec::new();
Expand Down
20 changes: 10 additions & 10 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl Client {
/// let string = client.get("https://httpbin.org/get").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn get(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn get(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Get, self.url(uri)).with_client(self.clone())
}

Expand All @@ -359,7 +359,7 @@ impl Client {
/// let string = client.head("https://httpbin.org/head").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn head(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn head(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Head, self.url(uri)).with_client(self.clone())
}

Expand All @@ -382,7 +382,7 @@ impl Client {
/// let string = client.post("https://httpbin.org/post").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn post(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn post(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Post, self.url(uri)).with_client(self.clone())
}

Expand All @@ -405,7 +405,7 @@ impl Client {
/// let string = client.put("https://httpbin.org/put").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn put(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn put(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Put, self.url(uri)).with_client(self.clone())
}

Expand All @@ -428,7 +428,7 @@ impl Client {
/// let string = client.delete("https://httpbin.org/delete").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn delete(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn delete(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Delete, self.url(uri)).with_client(self.clone())
}

Expand All @@ -451,7 +451,7 @@ impl Client {
/// let string = client.connect("https://httpbin.org/connect").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn connect(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn connect(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Connect, self.url(uri)).with_client(self.clone())
}

Expand All @@ -474,7 +474,7 @@ impl Client {
/// let string = client.options("https://httpbin.org/options").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn options(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn options(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Options, self.url(uri)).with_client(self.clone())
}

Expand All @@ -497,7 +497,7 @@ impl Client {
/// let string = client.trace("https://httpbin.org/trace").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn trace(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn trace(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Trace, self.url(uri)).with_client(self.clone())
}

Expand All @@ -520,7 +520,7 @@ impl Client {
/// let string = client.patch("https://httpbin.org/patch").recv_string().await?;
/// # Ok(()) }
/// ```
pub fn patch(&self, uri: impl AsRef<str>) -> RequestBuilder {
pub fn patch(&self, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(Method::Patch, self.url(uri)).with_client(self.clone())
}

Expand All @@ -544,7 +544,7 @@ impl Client {
/// let res = client.send(req).await?;
/// # Ok(()) }
/// ```
pub fn request(&self, verb: Method, uri: impl AsRef<str>) -> RequestBuilder {
pub fn request(&self, verb: Method, uri: impl AsRef<str>) -> crate::Result<RequestBuilder> {
RequestBuilder::new(verb, self.url(uri)).with_client(self.clone())
}

Expand Down
33 changes: 21 additions & 12 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::http::{
self,
headers::{self, HeaderName, HeaderValues, ToHeaderValues},
Body, Method, Mime, Url,
mime::Mime,
Body, Method, Url,
};
use crate::middleware::Middleware;
use crate::RequestBuilder;
Expand Down Expand Up @@ -146,15 +147,19 @@ impl Request {
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> Option<HeaderValues> {
) -> crate::Result<Option<HeaderValues>> {
self.req.insert_header(name, values)
}

/// Append a header to the headers.
///
/// Unlike `insert` this function will not override the contents of a header, but insert a
/// header if there aren't any. Or else append to the existing list of headers.
pub fn append_header(&mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues) {
pub fn append_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> crate::Result<()> {
self.req.append_header(name, values)
}

Expand Down Expand Up @@ -200,8 +205,12 @@ impl Request {
/// assert_eq!(req.header("X-Requested-With").unwrap(), "surf");
/// # Ok(()) }
/// ```
pub fn set_header(&mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) {
self.insert_header(key, value);
pub fn set_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
) -> crate::Result<Option<HeaderValues>> {
self.insert_header(key, value)
}

/// Get a request extension value.
Expand Down Expand Up @@ -276,7 +285,7 @@ impl Request {
/// value to decide whether to use `Chunked` encoding, or set the
/// response length.
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> Option<usize> {
pub fn len(&self) -> Option<u64> {
self.req.len()
}

Expand Down Expand Up @@ -351,8 +360,8 @@ impl Request {
///
/// This method will return an error if the file couldn't be read.
#[cfg(not(target_arch = "wasm32"))]
pub async fn body_file(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
self.set_body(Body::from_file(path).await?);
pub async fn body_file(&mut self, file: async_std::fs::File) -> std::io::Result<()> {
self.set_body(Body::from_file(file).await?);
Ok(())
}

Expand Down Expand Up @@ -400,14 +409,14 @@ impl Request {
}
}

impl AsRef<http::Headers> for Request {
fn as_ref(&self) -> &http::Headers {
impl AsRef<http::headers::Headers> for Request {
fn as_ref(&self) -> &http::headers::Headers {
self.req.as_ref()
}
}

impl AsMut<http::Headers> for Request {
fn as_mut(&mut self) -> &mut http::Headers {
impl AsMut<http::headers::Headers> for Request {
fn as_mut(&mut self) -> &mut http::headers::Headers {
self.req.as_mut()
}
}
Expand Down
23 changes: 14 additions & 9 deletions src/request_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::http::{
headers::{HeaderName, ToHeaderValues},
Body, Method, Mime, Url,
mime::Mime,
Body, Method, Url,
};
use crate::middleware::Middleware;
use crate::{Client, Error, Request, Response, Result};
Expand Down Expand Up @@ -85,15 +86,15 @@ impl RequestBuilder {
}
}

pub(crate) fn with_client(mut self, client: Client) -> Self {
pub(crate) fn with_client(mut self, client: Client) -> crate::Result<Self> {
let req = self.req.as_mut().unwrap();

for (header_name, header_values) in client.config().headers.iter() {
req.append_header(header_name, header_values);
req.append_header(header_name, header_values)?;
}

self.client = Some(client);
self
Ok(self)
}

/// Sets a header on the request.
Expand All @@ -104,9 +105,13 @@ impl RequestBuilder {
/// let req = surf::get("https://httpbin.org/get").header("header-name", "header-value").build();
/// assert_eq!(req["header-name"], "header-value");
/// ```
pub fn header(mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) -> Self {
self.req.as_mut().unwrap().insert_header(key, value);
self
pub fn header(
mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
) -> crate::Result<Self> {
self.req.as_mut().unwrap().insert_header(key, value)?;
Ok(self)
}

/// Sets the Content-Type header on the request.
Expand Down Expand Up @@ -245,8 +250,8 @@ impl RequestBuilder {
/// # Ok(()) }
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub async fn body_file(self, path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
Ok(self.body(Body::from_file(path).await?))
pub async fn body_file(self, file: async_std::fs::File) -> std::io::Result<Self> {
Ok(self.body(Body::from_file(file).await?))
}

/// Set the URL querystring.
Expand Down
29 changes: 19 additions & 10 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::http::{
self,
headers::{self, HeaderName, HeaderValues, ToHeaderValues},
Body, Error, Mime, StatusCode, Version,
mime::Mime,
Body, Error, StatusCode, Version,
};

use async_std::io::BufRead;
Expand Down Expand Up @@ -86,13 +87,21 @@ impl Response {
}

/// Insert an HTTP header.
pub fn insert_header(&mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) {
self.res.insert_header(key, value);
pub fn insert_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
) -> crate::Result<Option<HeaderValues>> {
self.res.insert_header(key, value)
}

/// Append an HTTP header.
pub fn append_header(&mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) {
self.res.append_header(key, value);
pub fn append_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
) -> crate::Result<()> {
self.res.append_header(key, value)
}

/// An iterator visiting all header pairs in arbitrary order.
Expand Down Expand Up @@ -162,7 +171,7 @@ impl Response {
/// value to decide whether to use `Chunked` encoding, or set the
/// response length.
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> Option<usize> {
pub fn len(&self) -> Option<u64> {
self.res.len()
}

Expand Down Expand Up @@ -329,14 +338,14 @@ impl Into<http::Response> for Response {
}
}

impl AsRef<http::Headers> for Response {
fn as_ref(&self) -> &http::Headers {
impl AsRef<http::headers::Headers> for Response {
fn as_ref(&self) -> &http::headers::Headers {
self.res.as_ref()
}
}

impl AsMut<http::Headers> for Response {
fn as_mut(&mut self) -> &mut http::Headers {
impl AsMut<http::headers::Headers> for Response {
fn as_mut(&mut self) -> &mut http::headers::Headers {
self.res.as_mut()
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn post_json() -> Result<(), http_types::Error> {
.with_body(&serde_json::to_string(&cat)?[..])
.create();
let res = surf::post(mockito::server_url())
.header("Accept", "application/json")
.header("Accept", "application/json")?
.body(Body::from_json(&cat)?)
.await?;
m.assert();
Expand Down Expand Up @@ -192,7 +192,7 @@ async fn config_client_headers() -> Result<(), http_types::Error> {
.add_header("X-Header-Name", "X-Header-Values")?
.try_into()?;

let res = client.get("http://example.org/").await?;
let res = client.get("http://example.org/")?.await?;

assert_eq!(res["X-Header-Name"], "X-Header-Values");

Expand Down
6 changes: 3 additions & 3 deletions wasm-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2018"

[dependencies]
surf = { path = "..", default-features = false, features = ["wasm-client"] }
wasm-bindgen-test = "0.3.24"
async-std = "1.6.4"
serde_json = "1.0.57"
wasm-bindgen-test = "0.3.36"
async-std = "1.12.0"
serde_json = "1.0.96"