Skip to content

Commit 92a39e4

Browse files
Merge pull request #157 from theseus-rs/remove-http-dependency
chore: remove http dependency
2 parents 3cea664 + 3e7d475 commit 92a39e4

File tree

5 files changed

+37
-85
lines changed

5 files changed

+37
-85
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ flate2 = "1.0.35"
3535
futures-util = "0.3.30"
3636
hex = "0.4.3"
3737
home = "0.5.11"
38-
http = "1.1.0"
3938
indicatif = "0.17.8"
4039
indoc = "2.0.5"
4140
liblzma = "0.3.4"

postgresql_archive/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ async-trait = { workspace = true }
1515
flate2 = { workspace = true }
1616
futures-util = { workspace = true }
1717
hex = { workspace = true }
18-
http = { workspace = true }
1918
liblzma = { workspace = true }
2019
md-5 = { workspace = true, optional = true }
2120
num-format = { workspace = true }

postgresql_archive/src/repository/github/repository.rs

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use crate::Error::{
88
use crate::{hasher, matcher, Result};
99
use async_trait::async_trait;
1010
use futures_util::StreamExt;
11-
use http::{header, Extensions};
1211
use regex::Regex;
13-
use reqwest::{Request, Response};
14-
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, Middleware, Next};
12+
use reqwest::header::HeaderMap;
13+
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
1514
use reqwest_retry::policies::ExponentialBackoff;
1615
use reqwest_retry::RetryTransientMiddleware;
1716
use reqwest_tracing::TracingMiddleware;
@@ -114,6 +113,7 @@ impl GitHub {
114113
loop {
115114
let request = client
116115
.get(&self.releases_url)
116+
.headers(Self::headers())
117117
.query(&[("page", page.to_string().as_str()), ("per_page", "100")]);
118118
let response = request.send().await?.error_for_status()?;
119119
let response_releases = response.json::<Vec<Release>>().await?;
@@ -199,6 +199,20 @@ impl GitHub {
199199

200200
Ok((asset, asset_hash, asset_hasher_fn))
201201
}
202+
203+
/// Returns the headers for the GitHub request.
204+
fn headers() -> HeaderMap {
205+
let mut headers = HeaderMap::new();
206+
headers.append(
207+
GITHUB_API_VERSION_HEADER,
208+
GITHUB_API_VERSION.parse().unwrap(),
209+
);
210+
headers.append("User-Agent", USER_AGENT.parse().unwrap());
211+
if let Some(token) = &*GITHUB_TOKEN {
212+
headers.append("Authorization", format!("Bearer {token}").parse().unwrap());
213+
}
214+
headers
215+
}
202216
}
203217

204218
#[async_trait]
@@ -224,7 +238,9 @@ impl Repository for GitHub {
224238

225239
let client = reqwest_client();
226240
debug!("Downloading archive {}", asset.browser_download_url);
227-
let request = client.get(&asset.browser_download_url);
241+
let request = client
242+
.get(&asset.browser_download_url)
243+
.headers(Self::headers());
228244
let response = request.send().await?.error_for_status()?;
229245
#[cfg(feature = "indicatif")]
230246
let span = tracing::Span::current();
@@ -257,7 +273,9 @@ impl Repository for GitHub {
257273
"Downloading archive hash {}",
258274
asset_hash.browser_download_url
259275
);
260-
let request = client.get(&asset_hash.browser_download_url);
276+
let request = client
277+
.get(&asset_hash.browser_download_url)
278+
.headers(Self::headers());
261279
let response = request.send().await?.error_for_status()?;
262280
let text = response.text().await?;
263281
let re = Regex::new(&format!(r"[0-9a-f]{{{hash_len}}}"))?;
@@ -281,51 +299,11 @@ impl Repository for GitHub {
281299
}
282300
}
283301

284-
/// Middleware to add headers to the request. If a GitHub token is set, then it is added as a
285-
/// bearer token. This is used to authenticate with the GitHub API to increase the rate limit.
286-
#[derive(Debug)]
287-
struct GithubMiddleware;
288-
289-
impl GithubMiddleware {
290-
#[expect(clippy::unnecessary_wraps)]
291-
fn add_headers(request: &mut Request) -> Result<()> {
292-
let headers = request.headers_mut();
293-
headers.append(
294-
GITHUB_API_VERSION_HEADER,
295-
GITHUB_API_VERSION.parse().unwrap(),
296-
);
297-
headers.append(header::USER_AGENT, USER_AGENT.parse().unwrap());
298-
if let Some(token) = &*GITHUB_TOKEN {
299-
headers.append(
300-
header::AUTHORIZATION,
301-
format!("Bearer {token}").parse().unwrap(),
302-
);
303-
}
304-
Ok(())
305-
}
306-
}
307-
308-
#[async_trait::async_trait]
309-
impl Middleware for GithubMiddleware {
310-
async fn handle(
311-
&self,
312-
mut request: Request,
313-
extensions: &mut Extensions,
314-
next: Next<'_>,
315-
) -> reqwest_middleware::Result<Response> {
316-
match GithubMiddleware::add_headers(&mut request) {
317-
Ok(()) => next.run(request, extensions).await,
318-
Err(error) => Err(reqwest_middleware::Error::Middleware(error.into())),
319-
}
320-
}
321-
}
322-
323-
/// Creates a new reqwest client with middleware for tracing, GitHub, and retrying transient errors.
302+
/// Creates a new reqwest client with middleware for tracing, and retrying transient errors.
324303
fn reqwest_client() -> ClientWithMiddleware {
325304
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
326305
ClientBuilder::new(reqwest::Client::new())
327306
.with(TracingMiddleware::default())
328-
.with(GithubMiddleware)
329307
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
330308
.build()
331309
}

postgresql_archive/src/repository/maven/repository.rs

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use crate::Error::{ArchiveHashMismatch, ParseError, RepositoryFailure, VersionNo
55
use crate::{hasher, Result};
66
use async_trait::async_trait;
77
use futures_util::StreamExt;
8-
use http::{header, Extensions};
9-
use reqwest::{Request, Response};
10-
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, Middleware, Next};
8+
use reqwest::header::HeaderMap;
9+
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
1110
use reqwest_retry::policies::ExponentialBackoff;
1211
use reqwest_retry::RetryTransientMiddleware;
1312
use reqwest_tracing::TracingMiddleware;
@@ -58,7 +57,7 @@ impl Maven {
5857
debug!("Attempting to locate release for version requirement {version_req}");
5958
let client = reqwest_client();
6059
let url = format!("{}/maven-metadata.xml", self.url);
61-
let request = client.get(&url);
60+
let request = client.get(&url).headers(Self::headers());
6261
let response = request.send().await?.error_for_status()?;
6362
let text = response.text().await?;
6463
let metadata: Metadata =
@@ -86,6 +85,13 @@ impl Maven {
8685
None => Err(VersionNotFound(version_req.to_string())),
8786
}
8887
}
88+
89+
/// Returns the headers for the Maven request.
90+
fn headers() -> HeaderMap {
91+
let mut headers = HeaderMap::new();
92+
headers.append("User-Agent", USER_AGENT.parse().unwrap());
93+
headers
94+
}
8995
}
9096

