Repository navigation
Expand file tree
/
Copy pathteam_api.rs
More file actions
82 lines (74 loc) · 2.89 KB
/
Copy pathteam_api.rs
File metadata and controls
82 lines (74 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::sync::utils::ResponseExt;
use log::{debug, trace};
use std::borrow::Cow;
use std::path::PathBuf;
/// Determines how do we get access to the ground-truth data from `rust-lang/team`.
pub enum TeamApi {
/// Access the live data from the published production REST API.
Production,
/// Directly access a directory with prebuilt JSON data.
Prebuilt(PathBuf),
}
impl TeamApi {
pub(crate) async fn get_teams(&self) -> anyhow::Result<Vec<rust_team_data::v1::Team>> {
debug!("loading teams list from the Team API");
Ok(self
.req::<rust_team_data::v1::Teams>("teams.json")
.await?
.teams
.into_iter()
.map(|(_k, v)| v)
.collect())
}
pub(crate) async fn get_repos(&self) -> anyhow::Result<Vec<rust_team_data::v1::Repo>> {
debug!("loading teams list from the Team API");
Ok(self
.req::<rust_team_data::v1::Repos>("repos.json")
.await?
.repos
.into_iter()
.flat_map(|(_k, v)| v)
.collect())
}
pub(crate) async fn get_lists(&self) -> anyhow::Result<rust_team_data::v1::Lists> {
debug!("loading email lists list from the Team API");
self.req::<rust_team_data::v1::Lists>("lists.json").await
}
pub(crate) async fn get_zulip_groups(&self) -> anyhow::Result<rust_team_data::v1::ZulipGroups> {
debug!("loading Zulip groups from the Team API");
self.req::<rust_team_data::v1::ZulipGroups>("zulip-groups.json")
.await
}
pub(crate) async fn get_zulip_map(&self) -> anyhow::Result<rust_team_data::v1::ZulipMapping> {
debug!("loading GitHub id to Zulip id map from the Team API");
self.req::<rust_team_data::v1::ZulipMapping>("zulip-map.json")
.await
}
pub(crate) async fn get_zulip_streams(
&self,
) -> anyhow::Result<rust_team_data::v1::ZulipStreams> {
debug!("loading Zulip streams from the Team API");
self.req::<rust_team_data::v1::ZulipStreams>("zulip-streams.json")
.await
}
async fn req<T: serde::de::DeserializeOwned>(&self, url: &str) -> anyhow::Result<T> {
match self {
TeamApi::Production => {
let base = std::env::var("TEAM_DATA_BASE_URL")
.map(Cow::Owned)
.unwrap_or_else(|_| Cow::Borrowed(rust_team_data::v1::BASE_URL));
let url = format!("{base}/{url}");
trace!("http request: GET {url}");
Ok(reqwest::get(&url)
.await?
.error_for_status()?
.json_annotated()
.await?)
}
TeamApi::Prebuilt(directory) => {
let contents = std::fs::read(directory.join("v1").join(url))?;
Ok(serde_json::from_slice(&contents)?)
}
}
}
}