From 3e4a4958c3b6960296801aef68cbc2b19b6b4a43 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 28 Apr 2024 20:17:25 +0000 Subject: [PATCH 1/2] Pass cleint instead of holding reference in provider --- src/provider/core.rs | 14 ++++++++------ src/provider/github.rs | 21 ++++++++++----------- src/provider/gitlab.rs | 21 ++++++++++----------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/provider/core.rs b/src/provider/core.rs index 32ddd3e..05faa74 100644 --- a/src/provider/core.rs +++ b/src/provider/core.rs @@ -1,22 +1,24 @@ use super::{github::Github, gitlab::Gitlab}; use crate::SshPublicKey; +use reqwest::Client; /// 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 { /// Get the public keys of a user by their username. async fn get_keys_by_username( &self, username: &str, + client: &Client, ) -> Result, 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, } } } diff --git a/src/provider/github.rs b/src/provider/github.rs index 3a20fde..fb91869 100644 --- a/src/provider/github.rs +++ b/src/provider/github.rs @@ -2,14 +2,12 @@ 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"; @@ -17,13 +15,16 @@ impl Github<'_> { /// /// # 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> { + pub async fn get_keys_by_username( + &self, + username: &str, + client: &Client, + ) -> Result> { 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) @@ -59,9 +60,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(); } @@ -114,9 +114,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); } diff --git a/src/provider/gitlab.rs b/src/provider/gitlab.rs index b6d2451..102df59 100644 --- a/src/provider/gitlab.rs +++ b/src/provider/gitlab.rs @@ -3,14 +3,12 @@ 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"; @@ -18,7 +16,11 @@ impl Gitlab<'_> { /// /// # 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> { + pub async fn get_keys_by_username( + &self, + username: &str, + client: &Client, + ) -> Result> { let url = self .base_url .join(&format!( @@ -26,8 +28,7 @@ impl Gitlab<'_> { version = Self::VERSION, )) .unwrap(); - let request = self - .client + let request = client .get(url) .header("User-Agent", USER_AGENT) .header("Accept", Self::ACCEPT_HEADER); @@ -102,9 +103,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(); } @@ -162,9 +162,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); } From f03dc465f4dad98a1a7f2bb097c19d49084e0b89 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 28 Apr 2024 20:30:54 +0000 Subject: [PATCH 2/2] Enable converting config `Source` to `GitProvider` --- src/config.rs | 10 ++++++++++ src/provider/core.rs | 10 +++++++++- src/provider/github.rs | 4 ++++ src/provider/gitlab.rs | 4 ++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 8ba2761..e8d7069 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,4 @@ +use crate::GitProvider; use figment::{ providers::{Format, Serialized, Toml}, Figment, @@ -110,6 +111,15 @@ struct Source { url: String, } +impl From 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::*; diff --git a/src/provider/core.rs b/src/provider/core.rs index 05faa74..b4adea8 100644 --- a/src/provider/core.rs +++ b/src/provider/core.rs @@ -1,6 +1,6 @@ use super::{github::Github, gitlab::Gitlab}; use crate::SshPublicKey; -use reqwest::Client; +use reqwest::{Client, Url}; /// A Git provider. #[derive(Debug)] @@ -10,6 +10,14 @@ pub enum 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, diff --git a/src/provider/github.rs b/src/provider/github.rs index fb91869..5f95f86 100644 --- a/src/provider/github.rs +++ b/src/provider/github.rs @@ -11,6 +11,10 @@ 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 diff --git a/src/provider/gitlab.rs b/src/provider/gitlab.rs index 102df59..d2b4337 100644 --- a/src/provider/gitlab.rs +++ b/src/provider/gitlab.rs @@ -12,6 +12,10 @@ 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