| 301 | } |
| 302 | |
| 303 | async fn download_example( |
| 304 | name: &str, |
| 305 | cache: Option<PathBuf>, |
| 306 | authority: Option<&str>, |
| 307 | ) -> Result<String> { |
| 308 | let authority = authority.unwrap_or(EXAMPLES_URL); |
| 309 | let target = format!("{authority}/{name}"); |
| 310 | |
| 311 | tracing::debug!(?target, "downloading remote example"); |
| 312 | let response = reqwest::get(&target) |
| 313 | .await |
| 314 | .into_diagnostic() |
| 315 | .wrap_err("error dowloading example data")?; |
| 316 | |
| 317 | if response.status() != StatusCode::OK { |
| 318 | Err(InvokeError::ExampleDownloadFailed(target, response).into()) |
| 319 | } else { |
| 320 | let content = response |
| 321 | .text() |
| 322 | .await |
| 323 | .into_diagnostic() |
| 324 | .wrap_err("error reading example data")?; |
| 325 | |
| 326 | if let Some(cache) = cache { |
| 327 | tracing::debug!(?cache, "storing example in cache"); |
| 328 | create_dir_all(cache.parent().unwrap()).into_diagnostic()?; |
| 329 | let mut dest = File::create(cache).into_diagnostic()?; |
| 330 | copy(&mut content.as_bytes(), &mut dest).into_diagnostic()?; |
| 331 | } |
| 332 | Ok(content) |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | fn parse_invoke_ip_address(address: &str) -> Result<String> { |
| 337 | let invoke_address = IpAddr::from_str(address).map_err(|e| miette::miette!(e))?; |