use crate::syntax::Did;
use crate::identity::DidDocument;
use crate::identity::IdentityError;
pub struct PlcClient {
url: String,
http: reqwest::Client,
}
impl PlcClient {
pub fn new(url: &str) -> Self {
PlcClient {
url: url.to_string(),
http: reqwest::Client::new(),
}
}
pub fn production() -> Self {
Self::new("https://plc.directory")
}
pub async fn resolve(&self, did: &Did) -> Result<DidDocument, IdentityError> {
let url = format!("{}/{}", self.url, did.as_str());
let resp = self
.http
.get(&url)
.send()
.await
.map_err(|e| IdentityError::Network(e.to_string()))?;
if !resp.status().is_success() {
return Err(IdentityError::NotFound(did.to_string()));
}
resp.json()
.await
.map_err(|e| IdentityError::InvalidDocument(e.to_string()))
}
}