Skip to main content

aeo_protocol/
client.rs

1use crate::{AeoError, Document};
2
3const WELL_KNOWN_PATH: &str = "/.well-known/aeo.json";
4const ACCEPT_HEADER: &str = "application/aeo+json, application/json";
5
6/// Build the canonical well-known URL for an origin.
7///
8/// Strips any trailing slashes from the origin and appends the AEO
9/// Protocol well-known path.
10pub fn well_known_url(origin: &str) -> String {
11    format!("{}{}", origin.trim_end_matches('/'), WELL_KNOWN_PATH)
12}
13
14/// Fetch and parse the AEO declaration at `origin`'s well-known URL.
15///
16/// Returns an [`AeoError`] for non-2xx responses, network errors,
17/// or malformed documents.
18pub fn fetch_well_known(origin: &str) -> Result<Document, AeoError> {
19    let url = well_known_url(origin);
20    let response = ureq::get(&url)
21        .set("Accept", ACCEPT_HEADER)
22        .call()
23        .map_err(|e| match e {
24            ureq::Error::Status(status, _) => AeoError::HttpStatus {
25                status,
26                url: url.clone(),
27            },
28            other => AeoError::Http(Box::new(other)),
29        })?;
30
31    let body = response.into_string()?;
32    Document::from_json(&body)
33}