Skip to content

Commit 1eefd82

Browse files
committed
Add basic TeamRepo trait
1 parent 90d2405 commit 1eefd82

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

Cargo.lock

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ slow-tests = []
4949
[dependencies]
5050
anyhow = "=1.0.76"
5151
async-trait = "=0.1.77"
52+
mockall = "=0.12.1"
5253
aws-credential-types = { version = "=1.1.1", features = ["hardcoded-credentials"] }
5354
aws-ip-ranges = "=0.42.0"
5455
aws-sdk-cloudfront = "=1.9.0"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub mod sql;
5555
pub mod ssh;
5656
pub mod storage;
5757
pub mod tasks;
58+
pub mod team_repo;
5859
mod test_util;
5960
pub mod typosquat;
6061
pub mod util;

src/team_repo.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! The code in this module interacts with the
2+
//! <https://github.com/rust-lang/team/> repository.
3+
//!
4+
//! The [TeamRepo] trait is used to abstract away the HTTP client for testing
5+
//! purposes. The [TeamRepoImpl] struct is the actual implementation of
6+
//! the trait.
7+
8+
use async_trait::async_trait;
9+
use mockall::automock;
10+
use reqwest::Client;
11+
12+
#[automock]
13+
#[async_trait]
14+
pub trait TeamRepo {
15+
async fn get_team(&self, name: &str) -> anyhow::Result<Team>;
16+
}
17+
18+
#[derive(Debug, Clone, Deserialize)]
19+
pub struct Team {
20+
pub name: String,
21+
pub kind: String,
22+
pub members: Vec<Member>,
23+
}
24+
25+
#[derive(Debug, Clone, Deserialize)]
26+
pub struct Member {
27+
pub name: String,
28+
pub github: String,
29+
pub github_id: i32,
30+
pub is_lead: bool,
31+
}
32+
33+
pub struct TeamRepoImpl {
34+
client: Client,
35+
}
36+
37+
impl TeamRepoImpl {
38+
pub fn new(client: Client) -> Self {
39+
TeamRepoImpl { client }
40+
}
41+
}
42+
43+
#[async_trait]
44+
impl TeamRepo for TeamRepoImpl {
45+
async fn get_team(&self, name: &str) -> anyhow::Result<Team> {
46+
let url = format!("https://team-api.infra.rust-lang.org/v1/teams/{name}.json");
47+
let response = self.client.get(url).send().await?.error_for_status()?;
48+
Ok(response.json().await?)
49+
}
50+
}

0 commit comments

Comments
 (0)