1
0
Fork 0

Merge pull request 'organize better' (#2) from organize into main

Reviewed-on: https://codeberg.org/Cyborus/forgejo-api/pulls/2
This commit is contained in:
Cyborus 2023-11-13 18:40:21 +00:00
commit 77e8b032a6
4 changed files with 1248 additions and 97 deletions

1129
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,12 @@ pub struct Forgejo {
client: Client,
}
mod repository;
mod user;
pub use repository::*;
pub use user::*;
#[derive(thiserror::Error, Debug)]
pub enum ForgejoError {
#[error("url must have a host")]
@ -65,31 +71,6 @@ impl Forgejo {
Ok(Self { url, client })
}
pub async fn get_repo(&self, user: &str, repo: &str) -> Result<Option<Repo>, ForgejoError> {
self.get_opt(&format!("repos/{user}/{repo}/")).await
}
pub async fn create_repo(&self, repo: CreateRepoOption) -> Result<Repo, ForgejoError> {
self.post("user/repos", &repo).await
}
/// Returns user info about the authorized user.
pub async fn myself(&self) -> Result<User, ForgejoError> {
self.get("user").await
}
pub async fn get_user(&self, user: &str) -> Result<Option<User>, ForgejoError> {
self.get_opt(&format!("users/{user}/")).await
}
pub async fn get_followers(&self, user: &str) -> Result<Option<Vec<User>>, ForgejoError> {
self.get_opt(&format!("users/{user}/followers/")).await
}
pub async fn get_following(&self, user: &str) -> Result<Option<Vec<User>>, ForgejoError> {
self.get_opt(&format!("users/{user}/following/")).await
}
async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T, ForgejoError> {
let url = self.url.join("api/v1/").unwrap().join(path).unwrap();
let request = self.client.get(url).build()?;
@ -149,75 +130,3 @@ struct ErrorMessage {
// url: Url
}
#[derive(serde::Deserialize, Debug, PartialEq)]
pub struct Repo {
pub clone_url: Url,
#[serde(with = "time::serde::rfc3339")]
pub created_at: time::OffsetDateTime,
pub default_branch: String,
pub description: String,
pub fork: bool,
pub forks_count: u64,
pub full_name: String,
pub owner: User,
}
#[derive(serde::Deserialize, Debug, PartialEq)]
pub struct User {
pub active: bool,
pub avatar_url: Url,
#[serde(with = "time::serde::rfc3339")]
pub created: time::OffsetDateTime,
pub description: String,
pub email: String,
pub followers_count: u64,
pub following_count: u64,
pub full_name: String,
pub id: u64,
pub is_admin: bool,
pub language: String,
#[serde(with = "time::serde::rfc3339")]
pub last_login: time::OffsetDateTime,
pub location: String,
pub login: String,
pub login_name: String,
pub prohibit_login: bool,
pub restricted: bool,
pub starred_repos_count: u64,
pub website: String,
}
#[derive(serde::Deserialize, Debug, PartialEq)]
pub enum UserVisibility {
#[serde(rename = "public")]
Public,
#[serde(rename = "limited")]
Limited,
#[serde(rename = "private")]
Private,
}
#[derive(serde::Serialize, Debug, PartialEq)]
pub struct CreateRepoOption {
pub auto_init: bool,
pub default_branch: String,
pub description: Option<String>,
pub gitignores: String,
pub issue_labels: String,
pub license: String,
pub name: String,
pub private: bool,
pub readme: String,
pub template: bool,
pub trust_model: TrustModel,
}
#[derive(serde::Serialize, Debug, PartialEq)]
pub enum TrustModel {
Default,
Collaborator,
Committer,
#[serde(rename = "collaboratorcommiter")]
CollaboratorCommitter,
}

52
src/repository.rs Normal file
View file

@ -0,0 +1,52 @@
use super::*;
/// Repository operations.
impl Forgejo {
/// Gets info about the specified repository.
pub async fn get_repo(&self, user: &str, repo: &str) -> Result<Option<Repo>, ForgejoError> {
self.get_opt(&format!("repos/{user}/{repo}/")).await
}
/// Creates a repository.
pub async fn create_repo(&self, repo: CreateRepoOption) -> Result<Repo, ForgejoError> {
self.post("user/repos", &repo).await
}
}
#[derive(serde::Deserialize, Debug, PartialEq)]
pub struct Repo {
pub clone_url: Url,
#[serde(with = "time::serde::rfc3339")]
pub created_at: time::OffsetDateTime,
pub default_branch: String,
pub description: String,
pub fork: bool,
pub forks_count: u64,
pub full_name: String,
pub owner: User,
}
#[derive(serde::Serialize, Debug, PartialEq)]
pub struct CreateRepoOption {
pub auto_init: bool,
pub default_branch: String,
pub description: Option<String>,
pub gitignores: String,
pub issue_labels: String,
pub license: String,
pub name: String,
pub private: bool,
pub readme: String,
pub template: bool,
pub trust_model: TrustModel,
}
#[derive(serde::Serialize, Debug, PartialEq)]
pub enum TrustModel {
Default,
Collaborator,
Committer,
#[serde(rename = "collaboratorcommiter")]
CollaboratorCommitter,
}

61
src/user.rs Normal file
View file

@ -0,0 +1,61 @@
use super::*;
/// User operations.
impl Forgejo {
/// Returns info about the authorized user.
pub async fn myself(&self) -> Result<User, ForgejoError> {
self.get("user").await
}
/// Returns info about the specified user.
pub async fn get_user(&self, user: &str) -> Result<Option<User>, ForgejoError> {
self.get_opt(&format!("users/{user}/")).await
}
/// Gets the list of users that follow the specified user.
pub async fn get_followers(&self, user: &str) -> Result<Option<Vec<User>>, ForgejoError> {
self.get_opt(&format!("users/{user}/followers/")).await
}
/// Gets the list of users the specified user is following.
pub async fn get_following(&self, user: &str) -> Result<Option<Vec<User>>, ForgejoError> {
self.get_opt(&format!("users/{user}/following/")).await
}
}
#[derive(serde::Deserialize, Debug, PartialEq)]
pub struct User {
pub active: bool,
pub avatar_url: Url,
#[serde(with = "time::serde::rfc3339")]
pub created: time::OffsetDateTime,
pub description: String,
pub email: String,
pub followers_count: u64,
pub following_count: u64,
pub full_name: String,
pub id: u64,
pub is_admin: bool,
pub language: String,
#[serde(with = "time::serde::rfc3339")]
pub last_login: time::OffsetDateTime,
pub location: String,
pub login: String,
pub login_name: String,
pub prohibit_login: bool,
pub restricted: bool,
pub starred_repos_count: u64,
pub website: String,
}
#[derive(serde::Deserialize, Debug, PartialEq)]
pub enum UserVisibility {
#[serde(rename = "public")]
Public,
#[serde(rename = "limited")]
Limited,
#[serde(rename = "private")]
Private,
}