use super::prelude::*;
use crate::{
methods::Pagination,
model::gists::{Gist, SimpleGist},
};
use std::{collections::HashMap, fmt::Display};
user_and_pagination_methods!(
get_user_gists,
EndPoints::GetUsersusernameGists,
Vec<SimpleGist>
);
pub async fn delete_gist<T, A>(client: &T, gist_id: A) -> Result<(), GithubRestError>
where
T: Requester,
A: Into<String>,
{
client
.raw_req::<String, String>(EndPoints::DeleteGistsgistId(gist_id.into()), None, None)
.await?;
Ok(())
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct CreateGistBody {
pub files: HashMap<String, FileContents>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub public: bool,
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct FileContents {
pub content: String,
}
impl<T: Display> From<T> for FileContents {
fn from(val: T) -> Self {
FileContents {
content: format!("{val}"),
}
}
}
pub async fn create_gist<T>(client: &T, body: &CreateGistBody) -> Result<Gist, GithubRestError>
where
T: Requester,
{
client
.req::<String, String, Gist>(EndPoints::PostGists(), None, Some(serde_json::to_string(body).unwrap()))
.await
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct PatchGistBody {
pub files: HashMap<String, FileContents>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
pub async fn patch_gist<T, A>(client: &T, gist_id: A, body: &PatchGistBody) -> Result<Gist, GithubRestError>
where
T: Requester,
A: Into<String>,
{
client
.req::<String, String, Gist>(
EndPoints::PatchGistsgistId(gist_id.into()),
None,
Some(serde_json::to_string(&body).unwrap()),
)
.await
}
#[cfg(feature = "client")]
#[cfg(test)]
mod tests {
use crate::{client::DefaultRequester, methods::util};
use super::*;
const GIST_ID: &str = "c2196b06d002d4ee278175bb82454a95";
#[tokio::test]
async fn test_get_user_gists() {
let requester = DefaultRequester::new_none();
let res = get_user_gists(&requester, "tricked-dev", None).await.unwrap();
dbg!(res);
}
#[tokio::test]
async fn test_create_gist() {
let mut files = HashMap::new();
files.insert("1.rs".to_owned(), r#"fn main() { println!("testing")}"#.into());
let body = CreateGistBody {
files,
..Default::default()
};
let res = create_gist(&util::github_auth(), &body).await.unwrap();
dbg!(res);
}
#[tokio::test]
async fn test_delete_gist() {
delete_gist(&util::github_auth(), GIST_ID).await.unwrap()
}
#[tokio::test]
async fn test_patch_gist() {
let body = PatchGistBody {
description: Some("Something".to_owned()),
..Default::default()
};
let res = patch_gist(&util::github_auth(), GIST_ID, &body).await.unwrap();
dbg!(res);
}
}