Skip to content

Commit f47c0da

Browse files
authored
Implement From<Source> for GitProvider (#17)
* Pass cleint instead of holding reference in provider * Enable converting config `Source` to `GitProvider`
1 parent 8b36f23 commit f47c0da

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

src/config.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::GitProvider;
12
use figment::{
23
providers::{Format, Serialized, Toml},
34
Figment,
@@ -110,6 +111,15 @@ struct Source {
110111
url: String,
111112
}
112113

114+
impl From<Source> for GitProvider {
115+
fn from(source: Source) -> Self {
116+
match source.provider {
117+
GitProviderType::Github => GitProvider::github(source.url.parse().unwrap()),
118+
GitProviderType::Gitlab => GitProvider::gitlab(source.url.parse().unwrap()),
119+
}
120+
}
121+
}
122+
113123
#[cfg(test)]
114124
mod tests {
115125
use super::*;

src/provider/core.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
use super::{github::Github, gitlab::Gitlab};
22
use crate::SshPublicKey;
3+
use reqwest::{Client, Url};
34

45
/// A Git provider.
56
#[derive(Debug)]
6-
pub enum GitProvider<'a> {
7-
Github(Github<'a>),
8-
Gitlab(Gitlab<'a>),
7+
pub enum GitProvider {
8+
Github(Github),
9+
Gitlab(Gitlab),
910
}
1011

11-
impl GitProvider<'_> {
12+
impl GitProvider {
13+
pub fn github(url: Url) -> Self {
14+
Self::Github(Github::new(url))
15+
}
16+
17+
pub fn gitlab(url: Url) -> Self {
18+
Self::Gitlab(Gitlab::new(url))
19+
}
20+
1221
/// Get the public keys of a user by their username.
1322
async fn get_keys_by_username(
1423
&self,
1524
username: &str,
25+
client: &Client,
1626
) -> Result<Vec<SshPublicKey>, reqwest::Error> {
1727
match self {
18-
Self::Github(provider) => provider.get_keys_by_username(username).await,
19-
Self::Gitlab(provider) => provider.get_keys_by_username(username).await,
28+
Self::Github(provider) => provider.get_keys_by_username(username, client).await,
29+
Self::Gitlab(provider) => provider.get_keys_by_username(username, client).await,
2030
}
2131
}
2232
}

src/provider/github.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@ use crate::{SshPublicKey, USER_AGENT};
22
use reqwest::{Client, Result, Url};
33

44
#[derive(Debug)]
5-
pub struct Github<'a> {
5+
pub struct Github {
66
/// The base URL of the API.
77
base_url: Url,
8-
/// The client used to make requests to the API.
9-
client: &'a Client,
108
}
119

12-
impl Github<'_> {
10+
impl Github {
1311
const VERSION: &'static str = "2022-11-28";
1412
const ACCEPT_HEADER: &'static str = "application/vnd.github+json";
1513

14+
pub fn new(base_url: Url) -> Self {
15+
Self { base_url }
16+
}
17+
1618
/// Get the signing keys of a user by their username.
1719
///
1820
/// # API documentation
1921
/// https://docs.github.com/en/rest/users/ssh-signing-keys?apiVersion=2022-11-28#list-ssh-signing-keys-for-a-user
20-
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
22+
pub async fn get_keys_by_username(
23+
&self,
24+
username: &str,
25+
client: &Client,
26+
) -> Result<Vec<SshPublicKey>> {
2127
let url = self
2228
.base_url
2329
.join(&format!("/users/{username}/ssh_signing_keys"))
2430
.unwrap();
25-
let request = self
26-
.client
31+
let request = client
2732
.get(url)
2833
.header("User-Agent", USER_AGENT)
2934
.header("Accept", Self::ACCEPT_HEADER)
@@ -59,9 +64,8 @@ mod tests {
5964
let client = Client::new();
6065
let api = Github {
6166
base_url: server.base_url().parse().unwrap(),
62-
client: &client,
6367
};
64-
let _ = api.get_keys_by_username(username).await;
68+
let _ = api.get_keys_by_username(username, &client).await;
6569

6670
mock.assert();
6771
}
@@ -114,9 +118,8 @@ mod tests {
114118
let client = Client::new();
115119
let api = Github {
116120
base_url: server.base_url().parse().unwrap(),
117-
client: &client,
118121
};
119-
let keys = api.get_keys_by_username(username).await.unwrap();
122+
let keys = api.get_keys_by_username(username, &client).await.unwrap();
120123

121124
assert_eq!(keys, expected);
122125
}

src/provider/gitlab.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@ use reqwest::{Client, Result, Url};
33
use serde::Deserialize;
44

55
#[derive(Debug)]
6-
pub struct Gitlab<'a> {
6+
pub struct Gitlab {
77
/// The base URL of the API.
88
base_url: Url,
9-
/// The client used to make requests to the API.
10-
client: &'a Client,
119
}
1210

13-
impl Gitlab<'_> {
11+
impl Gitlab {
1412
const VERSION: &'static str = "v4";
1513
const ACCEPT_HEADER: &'static str = "application/json";
1614

15+
pub fn new(base_url: Url) -> Self {
16+
Self { base_url }
17+
}
18+
1719
/// Get the signing keys of a user by their username.
1820
///
1921
/// # API documentation
2022
/// https://docs.gitlab.com/16.10/ee/api/users.html#list-ssh-keys-for-user
21-
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
23+
pub async fn get_keys_by_username(
24+
&self,
25+
username: &str,
26+
client: &Client,
27+
) -> Result<Vec<SshPublicKey>> {
2228
let url = self
2329
.base_url
2430
.join(&format!(
2531
"/api/{version}/users/{username}/keys",
2632
version = Self::VERSION,
2733
))
2834
.unwrap();
29-
let request = self
30-
.client
35+
let request = client
3136
.get(url)
3237
.header("User-Agent", USER_AGENT)
3338
.header("Accept", Self::ACCEPT_HEADER);
@@ -102,9 +107,8 @@ mod tests {
102107
let client = Client::new();
103108
let api = Gitlab {
104109
base_url: server.base_url().parse().unwrap(),
105-
client: &client,
106110
};
107-
let _ = api.get_keys_by_username(username).await;
111+
let _ = api.get_keys_by_username(username, &client).await;
108112

109113
mock.assert();
110114
}
@@ -162,9 +166,8 @@ mod tests {
162166
let client = Client::new();
163167
let api = Gitlab {
164168
base_url: server.base_url().parse().unwrap(),
165-
client: &client,
166169
};
167-
let keys = api.get_keys_by_username(username).await.unwrap();
170+
let keys = api.get_keys_by_username(username, &client).await.unwrap();
168171

169172
assert_eq!(keys, expected);
170173
}

0 commit comments

Comments
 (0)