mirror of
https://github.com/Hopiu/lychee.git
synced 2026-04-18 20:21:04 +00:00
Make accepted codes non-optional
This commit is contained in:
parent
d22d1888f1
commit
3100fb2ee7
5 changed files with 12 additions and 15 deletions
|
|
@ -17,10 +17,7 @@ async fn main() -> Result<()> {
|
|||
let mut headers = HeaderMap::new();
|
||||
headers.insert(header::ACCEPT, "text/html".parse().unwrap());
|
||||
|
||||
let accepted = Some(HashSet::from_iter(vec![
|
||||
StatusCode::OK,
|
||||
StatusCode::NO_CONTENT,
|
||||
]));
|
||||
let accepted = HashSet::from_iter(vec![StatusCode::OK, StatusCode::NO_CONTENT]);
|
||||
|
||||
let client = ClientBuilder::builder()
|
||||
.excludes(excludes)
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ pub(crate) struct WebsiteChecker {
|
|||
/// Set of accepted return codes / status codes.
|
||||
///
|
||||
/// Unmatched return codes/ status codes are deemed as errors.
|
||||
accepted: Option<HashSet<StatusCode>>,
|
||||
accepted: HashSet<StatusCode>,
|
||||
|
||||
/// Requires using HTTPS when it's available.
|
||||
///
|
||||
|
|
@ -59,7 +59,7 @@ impl WebsiteChecker {
|
|||
retry_wait_time: Duration,
|
||||
max_retries: u64,
|
||||
reqwest_client: reqwest::Client,
|
||||
accepted: Option<HashSet<StatusCode>>,
|
||||
accepted: HashSet<StatusCode>,
|
||||
github_client: Option<Octocrab>,
|
||||
require_https: bool,
|
||||
plugin_request_chain: RequestChain,
|
||||
|
|
|
|||
|
|
@ -238,7 +238,10 @@ pub struct ClientBuilder {
|
|||
/// Set of accepted return codes / status codes.
|
||||
///
|
||||
/// Unmatched return codes/ status codes are deemed as errors.
|
||||
accepted: Option<HashSet<StatusCode>>,
|
||||
///
|
||||
/// TODO: accept all "valid" status codes by default. Maybe use `AcceptRange`?
|
||||
#[builder(default = HashSet::from([StatusCode::OK]))]
|
||||
accepted: HashSet<StatusCode>,
|
||||
|
||||
/// Response timeout per request in seconds.
|
||||
timeout: Option<Duration>,
|
||||
|
|
@ -633,7 +636,7 @@ mod tests {
|
|||
password: "pass".into(),
|
||||
});
|
||||
|
||||
let res = get_mock_client_response(r).await;
|
||||
let res = dbg!(get_mock_client_response(r).await);
|
||||
assert!(res.status().is_success());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -94,11 +94,6 @@ impl RetryExt for http::Error {
|
|||
|
||||
impl RetryExt for ErrorKind {
|
||||
fn should_retry(&self) -> bool {
|
||||
match self {
|
||||
Self::RejectedStatusCode(StatusCode::TOO_MANY_REQUESTS) => return true,
|
||||
_ => {}
|
||||
};
|
||||
|
||||
// If the error is a `reqwest::Error`, delegate to that
|
||||
if let Some(r) = self.reqwest_error() {
|
||||
r.should_retry()
|
||||
|
|
@ -110,6 +105,8 @@ impl RetryExt for ErrorKind {
|
|||
}) = self.github_error()
|
||||
{
|
||||
source.should_retry()
|
||||
} else if let Self::RejectedStatusCode(StatusCode::TOO_MANY_REQUESTS) = self {
|
||||
return true;
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,10 +83,10 @@ impl Serialize for Status {
|
|||
impl Status {
|
||||
#[must_use]
|
||||
/// Create a status object from a response and the set of accepted status codes
|
||||
pub fn new(response: &Response, accepted: Option<HashSet<StatusCode>>) -> Self {
|
||||
pub fn new(response: &Response, accepted: HashSet<StatusCode>) -> Self {
|
||||
let code = response.status();
|
||||
|
||||
if let Some(true) = accepted.map(|a| a.contains(&code)) {
|
||||
if accepted.contains(&code) {
|
||||
Self::Ok(code)
|
||||
} else {
|
||||
Self::Error(ErrorKind::RejectedStatusCode(code))
|
||||
|
|
|
|||
Loading…
Reference in a new issue