|
| 1 | +use crate::v0::support::{with_ipfs, StringError, StringSerialized}; |
| 2 | +use ipfs::{Ipfs, IpfsPath, IpfsTypes}; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use warp::{query, Filter, Rejection, Reply}; |
| 5 | + |
| 6 | +#[derive(Debug, Deserialize)] |
| 7 | +pub struct ResolveQuery { |
| 8 | + // the name to resolve |
| 9 | + arg: StringSerialized<IpfsPath>, |
| 10 | + #[serde(rename = "dht-record-count")] |
| 11 | + dht_record_count: Option<usize>, |
| 12 | + #[serde(rename = "dht-timeout")] |
| 13 | + dht_timeout: Option<String>, |
| 14 | +} |
| 15 | + |
| 16 | +pub fn resolve<T: IpfsTypes>( |
| 17 | + ipfs: &Ipfs<T>, |
| 18 | +) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone { |
| 19 | + with_ipfs(ipfs) |
| 20 | + .and(query::<ResolveQuery>()) |
| 21 | + .and_then(resolve_query) |
| 22 | +} |
| 23 | + |
| 24 | +async fn resolve_query<T: IpfsTypes>( |
| 25 | + ipfs: Ipfs<T>, |
| 26 | + query: ResolveQuery, |
| 27 | +) -> Result<impl Reply, Rejection> { |
| 28 | + let ResolveQuery { arg, .. } = query; |
| 29 | + let name = arg.into_inner(); |
| 30 | + let path = ipfs |
| 31 | + .resolve(&name) |
| 32 | + .await |
| 33 | + .map_err(StringError::from)? |
| 34 | + .to_string(); |
| 35 | + |
| 36 | + let response = ResolveResponse { path }; |
| 37 | + |
| 38 | + Ok(warp::reply::json(&response)) |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Debug, Serialize)] |
| 42 | +#[serde(rename_all = "PascalCase")] |
| 43 | +struct ResolveResponse { |
| 44 | + path: String, |
| 45 | +} |
0 commit comments