Factor more bookkeeping into check_response

This commit is contained in:
asonix 2022-11-16 12:38:34 -06:00
parent 0d42f72f87
commit 2dd1dfe43f

View file

@ -1,7 +1,7 @@
use crate::error::{Error, ErrorKind}; use crate::error::{Error, ErrorKind};
use activitystreams::iri_string::types::IriString; use activitystreams::iri_string::types::IriString;
use actix_web::{http::header::Date, web::Bytes}; use actix_web::{http::header::Date, web::Bytes};
use awc::{Client, ClientResponse}; use awc::{error::SendRequestError, Client, ClientResponse};
use dashmap::DashMap; use dashmap::DashMap;
use http_signature_normalization_actix::prelude::*; use http_signature_normalization_actix::prelude::*;
use rand::thread_rng; use rand::thread_rng;
@ -203,8 +203,16 @@ impl Requests {
async fn check_response( async fn check_response(
&self, &self,
parsed_url: &IriString, parsed_url: &IriString,
res: &mut ClientResponse, res: Result<ClientResponse, SendRequestError>,
) -> Result<(), Error> { ) -> Result<ClientResponse, Error> {
if res.is_err() {
self.count_err();
self.breakers.fail(&parsed_url);
}
let mut res =
res.map_err(|e| ErrorKind::SendRequest(parsed_url.to_string(), e.to_string()))?;
self.reset_err(); self.reset_err();
if !res.status().is_success() { if !res.status().is_success() {
@ -226,7 +234,7 @@ impl Requests {
self.breakers.succeed(&parsed_url); self.breakers.succeed(&parsed_url);
Ok(()) Ok(res)
} }
#[tracing::instrument(name = "Fetch Json", skip(self), fields(signing_string))] #[tracing::instrument(name = "Fetch Json", skip(self), fields(signing_string))]
@ -275,14 +283,7 @@ impl Requests {
.send() .send()
.await; .await;
if res.is_err() { let mut res = self.check_response(&parsed_url, res).await?;
self.count_err();
self.breakers.fail(&parsed_url);
}
let mut res = res.map_err(|e| ErrorKind::SendRequest(url.to_string(), e.to_string()))?;
self.check_response(&parsed_url, &mut res).await?;
let body = res let body = res
.body() .body()
@ -320,14 +321,7 @@ impl Requests {
.send() .send()
.await; .await;
if res.is_err() { let mut res = self.check_response(&parsed_url, res).await?;
self.breakers.fail(&parsed_url);
self.count_err();
}
let mut res = res.map_err(|e| ErrorKind::SendRequest(url.to_string(), e.to_string()))?;
self.check_response(&parsed_url, &mut res).await?;
let content_type = if let Some(content_type) = res.headers().get("content-type") { let content_type = if let Some(content_type) = res.headers().get("content-type") {
if let Ok(s) = content_type.to_str() { if let Ok(s) = content_type.to_str() {
@ -387,14 +381,7 @@ impl Requests {
let res = req.send_body(body).await; let res = req.send_body(body).await;
if res.is_err() { self.check_response(&inbox, res).await?;
self.count_err();
self.breakers.fail(&inbox);
}
let mut res = res.map_err(|e| ErrorKind::SendRequest(inbox.to_string(), e.to_string()))?;
self.check_response(&inbox, &mut res).await?;
Ok(()) Ok(())
} }