team_data.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. use crate::github::GithubClient;
  2. use anyhow::Context as _;
  3. use rust_team_data::v1::{Teams, ZulipMapping, BASE_URL};
  4. use serde::de::DeserializeOwned;
  5. async fn by_url<T: DeserializeOwned>(client: &GithubClient, path: &str) -> anyhow::Result<T> {
  6. let base = std::env::var("TEAMS_API_URL").unwrap_or(BASE_URL.to_string());
  7. let url = format!("{}{}", base, path);
  8. for _ in 0i32..3 {
  9. let map: Result<T, _> = client.json(client.raw().get(&url)).await;
  10. match map {
  11. Ok(v) => return Ok(v),
  12. Err(e) => {
  13. if e.downcast_ref::<reqwest::Error>()
  14. .map_or(false, |e| e.is_timeout())
  15. {
  16. continue;
  17. } else {
  18. return Err(e);
  19. }
  20. }
  21. }
  22. }
  23. Err(anyhow::anyhow!("Failed to retrieve {} in 3 requests", url))
  24. }
  25. pub async fn zulip_map(client: &GithubClient) -> anyhow::Result<ZulipMapping> {
  26. by_url(client, "/zulip-map.json")
  27. .await
  28. .context("team-api: zulip-map.json")
  29. }
  30. pub async fn teams(client: &GithubClient) -> anyhow::Result<Teams> {
  31. by_url(client, "/teams.json")
  32. .await
  33. .context("team-api: teams.json")
  34. }