gfas_api/
lib.rs

1//! This crate exports some GitHub API bindings through [`GitHub`].
2
3use std::collections::HashSet;
4use std::ops::{Deref, DerefMut};
5
6use octorust::Client;
7use octorust::auth::Credentials;
8
9type Result<T> = std::result::Result<T, octorust::ClientError>;
10
11/// Asynchronous GitHub API bindings that wraps [`octorust::Client`] internally.
12///
13/// # Examples
14///
15/// ```rust
16/// use gfas_api::GitHub;
17///
18/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
19/// let github = GitHub::new(String::from("<TOKEN>"))?;
20/// # Ok(())
21/// # }
22/// ```
23#[derive(Clone)]
24#[repr(transparent)]
25pub struct GitHub(Client);
26
27impl Deref for GitHub {
28	type Target = Client;
29
30	fn deref(&self) -> &Self::Target { &self.0 }
31}
32
33impl DerefMut for GitHub {
34	fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
35}
36
37impl GitHub {
38	const USER_AGENT: &'static str = "gfas";
39
40	/// Create a new GitHub API client.
41	#[allow(clippy::result_large_err)]
42	pub fn new(token: String) -> Result<Self> {
43		Ok(Self(Client::new(Self::USER_AGENT, Credentials::Token(token))?))
44	}
45
46	/// List all followings of given user.
47	///
48	/// # Errors
49	///
50	/// Fails if an error occurs during sending requests.
51	pub async fn list_followings(&self, user: &str) -> Result<HashSet<String>> {
52		Ok(
53			self
54				.users()
55				.list_all_following_for_user(user)
56				.await?
57				.body
58				.into_iter()
59				.map(|u| u.login)
60				.collect()
61		)
62	}
63
64	/// List all followings of given user.
65	///
66	/// # Errors
67	///
68	/// Fails if an error occurs during sending requests.
69	pub async fn list_followers(&self, user: &str) -> Result<HashSet<String>> {
70		Ok(
71			self
72				.users()
73				.list_all_followers_for_user(user)
74				.await?
75				.body
76				.into_iter()
77				.map(|u| u.login)
78				.collect()
79		)
80	}
81}