asyncify reqwest calls (again?)

This commit is contained in:
Igor Galić 2020-02-14 19:29:30 +01:00
parent 82088596a8
commit a010025074
No known key found for this signature in database
GPG key ID: ACFEFF7F6A123A86

View file

@ -221,8 +221,8 @@ impl User {
.ok_or(Error::Webfinger)
}
fn fetch(url: &str) -> Result<CustomPerson> {
let mut res = ClientBuilder::new()
async fn fetch(url: &str) -> Result<CustomPerson> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(url)
@ -235,8 +235,8 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send().await?;
let text = &res.text().await?;
// without this workaround, publicKey is not correctly deserialized
let ap_sign = serde_json::from_str::<ApSignature>(text)?;
let mut json = serde_json::from_str::<CustomPerson>(text)?;
@ -244,12 +244,13 @@ impl User {
Ok(json)
}
pub fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> {
User::fetch(url).and_then(|json| User::from_activity(c, json))
pub async fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> {
let json = User::fetch(url).await?;
User::from_activity(c, json)
}
pub fn refetch(&self, conn: &Connection) -> Result<()> {
User::fetch(&self.ap_url.clone()).and_then(|json| {
pub async fn refetch(&self, conn: &Connection) -> Result<()> {
let json = User::fetch(&self.ap_url.clone()).await?;
let avatar = Media::save_remote(
conn,
json.object
@ -285,7 +286,6 @@ impl User {
.execute(conn)
.map(|_| ())
.map_err(Error::from)
})
}
pub fn hash_pass(pass: &str) -> Result<String> {
@ -356,8 +356,9 @@ impl User {
.set_part_of_link(Id::new(&self.outbox_url))?;
Ok(ActivityStream::new(coll))
}
fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
let mut res = ClientBuilder::new()
async fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(url)
@ -370,8 +371,8 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send().await?;
let text = &res.text().await?;
let json: serde_json::Value = serde_json::from_str(text)?;
let items = json["items"]
.as_array()
@ -386,8 +387,8 @@ impl User {
};
Ok((items, next))
}
pub fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
let mut res = ClientBuilder::new()
pub async fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(&self.outbox_url[..])
@ -400,13 +401,13 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send().await?;
let text = &res.text().await?;
let json: serde_json::Value = serde_json::from_str(text)?;
if let Some(first) = json.get("first") {
let mut items: Vec<T> = Vec::new();
let mut next = first.as_str().unwrap().to_owned();
while let Ok((mut page, nxt)) = self.fetch_outbox_page(&next) {
while let Ok((mut page, nxt)) = self.fetch_outbox_page(&next).await {
if page.is_empty() {
break;
}
@ -431,8 +432,8 @@ impl User {
}
}
pub fn fetch_followers_ids(&self) -> Result<Vec<String>> {
let mut res = ClientBuilder::new()
pub async fn fetch_followers_ids(&self) -> Result<Vec<String>> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(&self.followers_endpoint[..])
@ -445,8 +446,8 @@ impl User {
.join(", "),
)?,
)
.send()?;
let text = &res.text()?;
.send().await?;
let text = &res.text().await?;
let json: serde_json::Value = serde_json::from_str(text)?;
Ok(json["items"]
.as_array()