Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<Source> for GitProvider #17

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::GitProvider;
use figment::{
providers::{Format, Serialized, Toml},
Figment,
Expand Down Expand Up @@ -110,6 +111,15 @@ struct Source {
url: String,
}

impl From<Source> for GitProvider {
fn from(source: Source) -> Self {
match source.provider {
GitProviderType::Github => GitProvider::github(source.url.parse().unwrap()),
GitProviderType::Gitlab => GitProvider::gitlab(source.url.parse().unwrap()),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
22 changes: 16 additions & 6 deletions src/provider/core.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
use super::{github::Github, gitlab::Gitlab};
use crate::SshPublicKey;
use reqwest::{Client, Url};

/// A Git provider.
#[derive(Debug)]
pub enum GitProvider<'a> {
Github(Github<'a>),
Gitlab(Gitlab<'a>),
pub enum GitProvider {
Github(Github),
Gitlab(Gitlab),
}

impl GitProvider<'_> {
impl GitProvider {
pub fn github(url: Url) -> Self {
Self::Github(Github::new(url))
}

pub fn gitlab(url: Url) -> Self {
Self::Gitlab(Gitlab::new(url))
}

/// Get the public keys of a user by their username.
async fn get_keys_by_username(
&self,
username: &str,
client: &Client,
) -> Result<Vec<SshPublicKey>, reqwest::Error> {
match self {
Self::Github(provider) => provider.get_keys_by_username(username).await,
Self::Gitlab(provider) => provider.get_keys_by_username(username).await,
Self::Github(provider) => provider.get_keys_by_username(username, client).await,
Self::Gitlab(provider) => provider.get_keys_by_username(username, client).await,
}
}
}
25 changes: 14 additions & 11 deletions src/provider/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ use crate::{SshPublicKey, USER_AGENT};
use reqwest::{Client, Result, Url};

#[derive(Debug)]
pub struct Github<'a> {
pub struct Github {
/// The base URL of the API.
base_url: Url,
/// The client used to make requests to the API.
client: &'a Client,
}

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

pub fn new(base_url: Url) -> Self {
Self { base_url }
}

/// Get the signing keys of a user by their username.
///
/// # API documentation
/// https://docs.github.com/en/rest/users/ssh-signing-keys?apiVersion=2022-11-28#list-ssh-signing-keys-for-a-user
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
pub async fn get_keys_by_username(
&self,
username: &str,
client: &Client,
) -> Result<Vec<SshPublicKey>> {
let url = self
.base_url
.join(&format!("/users/{username}/ssh_signing_keys"))
.unwrap();
let request = self
.client
let request = client
.get(url)
.header("User-Agent", USER_AGENT)
.header("Accept", Self::ACCEPT_HEADER)
Expand Down Expand Up @@ -59,9 +64,8 @@ mod tests {
let client = Client::new();
let api = Github {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
let _ = api.get_keys_by_username(username).await;
let _ = api.get_keys_by_username(username, &client).await;

mock.assert();
}
Expand Down Expand Up @@ -114,9 +118,8 @@ mod tests {
let client = Client::new();
let api = Github {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
let keys = api.get_keys_by_username(username).await.unwrap();
let keys = api.get_keys_by_username(username, &client).await.unwrap();

assert_eq!(keys, expected);
}
Expand Down
25 changes: 14 additions & 11 deletions src/provider/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ use reqwest::{Client, Result, Url};
use serde::Deserialize;

#[derive(Debug)]
pub struct Gitlab<'a> {
pub struct Gitlab {
/// The base URL of the API.
base_url: Url,
/// The client used to make requests to the API.
client: &'a Client,
}

impl Gitlab<'_> {
impl Gitlab {
const VERSION: &'static str = "v4";
const ACCEPT_HEADER: &'static str = "application/json";

pub fn new(base_url: Url) -> Self {
Self { base_url }
}

/// Get the signing keys of a user by their username.
///
/// # API documentation
/// https://docs.gitlab.com/16.10/ee/api/users.html#list-ssh-keys-for-user
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
pub async fn get_keys_by_username(
&self,
username: &str,
client: &Client,
) -> Result<Vec<SshPublicKey>> {
let url = self
.base_url
.join(&format!(
"/api/{version}/users/{username}/keys",
version = Self::VERSION,
))
.unwrap();
let request = self
.client
let request = client
.get(url)
.header("User-Agent", USER_AGENT)
.header("Accept", Self::ACCEPT_HEADER);
Expand Down Expand Up @@ -102,9 +107,8 @@ mod tests {
let client = Client::new();
let api = Gitlab {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
let _ = api.get_keys_by_username(username).await;
let _ = api.get_keys_by_username(username, &client).await;

mock.assert();
}
Expand Down Expand Up @@ -162,9 +166,8 @@ mod tests {
let client = Client::new();
let api = Gitlab {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
let keys = api.get_keys_by_username(username).await.unwrap();
let keys = api.get_keys_by_username(username, &client).await.unwrap();

assert_eq!(keys, expected);
}
Expand Down