1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! The users API.
use std::backtrace::Backtrace;
use http::StatusCode;
pub use self::follow::{ListUserFollowerBuilder, ListUserFollowingBuilder};
pub use self::hovercard::HovercardBuilder;
pub use self::user_gpg_keys::{ListUserGpgKeysBuilder, UserGpgKeysOpsBuilder};
use self::user_repos::ListUserReposBuilder;
use crate::api::activity::starring::ListReposStarredByUserBuilder;
use crate::api::activity::watching::ListUserSubscriptionsBuilder;
pub use crate::api::billing::ScopedBillingHandler as UserBillingHandler;
use crate::api::events::EventsBuilder;
use crate::api::users::user_blocks::BlockedUsersBuilder;
use crate::api::users::user_emails::UserEmailsOpsBuilder;
use crate::api::users::user_git_ssh_keys::UserGitSshKeysOpsBuilder;
use crate::api::users::user_social_accounts::UserSocialAccountsOpsBuilder;
use crate::api::users::user_ssh_signing_keys::UserSshSigningKeysOpsBuilder;
use crate::models::UserId;
use crate::params::users::emails::EmailVisibilityState;
use crate::{error, GitHubError, Octocrab};
mod follow;
mod hovercard;
mod user_blocks;
mod user_emails;
mod user_git_ssh_keys;
mod user_gpg_keys;
mod user_repos;
mod user_social_accounts;
mod user_ssh_signing_keys;
pub(crate) enum UserRef {
ByString(String),
ById(UserId),
}
impl std::fmt::Display for UserRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UserRef::ByString(str) => write!(f, "users/{str}"),
UserRef::ById(id) => write!(f, "user/{id}"),
}
}
}
/// Handler for GitHub's users API.
///
/// Created with [`Octocrab::users`].
pub struct UserHandler<'octo> {
crab: &'octo Octocrab,
user: UserRef,
}
impl<'octo> UserHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, user: UserRef) -> Self {
Self { crab, user }
}
/// Handle billing for this user.
///
/// See: <https://docs.github.com/en/rest/billing?apiVersion=2022-11-28>
pub fn billing(&self) -> crate::api::billing::ScopedBillingHandler<'octo> {
let username = match &self.user {
UserRef::ByString(name) => name.clone(),
UserRef::ById(id) => id.to_string(),
};
crate::api::billing::ScopedBillingHandler::new(
self.crab,
crate::api::billing::BillingOwner::User(username),
)
}
/// Handle packages for this user.
///
/// See: https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28
pub fn packages(&self) -> crate::api::packages::PackagesHandler<'octo> {
let username = match &self.user {
UserRef::ByString(name) => name.clone(),
UserRef::ById(id) => id.to_string(),
};
crate::api::packages::PackagesHandler::new(
self.crab,
crate::api::packages::PackagesOwner::User(username),
)
}
/// Get this users profile info
pub async fn profile(&self) -> crate::Result<crate::models::UserProfile> {
// build the route to get info on this user
let route = format!("/{}", self.user);
// get info on the specified user
self.crab.get(route, None::<&()>).await
}
/// Gets a user installation for the authenticated app.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-a-user-installation-for-the-authenticated-app)
pub async fn installation(&self) -> crate::Result<crate::models::Installation> {
let route = format!("/{}/installation", self.user);
self.crab.get(route, None::<&()>).await
}
/// List this users that follow this user
pub fn followers(&self) -> ListUserFollowerBuilder<'_, '_> {
ListUserFollowerBuilder::new(self)
}
/// List this user is following
pub fn following(&self) -> ListUserFollowingBuilder<'_, '_> {
ListUserFollowingBuilder::new(self)
}
pub fn repos(&self) -> ListUserReposBuilder<'_, '_> {
ListUserReposBuilder::new(self)
}
/// Lists organizations for the specified user.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#list-organizations-for-a-user)
pub fn list_orgs(&self) -> crate::api::current::ListUserOrgsBuilder<'octo> {
crate::api::current::ListUserOrgsBuilder::new(self.crab, format!("/{}/orgs", self.user))
}
/// Lists repositories watched by this user.
///
/// [See the GitHub API documentation](https://docs.github.com/en/rest/activity/watching?apiVersion=2022-11-28#list-repositories-watched-by-a-user)
pub fn subscriptions(&self) -> ListUserSubscriptionsBuilder<'octo> {
ListUserSubscriptionsBuilder::new(self.crab, format!("/{}/subscriptions", self.user))
}
/// Lists repositories starred by this user.
///
/// [See the GitHub API documentation](https://docs.github.com/en/rest/activity/starring?apiVersion=2022-11-28#list-repositories-starred-by-a-user)
pub fn starred(&self) -> ListReposStarredByUserBuilder<'octo> {
ListReposStarredByUserBuilder::with_route(self.crab, format!("/{}/starred", self.user))
}
/// List events for this user.
///
/// [See the GitHub API documentation](https://docs.github.com/en/rest/activity/events?apiVersion=2022-11-28#list-events-for-the-authenticated-user)
pub fn events(&self) -> EventsBuilder<'octo> {
EventsBuilder::with_route(self.crab, format!("/{}/events", self.user))
}
/// List public events for this user.
///
/// [See the GitHub API documentation](https://docs.github.com/en/rest/activity/events?apiVersion=2022-11-28#list-public-events-for-a-user)
pub fn public_events(&self) -> EventsBuilder<'octo> {
EventsBuilder::with_route(self.crab, format!("/{}/events/public", self.user))
}
/// List events received by this user.
///
/// [See the GitHub API documentation](https://docs.github.com/en/rest/activity/events?apiVersion=2022-11-28#list-events-received-by-the-authenticated-user)
pub fn received_events(&self) -> EventsBuilder<'octo> {
EventsBuilder::with_route(self.crab, format!("/{}/received_events", self.user))
}
/// List public events received by this user.
///
/// [See the GitHub API documentation](https://docs.github.com/en/rest/activity/events?apiVersion=2022-11-28#list-public-events-received-by-a-user)
pub fn public_received_events(&self) -> EventsBuilder<'octo> {
EventsBuilder::with_route(self.crab, format!("/{}/received_events/public", self.user))
}
/// List organization events for this user.
///
/// [See the GitHub API documentation](https://docs.github.com/en/rest/activity/events?apiVersion=2022-11-28#list-organization-events-for-the-authenticated-user)
pub fn org_events(&self, org: impl AsRef<str>) -> EventsBuilder<'octo> {
EventsBuilder::with_route(
self.crab,
format!("/{}/events/orgs/{}", self.user, org.as_ref()),
)
}
/// API for listing blocked users
/// you must pass authentication information with your requests
pub fn blocks(&self) -> BlockedUsersBuilder<'_, '_> {
BlockedUsersBuilder::new(self)
}
///## Check if a user is blocked by the authenticated user
///works with the following token types:
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)
///
///The token must have the following permission set: `blocking:read`
///
///```no_run
/// async fn run() -> octocrab::Result<bool> {
/// let is_blocked = octocrab::instance()
/// .users("current_user")
/// .is_blocked("some_user")
/// .await?;
/// Ok(is_blocked)
/// }
pub async fn is_blocked(&self, username: &str) -> crate::Result<bool> {
let route = format!("/user/blocks/{username}");
let response = self.crab._get(route).await?;
Ok(response.status() == 204)
}
///## Blocks the given user
///works with the following token types:
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)
///
///The token must have the following permission set: `blocking:read`
///
///```no_run
/// async fn run() -> octocrab::Result<()> {
/// octocrab::instance()
/// .users("current_user")
/// .block_user("some_user")
/// .await
/// }
pub async fn block_user(&self, username: &str) -> crate::Result<()> {
let route = format!("/user/blocks/{username}");
/* '204 not found' is returned if user blocked */
let result: crate::Result<()> = self.crab.put(route, None::<&()>).await;
match result {
Ok(_) => Err(error::Error::GitHub {
source: Box::new(GitHubError {
status_code: StatusCode::OK,
documentation_url: None,
errors: None,
message: "".to_string(),
}),
backtrace: Backtrace::capture(),
}),
Err(_v) => Ok(()),
}
}
///## Unblocks the given user
///works with the following token types:
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)
///
///The token must have the following permission set: `blocking:read`
///
///```no_run
/// async fn run() -> octocrab::Result<()> {
/// octocrab::instance()
/// .users("current_user")
/// .unblock_user("some_user")
/// .await
/// }
pub async fn unblock_user(&self, username: &str) -> crate::Result<()> {
let route = format!("/user/blocks/{username}");
self.crab.delete(route, None::<&()>).await
}
///## Set primary email visibility for the authenticated user
///works with the following token types:
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)
///
///The fine-grained token must have the following permission set:
///
///"Email addresses" user permissions (write)
///
///```no_run
/// use octocrab::params::users::emails::EmailVisibilityState::*;
/// use octocrab::models::UserEmailInfo;
///
/// async fn run() -> octocrab::Result<Vec<UserEmailInfo>> {
/// octocrab::instance()
/// .users("current_user")
/// .set_primary_email_visibility(Public) // or Private
/// .await
/// }
pub async fn set_primary_email_visibility(
&self,
visibility: EmailVisibilityState,
) -> crate::Result<Vec<crate::models::UserEmailInfo>> {
let route = String::from("/user/email/visibility");
let params = serde_json::json!({
"visibility": serde_json::to_string(&visibility).unwrap(),
});
self.crab.patch(route, Some(¶ms)).await
}
///Email addresses operations builder
///* List email addresses for the authenticated user
///* Add an email address for the authenticated user
///* Delete an email address for the authenticated user
pub fn emails(&self) -> UserEmailsOpsBuilder<'_, '_> {
UserEmailsOpsBuilder::new(self)
}
///GPG Keys operations builder
///* List GPG keys for the authenticated user
///* Get a GPG key for the authenticated user
///* Add an GPG key for the authenticated user
///* Delete a GPG key for the authenticated user
pub fn gpg_keys(&self) -> UserGpgKeysOpsBuilder<'_, '_> {
UserGpgKeysOpsBuilder::new(self)
}
/// List GPG keys for the given user, allowing for pagination.
///
/// See: [GitHub API Documentation][docs] for `GET /users/{username}/gpg_keys`
///
/// # Examples
///
/// * Fetch 10 recent GPG keys for the user with login "foouser":
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let gpg_keys = octocrab::instance()
/// .users("foouser")
/// .list_user_gpg_keys()
/// .page(1u32)
/// .per_page(10u8)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
///
/// [docs]: https://docs.github.com/en/rest/users/gpg-keys?apiVersion=2022-11-28#list-gpg-keys-for-a-user
pub fn list_user_gpg_keys(&self) -> ListUserGpgKeysBuilder<'_, '_> {
ListUserGpgKeysBuilder::new(self)
}
///Git SSH keys operations builder
///* List public SSH keys for the authenticated user
///* Create a public SSH key for the authenticated user
///* Delete a public SSH key for the authenticated user
pub fn git_ssh_keys(&self) -> UserGitSshKeysOpsBuilder<'_, '_> {
UserGitSshKeysOpsBuilder::new(self)
}
///Social accounts operations builder
///* List social accounts for the authenticated user
///* Add social accounts for the authenticated user
///* Delete social accounts for the authenticated user
pub fn social_accounts(&self) -> UserSocialAccountsOpsBuilder<'_, '_> {
UserSocialAccountsOpsBuilder::new(self)
}
///SSH signing key administration
///* List SSH signing keys for the authenticated user
///* Create an SSH signing key for the authenticated user
///* Get an SSH signing key for the authenticated user
///* Delete an SSH signing key for the authenticated user
pub fn ssh_signing_keys(&self) -> UserSshSigningKeysOpsBuilder<'_, '_> {
UserSshSigningKeysOpsBuilder::new(self)
}
/// Get contextual information for a user.
///
/// Provides hovercard information. You can find out more about someone in
/// relation to their pull requests, issues, repositories, and organizations.
///
/// See: [GitHub API Documentation][docs] for `GET /users/{username}/hovercard`
///
/// # Examples
///
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let hovercard = octocrab::instance()
/// .users("octocat")
/// .hovercard()
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
///
/// [docs]: https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-contextual-information-for-a-user
pub fn hovercard(&self) -> HovercardBuilder<'_, '_> {
HovercardBuilder::new(self)
}
}