From 155895d188ad6199247f1831b7188018532385d7 Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 10 Jul 2025 13:58:58 -0600 Subject: refactor: extract an api type --- src/authorization/entities.rs | 62 +++++++------------------------------------ 1 file changed, 10 insertions(+), 52 deletions(-) (limited to 'src/authorization') diff --git a/src/authorization/entities.rs b/src/authorization/entities.rs index b4f5a762..6e7fd568 100644 --- a/src/authorization/entities.rs +++ b/src/authorization/entities.rs @@ -1,4 +1,4 @@ -use crate::gitlab::{Group, Member, Project}; +use crate::gitlab::Api; use serde::Serialize; use std::collections::HashSet; @@ -25,37 +25,23 @@ pub struct CedarParent { } pub struct EntitiesRepository { - pub token: String, - pub host: String, - pub project_url: String, + api: Api, + project: String, } impl EntitiesRepository { pub fn new(token: String, host: String, project: String) -> EntitiesRepository { EntitiesRepository { - token: token, - host: host.clone(), - project_url: format!( - "{}/api/v4/projects/{}", - host.trim_end_matches('/'), - urlencoding::encode(&project) - ), + api: Api::new(token, host), + project, } } pub async fn all(&self) -> Result, Box> { - let http = reqwest::Client::new(); let mut entities = Vec::new(); let mut groups = HashSet::new(); - let project: Project = http - .get(&self.project_url) - .header("PRIVATE-TOKEN", &self.token) - .send() - .await? - .error_for_status()? - .json() - .await?; + let project = self.api.get_project(&self.project).await?; entities.push(CedarEntity { uid: CedarUid { @@ -77,20 +63,7 @@ impl EntitiesRepository { }, }); - let members_url = format!( - "{}/api/v4/projects/{}/members/all", - self.host.trim_end_matches('/'), - project.id - ); - - let members: Vec = http - .get(&members_url) - .header("PRIVATE-TOKEN", &self.token) - .send() - .await? - .error_for_status()? - .json() - .await?; + let members = self.api.get_project_members(project.id).await?; for member in members { if member.state == "active" { @@ -110,7 +83,7 @@ impl EntitiesRepository { } if project.namespace.kind == "group" { - self.fetch_hierarchy(&http, project.namespace.id, &mut entities, &mut groups) + self.fetch_hierarchy(project.namespace.id, &mut entities, &mut groups) .await?; } @@ -119,7 +92,6 @@ impl EntitiesRepository { fn fetch_hierarchy<'a>( &'a self, - client: &'a reqwest::Client, group_id: u64, entities: &'a mut Vec, groups: &'a mut HashSet, @@ -133,24 +105,10 @@ impl EntitiesRepository { groups.insert(group_id); - let group_url = format!( - "{}/api/v4/groups/{}", - self.host.trim_end_matches('/'), - group_id - ); - - let group: Group = client - .get(&group_url) - .header("PRIVATE-TOKEN", &self.token) - .send() - .await? - .error_for_status()? - .json() - .await?; + let group = self.api.get_group(group_id).await?; let parents = if let Some(parent_id) = group.parent_id { - self.fetch_hierarchy(client, parent_id, entities, groups) - .await?; + self.fetch_hierarchy(parent_id, entities, groups).await?; vec![CedarParent { parent_type: "Group".to_string(), id: parent_id.to_string(), -- cgit v1.2.3