summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Anshul Gupta <ansg191@anshulg.com> 2024-03-13 19:32:05 -0700
committerGravatar GitHub <noreply@github.com> 2024-03-13 19:32:05 -0700
commit8f5d953037e20b5fbe752d51ec5d55fb8b53c3e9 (patch)
treed4775d3c14f222d17e44574d794e07df87befefc
parent397b6c063ceac2dc81b397c66d7b29bca6f5d046 (diff)
parent478272f94e64cd94e900c829912e64b925faea5b (diff)
downloadtrakt-8f5d953037e20b5fbe752d51ec5d55fb8b53c3e9.tar.gz
trakt-8f5d953037e20b5fbe752d51ec5d55fb8b53c3e9.tar.zst
trakt-8f5d953037e20b5fbe752d51ec5d55fb8b53c3e9.zip
Merge pull request #53 from ansg191/revert-51-hyper1
-rw-r--r--Cargo.toml2
-rw-r--r--trakt-rs/Cargo.toml2
-rw-r--r--trakt-rs/src/test.rs39
3 files changed, 33 insertions, 10 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 7a78bed..e88b359 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,7 +4,7 @@ resolver = "2"
[workspace.dependencies]
bytes = "1.5"
-http = "1"
+http = "0.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
time = { version = "0.3", features = ["formatting", "macros", "parsing", "serde"] }
diff --git a/trakt-rs/Cargo.toml b/trakt-rs/Cargo.toml
index bda8f1b..670cd1c 100644
--- a/trakt-rs/Cargo.toml
+++ b/trakt-rs/Cargo.toml
@@ -28,4 +28,4 @@ unicode-segmentation = "1"
[dev-dependencies]
httpmock = "0.7"
-ureq = { version = "2.9", default-features = false, features = ["http-crate"] }
+reqwest = { version = "0.11", default-features = false, features = ["blocking"] }
diff --git a/trakt-rs/src/test.rs b/trakt-rs/src/test.rs
index 3444499..c00a38d 100644
--- a/trakt-rs/src/test.rs
+++ b/trakt-rs/src/test.rs
@@ -1,8 +1,14 @@
+use std::sync::OnceLock;
+
+use bytes::Bytes;
+use reqwest::blocking::Client;
use trakt_core::{
error::{FromHttpError, IntoHttpError},
Context, Request, Response,
};
+static CLIENT: OnceLock<Client> = OnceLock::new();
+
pub fn assert_request<R, T>(ctx: Context, req: R, exp_url: &str, exp_body: &T)
where
R: Request,
@@ -35,26 +41,43 @@ where
}
pub fn execute<R: Request>(ctx: Context, req: R) -> Result<R::Response, Error> {
+ let client = CLIENT.get_or_init(Client::new);
+
let request: http::Request<Vec<u8>> = req.try_into_http_request(ctx)?;
- let (parts, body) = request.into_parts();
- let request = ureq::Request::from(parts);
- let response = request.send_bytes(&body)?;
- let http_res: http::Response<Vec<u8>> = http::Response::from(response);
+ let reqwest_res = client.execute(request.try_into()?)?;
+ let http_res = reqwest_to_http(reqwest_res)?;
Ok(Response::try_from_http_response(http_res)?)
}
+fn reqwest_to_http(
+ mut response: reqwest::blocking::Response,
+) -> Result<http::Response<Bytes>, Error> {
+ let mut builder = http::Response::builder()
+ .status(response.status())
+ .version(response.version());
+
+ std::mem::swap(
+ builder.headers_mut().expect("response should be valid"),
+ response.headers_mut(),
+ );
+
+ Ok(builder
+ .body(response.bytes()?)
+ .expect("response should be valid"))
+}
+
#[derive(Debug)]
pub enum Error {
- Reqwest(Box<ureq::Error>),
+ Reqwest(reqwest::Error),
IntoHttp(IntoHttpError),
FromHttp(FromHttpError),
}
-impl From<ureq::Error> for Error {
- fn from(e: ureq::Error) -> Self {
- Self::Reqwest(Box::new(e))
+impl From<reqwest::Error> for Error {
+ fn from(e: reqwest::Error) -> Self {
+ Self::Reqwest(e)
}
}