use crate::handlers::templates::PercentEncoded;
use crate::network::error;
use crate::network::net::Net;
use crate::network::responses::*;
use crate::network::{ForgeApi, ForgeApiKind};
use isahc::http::header::AUTHORIZATION;
use secrecy::{ExposeSecret, SecretString};
use url::Url;
#[derive(Clone, Debug)]
pub struct ForgejoApi {
_base_url: Url,
_kind: ForgeApiKind,
_private_token: Option<SecretString>,
}
impl ForgejoApi {
pub fn from_url(
forge_url: Url,
kind: ForgeApiKind,
private_token: &Option<SecretString>,
) -> Self {
let base_url: Url = forge_url.join("/api/v1/").expect("Valid URL path");
return ForgejoApi {
_base_url: base_url,
_kind: kind,
_private_token: private_token.clone(),
};
}
}
impl ForgeApi for ForgejoApi {
fn kind(&self) -> ForgeApiKind {
self._kind
}
fn access_token_header_name(&self) -> isahc::http::HeaderName {
AUTHORIZATION
}
fn access_token_value(&self) -> Option<SecretString> {
match &self._private_token {
None => None,
Some(token) => Some(SecretString::from(format!(
"token {}",
token.expose_secret()
))),
}
}
fn check_access_token(&self, net: &Net) -> Result<(), error::Error> {
match self.access_token_value() {
None => Ok(()),
Some(_) => super::get_authed_user(self, net).map(|_| ()),
}
}
fn base_url(&self) -> &Url {
&self._base_url
}
fn get_branches(
&self,
net: &Net,
username: &PercentEncoded,
repo_name: &PercentEncoded,
) -> Result<Vec<Branch>, error::Error> {
super::get_branches(self, net, username, repo_name)
}
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)
}
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)
}
fn get_recent_repos(&self, net: &Net) -> Result<Vec<Repo>, error::Error> {
super::get_recent_repos(self, net)
}
fn get_repo(
&self,
net: &Net,
username: &PercentEncoded,
repo_name: &PercentEncoded,
) -> Result<Repo, error::Error> {
super::get_repo(self, net, username, repo_name)
}
fn get_user_repos(
&self,
net: &Net,
username: &PercentEncoded,
) -> Result<Vec<Repo>, error::Error> {
super::get_user_repos(self, net, username)
}
fn get_users(&self, net: &Net) -> Result<Vec<User>, error::Error> {
super::get_users(self, net)
}
fn get_version(&self, net: &Net) -> Result<ServerVersion, error::Error> {
super::get_version(self, net)
}
}