Update deps + asyncify fetching

pull/9/head
Ana Gelez 4 years ago
parent ecb8a183bf
commit cdaab95ed5

@ -11,9 +11,10 @@ license = "GPL-3.0"
edition = "2018"
[dependencies]
reqwest = "0.9"
reqwest = { version = "0.10", features = [ "json" ] }
serde = { version = "1.0", features = [ "derive" ] }
[dev-dependencies]
serde_json = "1.0"
mockito = "0.16"
mockito = "0.22"
tokio = "0.2"

@ -119,7 +119,7 @@ pub fn url_for(
}
/// Fetches a WebFinger resource, identified by the `acct` parameter, a Webfinger URI.
pub fn resolve_with_prefix(
pub async fn resolve_with_prefix(
prefix: Prefix,
acct: impl Into<String>,
with_https: bool,
@ -129,27 +129,30 @@ pub fn resolve_with_prefix(
.get(&url[..])
.header(ACCEPT, "application/jrd+json, application/json")
.send()
.map_err(|_| WebfingerError::HttpError)
.and_then(|mut r| r.json().map_err(|_| WebfingerError::JsonError))
.await
.map_err(|_| WebfingerError::HttpError)?
.json()
.await
.map_err(|_| WebfingerError::JsonError)
}
/// Fetches a Webfinger resource.
///
/// If the resource doesn't have a prefix, `acct:` will be used.
pub fn resolve(acct: impl Into<String>, with_https: bool) -> Result<Webfinger, WebfingerError> {
pub async fn resolve(acct: impl Into<String>, with_https: bool) -> Result<Webfinger, WebfingerError> {
let acct = acct.into();
let mut parsed = acct.splitn(2, ':');
let first = parsed.next().ok_or(WebfingerError::ParseError)?;
if first.contains('@') {
// This : was a port number, not a prefix
resolve_with_prefix(Prefix::Acct, acct, with_https)
resolve_with_prefix(Prefix::Acct, acct, with_https).await
} else {
if let Some(other) = parsed.next() {
resolve_with_prefix(Prefix::from(first), other, with_https)
resolve_with_prefix(Prefix::from(first), other, with_https).await
} else {
// fallback to acct:
resolve_with_prefix(Prefix::Acct, first, with_https)
resolve_with_prefix(Prefix::Acct, first, with_https).await
}
}
}

@ -1,5 +1,6 @@
use super::*;
use serde_json;
use tokio::runtime::Runtime;
#[test]
fn test_url_for() {
@ -35,6 +36,7 @@ fn test_url_for() {
#[test]
fn test_resolve() {
let mut r = Runtime::new().unwrap();
let m = mockito::mock("GET", mockito::Matcher::Any)
.with_body(
r#"
@ -66,10 +68,12 @@ fn test_resolve() {
let url = format!("test@{}", mockito::server_url()).replace("http://", "");
println!("{}", url);
let res = resolve(url, false).unwrap();
assert_eq!(res.subject, String::from("acct:test@example.org"));
r.block_on(async {
let res = resolve(url, false).await.unwrap();
assert_eq!(res.subject, String::from("acct:test@example.org"));
m.assert();
m.assert();
});
}
#[test]

Loading…
Cancel
Save