git-gemini-forge 0.6.6

A simple Gemini server that serves a read-only view of public repositories from a Git forge.
use crate::config::SecretHeader;
use crate::handlers::templates::PercentEncoded;
use crate::network::error;
use crate::network::net::Net;
use crate::network::responses::*;
use crate::network::{ForgeApi, ForgeApiKind};
use async_trait::async_trait;
use reqwest::header::AUTHORIZATION;
use secrecy::ExposeSecret;
use url::Url;

/// Describes the Forgejo API.
///
/// See https://code.forgejo.org/api/swagger and https://gitea.com/api/swagger
#[derive(Clone, Debug)]
pub struct ForgejoApi {
	_base_url: Url,
	_kind: ForgeApiKind,
	_private_token: Option<SecretHeader>,
}

impl ForgejoApi {
	/// Constructs a reference to the API with the given forge URL, API kind, and secret.
	pub fn from_url(
		forge_url: Url,
		kind: ForgeApiKind,
		private_token: &Option<SecretHeader>,
	) -> Self {
		let base_url: Url = forge_url.join("/api/v1/").expect("Valid URL path");
		ForgejoApi {
			_base_url: base_url,
			_kind: kind,
			_private_token: private_token.clone(),
		}
	}
}

#[async_trait]
impl ForgeApi for ForgejoApi {
	fn kind(&self) -> ForgeApiKind {
		self._kind
	}

	fn access_token_header_name(&self) -> reqwest::header::HeaderName {
		AUTHORIZATION
	}

	fn access_token_value(&self) -> Option<SecretHeader> {
		self._private_token
			.as_ref()
			.map(|token| SecretHeader::from(token.expose_secret().with_prefix("token ")))
	}

	async fn check_access_token(&self, net: &Net) -> Result<(), error::Error> {
		match self.access_token_value() {
			None => Ok(()),
			Some(_) => super::get_authed_user(self, net).await.map(|_| ()),
		}
	}

	fn base_url(&self) -> &Url {
		&self._base_url
	}

	async fn get_branches(
		&self,
		net: &Net,
		username: &PercentEncoded,
		repo_name: &PercentEncoded,
	) -> Result<Vec<Branch>, error::Error> {
		super::get_branches(self, net, username, repo_name).await
	}

	async fn get_contents(
		&self,
		net: &Net,
		username: &PercentEncoded,
		repo_name: &PercentEncoded,
		branch_name: &PercentEncoded,
		item_path: &PercentEncoded,
	) -> Result<Contents<Item>, error::Error> {
		super::get_contents(self, net, username, repo_name, branch_name, item_path).await
	}

	async fn get_file_tree(
		&self,
		net: &Net,
		username: &PercentEncoded,
		repo_name: &PercentEncoded,
	) -> Result<Vec<Item>, error::Error> {
		super::get_file_tree(self, net, username, repo_name).await
	}

	async fn get_recent_repos(&self, net: &Net) -> Result<Vec<Repo>, error::Error> {
		super::get_recent_repos(self, net).await
	}

	async fn get_repo(
		&self,
		net: &Net,
		username: &PercentEncoded,
		repo_name: &PercentEncoded,
	) -> Result<Repo, error::Error> {
		super::get_repo(self, net, username, repo_name).await
	}

	async fn get_user_repos(
		&self,
		net: &Net,
		username: &PercentEncoded,
	) -> Result<Vec<Repo>, error::Error> {
		super::get_user_repos(self, net, username).await
	}

	async fn get_users(&self, net: &Net) -> Result<Vec<User>, error::Error> {
		super::get_users(self, net).await
	}

	async fn get_version(&self, net: &Net) -> Result<ServerVersion, error::Error> {
		super::get_version(self, net).await
	}
}