5
1
Fork 0

Merge pull request #9 from Plume-org/update-deps

Update deps + asyncify fetching
pull/13/head
Gelez vor 4 Jahren committet von GitHub
Commit c49467766c
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23

@ -11,9 +11,10 @@ license = "GPL-3.0"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
reqwest = "0.9" reqwest = { version = "0.10", features = [ "json" ] }
serde = { version = "1.0", features = [ "derive" ] } serde = { version = "1.0", features = [ "derive" ] }
[dev-dependencies] [dev-dependencies]
serde_json = "1.0" 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. /// 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, prefix: Prefix,
acct: impl Into<String>, acct: impl Into<String>,
with_https: bool, with_https: bool,
@ -129,27 +129,30 @@ pub fn resolve_with_prefix(
.get(&url[..]) .get(&url[..])
.header(ACCEPT, "application/jrd+json, application/json") .header(ACCEPT, "application/jrd+json, application/json")
.send() .send()
.map_err(|_| WebfingerError::HttpError) .await
.and_then(|mut r| r.json().map_err(|_| WebfingerError::JsonError)) .map_err(|_| WebfingerError::HttpError)?
.json()
.await
.map_err(|_| WebfingerError::JsonError)
} }
/// Fetches a Webfinger resource. /// Fetches a Webfinger resource.
/// ///
/// If the resource doesn't have a prefix, `acct:` will be used. /// 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 acct = acct.into();
let mut parsed = acct.splitn(2, ':'); let mut parsed = acct.splitn(2, ':');
let first = parsed.next().ok_or(WebfingerError::ParseError)?; let first = parsed.next().ok_or(WebfingerError::ParseError)?;
if first.contains('@') { if first.contains('@') {
// This : was a port number, not a prefix // 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 { } else {
if let Some(other) = parsed.next() { 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 { } else {
// fallback to acct: // 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 super::*;
use serde_json; use serde_json;
use tokio::runtime::Runtime;
#[test] #[test]
fn test_url_for() { fn test_url_for() {
@ -35,6 +36,7 @@ fn test_url_for() {
#[test] #[test]
fn test_resolve() { fn test_resolve() {
let mut r = Runtime::new().unwrap();
let m = mockito::mock("GET", mockito::Matcher::Any) let m = mockito::mock("GET", mockito::Matcher::Any)
.with_body( .with_body(
r#" r#"
@ -66,10 +68,12 @@ fn test_resolve() {
let url = format!("test@{}", mockito::server_url()).replace("http://", ""); let url = format!("test@{}", mockito::server_url()).replace("http://", "");
println!("{}", url); println!("{}", url);
let res = resolve(url, false).unwrap(); r.block_on(async {
assert_eq!(res.subject, String::from("acct:test@example.org")); let res = resolve(url, false).await.unwrap();
assert_eq!(res.subject, String::from("acct:test@example.org"));
m.assert(); m.assert();
});
} }
#[test] #[test]

Laden…
Abbrechen
Speichern