9197
#[async_trait]
@@ -125,13 +131,13 @@ impl Repository for Maven {
125131
let archive_hash_url = format!("{archive_url}.{extension}");
126132
let client = reqwest_client();
127133
debug!("Downloading archive hash {archive_hash_url}");
128-
let request = client.get(&archive_hash_url);
134+
let request = client.get(&archive_hash_url).headers(Self::headers());
129135
let response = request.send().await?.error_for_status()?;
130136
let hash = response.text().await?;
131137
debug!("Archive hash {archive_hash_url} downloaded: {}", hash.len(),);
132138

133139
debug!("Downloading archive {archive_url}");
134-
let request = client.get(&archive_url);
140+
let request = client.get(&archive_url).headers(Self::headers());
135141
let response = request.send().await?.error_for_status()?;
136142
#[cfg(feature = "indicatif")]
137143
let span = tracing::Span::current();
@@ -159,40 +165,11 @@ impl Repository for Maven {
159165
}
160166
}
161167

162-
/// Middleware to add headers to the request.
163-
#[derive(Debug)]
164-
struct MavenMiddleware;
165-
166-
impl MavenMiddleware {
167-
#[expect(clippy::unnecessary_wraps)]
168-
fn add_headers(request: &mut Request) -> Result<()> {
169-
let headers = request.headers_mut();
170-
headers.append(header::USER_AGENT, USER_AGENT.parse().unwrap());
171-
Ok(())
172-
}
173-
}
174-
175-
#[async_trait::async_trait]
176-
impl Middleware for MavenMiddleware {
177-
async fn handle(
178-
&self,
179-
mut request: Request,
180-
extensions: &mut Extensions,
181-
next: Next<'_>,
182-
) -> reqwest_middleware::Result<Response> {
183-
match MavenMiddleware::add_headers(&mut request) {
184-
Ok(()) => next.run(request, extensions).await,
185-
Err(error) => Err(reqwest_middleware::Error::Middleware(error.into())),
186-
}
187-
}
188-
}
189-
190-
/// Creates a new reqwest client with middleware for tracing, GitHub, and retrying transient errors.
168+
/// Creates a new reqwest client with middleware for tracing, and retrying transient errors.
191169
fn reqwest_client() -> ClientWithMiddleware {
192170
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
193171
ClientBuilder::new(reqwest::Client::new())
194172
.with(TracingMiddleware::default())
195-
.with(MavenMiddleware)
196173
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
197174
.build()
198175
}

0 commit comments

Comments
 (0)