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

Use an enum for GitProvider instead of trait #16

Merged
merged 1 commit 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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.79"
chrono = "0.4.37"
clap = { version = "4.5.4", features = ["derive", "env", "string"] }
figment = { version = "0.10.16", features = ["toml"] }
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ mod allowed_signers;
pub mod cli;
mod config;
mod core;
mod github;
mod gitlab;
mod provider;
11 changes: 0 additions & 11 deletions src/provider.rs

This file was deleted.

22 changes: 22 additions & 0 deletions src/provider/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::{github::Github, gitlab::Gitlab};
use crate::SshPublicKey;

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

impl GitProvider<'_> {
/// Get the public keys of a user by their username.
async fn get_keys_by_username(
&self,
username: &str,
) -> 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,
}
}
}
18 changes: 6 additions & 12 deletions src/github.rs → src/provider/github.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
use crate::{GitProvider, SshPublicKey, USER_AGENT};
use async_trait::async_trait;
use crate::{SshPublicKey, USER_AGENT};
use reqwest::{Client, Result, Url};

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

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

#[async_trait]
impl GitProvider for Api<'_> {
type Err = reqwest::Error;

/// 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
async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
let url = self
.base_url
.join(&format!("/users/{username}/ssh_signing_keys"))
Expand Down Expand Up @@ -63,7 +57,7 @@ mod tests {
});

let client = Client::new();
let api = Api {
let api = Github {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
Expand Down Expand Up @@ -118,7 +112,7 @@ mod tests {
});

let client = Client::new();
let api = Api {
let api = Github {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
Expand Down
18 changes: 6 additions & 12 deletions src/gitlab.rs → src/provider/gitlab.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
use crate::{GitProvider, SshPublicKey, USER_AGENT};
use async_trait::async_trait;
use crate::{SshPublicKey, USER_AGENT};
use reqwest::{Client, Result, Url};
use serde::Deserialize;

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

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

#[async_trait]
impl GitProvider for Api<'_> {
type Err = reqwest::Error;

/// 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
async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
let url = self
.base_url
.join(&format!(
Expand Down Expand Up @@ -106,7 +100,7 @@ mod tests {
});

let client = Client::new();
let api = Api {
let api = Gitlab {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
Expand Down Expand Up @@ -166,7 +160,7 @@ mod tests {
});

let client = Client::new();
let api = Api {
let api = Gitlab {
base_url: server.base_url().parse().unwrap(),
client: &client,
};
Expand Down
5 changes: 5 additions & 0 deletions src/provider/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use core::GitProvider;

mod core;
mod github;
mod gitlab;