team_data.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 url = format!("{}{}", BASE_URL, path);
  7. for _ in 0i32..3 {
  8. let map: Result<T, _> = client.json(client.raw().get(&url)).await;
  9. match map {
  10. Ok(v) => return Ok(v),
  11. Err(e) => {
  12. if e.downcast_ref::<reqwest::Error>()
  13. .map_or(false, |e| e.is_timeout())
  14. {
  15. continue;
  16. } else {
  17. return Err(e);
  18. }
  19. }
  20. }
  21. }
  22. Err(anyhow::anyhow!("Failed to retrieve {} in 3 requests", url))
  23. }
  24. pub async fn zulip_map(client: &GithubClient) -> anyhow::Result<ZulipMapping> {
  25. by_url(client, "/zulip-map.json")
  26. .await
  27. .context("team-api: zulip-map.json")
  28. }
  29. pub async fn teams(client: &GithubClient) -> anyhow::Result<Teams> {
  30. by_url(client, "/teams.json")
  31. .await
  32. .context("team-api: teams.json")
  33. }