use super::converters::GitLabProject;
use super::GitLabApi;
use crate::handlers::templates::PercentEncoded;
use crate::network::error;
use crate::network::net::{ApiEndpoint, Net};
use crate::network::ForgeApi;
use url::Url;
struct UserReposEndpoint<'a> {
api: &'a GitLabApi,
base_url: Url,
user_id: &'a PercentEncoded,
}
impl ApiEndpoint<GitLabApi, Vec<GitLabProject>> for UserReposEndpoint<'_> {
fn api(&self) -> &GitLabApi {
self.api
}
fn url(&self) -> Url {
let mut url = self
.base_url
.join(&format!("users/{}/projects", self.user_id.uri_component()))
.expect("Valid URL path");
url.set_query(Some("per_page=50&page=1")); url
}
}
pub fn get_user_repos(
api: &GitLabApi,
net: &Net,
user_id: &PercentEncoded,
) -> Result<Vec<GitLabProject>, error::Error> {
let base_url = api.base_url().clone();
let endpoint = UserReposEndpoint {
api,
base_url,
user_id,
};
let mut repos = net.call(&endpoint)?;
repos.sort_by(|a, b| {
return b.updated_at.cmp(&a.updated_at);
});
return Ok(repos);
}