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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Trait defition related to the User on Github
use hyper::status::StatusCode;
use json::{SSHKey, Email, User, PatchUser};
use requests::*;
use github::Client;
use error::*;
use serde_json;

/// Trait used to define access to endpoints grouped under `Users` in the Github API
/// specification
pub trait Users {
    // Finished -- for now
    // User
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user
    /// ### Description
    /// Returns a `User` Struct for the authenticated user.
    fn get_user(&self) -> Result<User>;

    /// ### Request Type:
    /// `PATCH`
    /// ### Endpoint:
    /// /user
    /// ### Description
    /// Returns a `User` struct of the authenticated user once their data has been updated
    fn patch_user(&self, user: PatchUser) -> Result<User>;

    // Users
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/keys
    /// ### Description
    /// Returns a Vector of all `SSHKeys` from the username specified
    fn get_users_username(&self, username: &str) -> Result<User>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users
    /// ### Description
    /// Returns a vector of `User`s from the website. This is a paginated request to the endpoint.
    fn get_users(&self, user_id: Option<u64>, per_page: Option<u64>) -> Result<Vec<User>>;

    // Followers User/Users
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/followers
    /// ### Description
    /// Returns a vector of `User`s followin the authenticated user. This is a paginated request
    /// to the endpoint.
    fn get_user_followers(&self,
                          page_num: Option<u64>,
                          per_page: Option<u64>)
                          -> Result<Vec<User>>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/followers
    /// ### Description
    /// Returns a vector of `User`s following the username passed into the function. This is a
    /// paginated request to the endpoint.
    fn get_users_username_followers(&self,
                                    username: &str,
                                    page_num: Option<u64>,
                                    per_page: Option<u64>)
                                    -> Result<Vec<User>>;

    // Email
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/emails
    /// ### Description
    /// Returns a vector of `Email`s of the authenticated user. This is a paginated request to the
    /// endpoint.
    fn get_user_emails(&self, page_num: Option<u64>, per_page: Option<u64>) -> Result<Vec<Email>>;

    /// ### Request Type:
    /// `POST`
    /// ### Endpoint:
    /// /user/emails
    /// ### Description
    /// Returns a vector of `Email`s when they've been added to the user's profile
    fn post_user_emails(&self, emails: Vec<String>) -> Result<Vec<Email>>;

    /// ### Request Type:
    /// `DELETE`
    /// ### Endpoint:
    /// /user/emails
    /// ### Description
    /// Returns a boolean value. True if the email(s) were deleted and false otherwise. The email
    /// strings must match verbatim for the corresponding email to be deleted.
    fn delete_user_emails(&self, emails: Vec<String>) -> Result<bool>;

    // User Following
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/following
    /// ### Description
    /// Returns a vector of all `User`s the authenticated user is following. This is a paginated
    /// request to the endpoint.
    fn get_user_following(&self,
                          page_num: Option<u64>,
                          per_page: Option<u64>)
                          -> Result<Vec<User>>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/following/:username
    /// ### Description
    /// Returns true if the authenticated user is following the username passed to the function and
    /// false otherwise.
    fn get_user_following_username(&self, username: &str) -> Result<bool>;

    /// ### Request Type:
    /// `PUT`
    /// ### Endpoint:
    /// /user/following/:username
    /// ### Description
    /// Returns true is the user is now following the given username and false otherwise
    fn put_user_following_username(&self, username: &str) -> Result<bool>;

    /// ### Request Type:
    /// `DELETE`
    /// ### Endpoint:
    /// /user/following/:username
    /// ### Description
    /// Returns true if the authenticated user unfollowed the username passed in and false
    /// otherwise.
    fn delete_user_following_username(&self, username: &str) -> Result<bool>;

    // Users Following
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/following
    /// ### Description
    /// Returns a vector of `User`s that the given username is following. This is a paginated
    /// request to the endpoint.
    fn get_users_username_following(&self,
                                    username: &str,
                                    page_num: Option<u64>,
                                    per_page: Option<u64>)
                                    -> Result<Vec<User>>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/following/:target_user
    /// ### Description
    /// Returns true is the username is following the target user and false otherwise.
    fn get_users_username_following_target_user(&self,
                                                username: &str,
                                                target_user: &str)
                                                -> Result<bool>;

    // Keys User
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/keys
    /// ### Description
    /// Returns a Vector of all `SSHKey`s that the authenticate user might have.
    fn get_user_keys(&self) -> Result<Vec<SSHKey>>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/keys/:id
    /// ### Description
    /// Returns an `SSHKey` to the user that is specified by id. If it does not exist a None value
    /// is returned.
    fn get_user_keys_id(&self, id: u64) -> Result<Option<SSHKey>>;

    /// ### Request Type:
    /// `POST`
    /// ### Endpoint:
    /// /user/keys
    /// ### Description
    /// Creates a new SSH key sending it back if it worked out properly.
    fn post_user_keys(&self, new_key: SSHKey) -> Result<SSHKey>;

    /// ### Request Type:
    /// `DELETE`
    /// ### Endpoint:
    /// /user/keys
    /// ### Description
    /// Returns a boolean value. True is the ssh key was deleted and false otherwise. The id number
    /// of the key is what is used to determine which one to delete.
    fn delete_user_keys(&self, id: u64) -> Result<bool>;

    // Keys Users
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/keys
    /// ### Description
    /// Returns a Vector of all `SSHKeys` from the username specified
    fn get_users_username_keys(&self, username: &str) -> Result<Vec<SSHKey>>;

    // Untestable for myself but will make on best guess anyways
    // fn put_users_username_site_admin(&self, username: &str) -> bool;
    // fn delete_users_username_site_admin(&self, username: &str) -> bool;
    // fn put_users_username_suspended(&self, username: &str) -> bool;
    // fn delete_users_username_suspended(&self, username: &str) -> bool;

    // Experimental subject to change in API. Don't Implement for now
    // fn get_user_gpg_keys(&self) -> Vec<GPGKey>
    // fn get_user_gpg_keys_id(&self, id: u64) -> GPGKey
    // fn post_user_gpg_keys(&self, key: GPGKeyPost) -> GPGKey
    // fn delete_user_gpg_keys_id(&self, id: u64) -> bool
}

// Doc comments are duplicated so that regardless of trait or client they see how to use it
impl Users for Client {
    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user
    /// ### Description
    /// Returns a `User` Struct for the authenticated user.
    fn get_user(&self) -> Result<User> {
        let url = "https://api.github.com/user";
        let data = get(url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&data))

    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username
    /// ### Description
    /// Returns a `User` Struct for the person whose name is passed into the function
    fn get_users_username(&self, username: &str) -> Result<User> {
        let mut url = String::from("https://api.github.com/users/");
        url.push_str(username);
        let data = get(&url, self.get_headers().clone())?;

        try_serde!(serde_json::from_str(&data))
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users
    /// ### Description
    /// Returns a vector of `User`s from the website. This is a paginated request to the endpoint.
    fn get_users(&self, user_id: Option<u64>, per_page: Option<u64>) -> Result<Vec<User>> {
        let mut url = String::from("https://api.github.com/users");
        if let Some(user_id) = user_id {
            url.push_str("?since=");
            url.push_str(&user_id.to_string());
        }
        if let Some(num) = per_page {
            url.push_str("&per_page=");
            url.push_str(&num.to_string());
        }

        let data = get(&url, self.get_headers().clone())?;

        try_serde!(serde_json::from_str(&data))
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/emails
    /// ### Description
    /// Returns a vector of `Email`s of the authenticated user. This is a paginated request to the
    /// endpoint.
    fn get_user_emails(&self, page_num: Option<u64>, per_page: Option<u64>) -> Result<Vec<Email>> {
        let mut url = String::from("https://api.github.com/user/emails");
        paginate!(url,page_num,per_page);
        let data = get(&url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&data))
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/followers
    /// ### Description
    /// Returns a vector of `User`s following the username passed into the function. This is a
    /// paginated request to the endpoint.
    fn get_users_username_followers(&self,
                                    username: &str,
                                    page_num: Option<u64>,
                                    per_page: Option<u64>)
                                    -> Result<Vec<User>> {
        let mut url = String::from("https://api.github.com/users/");
        url.push_str(username);
        url.push_str("/followers");
        paginate!(url,page_num,per_page);
        let data = get(&url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&data))
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/followers
    /// ### Description
    /// Returns a vector of `User`s followin the authenticated user. This is a paginated request
    /// to the endpoint.
    fn get_user_followers(&self,
                          page_num: Option<u64>,
                          per_page: Option<u64>)
                          -> Result<Vec<User>> {
        let mut url = String::from("https://api.github.com/user/followers");
        paginate!(url,page_num,per_page);
        let data = get(&url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&data))

    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/following
    /// ### Description
    /// Returns a vector of `User`s that the given username is following. This is a paginated
    /// request to the endpoint.
    fn get_users_username_following(&self,
                                    username: &str,
                                    page_num: Option<u64>,
                                    per_page: Option<u64>)
                                    -> Result<Vec<User>> {
        let mut url = String::from("https://api.github.com/users/");
        url.push_str(username);
        url.push_str("/following");
        paginate!(url,page_num,per_page);
        let data = get(&url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&data))

    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/following
    /// ### Description
    /// Returns a vector of all `User`s the authenticated user is following. This is a paginated
    /// request to the endpoint.
    fn get_user_following(&self,
                          page_num: Option<u64>,
                          per_page: Option<u64>)
                          -> Result<Vec<User>> {
        let mut url = String::from("https://api.github.com/user/following");
        paginate!(url,page_num,per_page);
        let data = get(&url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&data))

    }

    /// ### Request Type:
    /// `PATCH`
    /// ### Endpoint:
    /// /user
    /// ### Description
    /// Returns a `User` struct of the authenticated user once their data has been updated
    fn patch_user(&self, user: PatchUser) -> Result<User> {
        let url = "https://api.github.com/user";
        let res = patch(url,
                        self.get_headers().clone(),
                        serde_json::to_string(&user)?)?;
        try_serde!(serde_json::from_str(&res))
    }

    /// ### Request Type:
    /// `POST`
    /// ### Endpoint:
    /// /user/emails
    /// ### Description
    /// Returns a vector of `Email`s when they've been added to the user's profile
    fn post_user_emails(&self, emails: Vec<String>) -> Result<Vec<Email>> {
        let url = "https://api.github.com/user/emails";
        let res = post(url,
                       self.get_headers().clone(),
                       serde_json::to_string(&emails)?)?;
        try_serde!(serde_json::from_str(&res))
    }

    /// ### Request Type:
    /// `PUT`
    /// ### Endpoint:
    /// /user/following/:username
    /// ### Description
    /// Returns true is the user is now following the given username and false otherwise
    fn put_user_following_username(&self, username: &str) -> Result<bool> {
        let mut url = String::from("https://api.github.com/user/following/");
        url.push_str(username);
        let res = put(&url, self.get_headers().clone())?;
        Ok(res == StatusCode::NoContent)
    }

    /// ### Request Type:
    /// `DELETE`
    /// ### Endpoint:
    /// /user/following/:username
    /// ### Description
    /// Returns true if the authenticated user unfollowed the username passed in and false
    /// otherwise.
    fn delete_user_following_username(&self, username: &str) -> Result<bool> {
        let mut url = String::from("https://api.github.com/user/following/");
        url.push_str(username);
        let res = delete(&url, self.get_headers().clone())?;
        Ok(res == StatusCode::NoContent)
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/following/:username
    /// ### Description
    /// Returns true if the authenticated user is following the username passed to the function and
    /// false otherwise.
    fn get_user_following_username(&self, username: &str) -> Result<bool> {
        let mut url = String::from("https://api.github.com/user/following/");
        url.push_str(username);
        let res = get_status_code(&url, self.get_headers().clone())?;
        Ok(res == StatusCode::NoContent)
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/following/:target_user
    /// ### Description
    /// Returns true is the username is following the target user and false otherwise.
    fn get_users_username_following_target_user(&self,
                                                username: &str,
                                                target_user: &str)
                                                -> Result<bool> {
        let mut url = String::from("https://api.github.com/users/");
        url.push_str(username);
        url.push_str("/following/");
        url.push_str(target_user);
        let res = get_status_code(&url, self.get_headers().clone())?;
        Ok(res == StatusCode::NoContent)
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /users/:username/keys
    /// ### Description
    /// Returns a Vector of all `SSHKeys` from the username specified
    fn get_users_username_keys(&self, username: &str) -> Result<Vec<SSHKey>> {
        let mut url = String::from("https://api.github.com/users/");
        url.push_str(username);
        url.push_str("/keys");
        let res = get(&url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&res))
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/keys
    /// ### Description
    /// Returns a Vector of all `SSHKey`s that the authenticate user might have.
    fn get_user_keys(&self) -> Result<Vec<SSHKey>> {
        let url = "https://api.github.com/user/keys";
        let res = get(url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&res))
    }

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /user/keys/:id
    /// ### Description
    /// Returns an `SSHKey` to the user that is specified by id. If it does not exist a None value
    /// is returned.
    fn get_user_keys_id(&self, id: u64) -> Result<Option<SSHKey>> {
        let mut url = String::from("https://api.github.com/user/keys/");
        url.push_str(&id.to_string());
        let res = get(&url, self.get_headers().clone())?;

        // Try and fix this
        if let Ok(serial) = serde_json::from_str(&res) {
            Ok(Some(serial))
        } else {
            Ok(None)
        }
    }

    /// ### Request Type:
    /// `POST`
    /// ### Endpoint:
    /// /user/keys
    /// ### Description
    /// Creates a new SSH key sending it back if it worked out properly.
    fn post_user_keys(&self, new_key: SSHKey) -> Result<SSHKey> {
        let url = "https://api.github.com/user/keys";
        let res = post(url,
                       self.get_headers().clone(),
                       serde_json::to_string(&new_key)?)?;
        try_serde!(serde_json::from_str(&res))

    }

    /// ### Request Type:
    /// `DELETE`
    /// ### Endpoint:
    /// /user/keys
    /// ### Description
    /// Returns a boolean value. True is the ssh key was deleted and false otherwise. The id number
    /// of the key is what is used to determine which one to delete.
    fn delete_user_keys(&self, id: u64) -> Result<bool> {
        let mut url = String::from("https://api.github.com/user/keys");
        url.push_str(&id.to_string());
        let res = delete(&url, self.get_headers().clone())?;
        Ok(res == StatusCode::NoContent)
    }

    /// ### Request Type:
    /// `DELETE`
    /// ### Endpoint:
    /// /user/emails
    /// ### Description
    /// Returns a boolean value. True if the email(s) were deleted and false otherwise. The email
    /// strings must match verbatim for the corresponding email to be deleted.
    fn delete_user_emails(&self, emails: Vec<String>) -> Result<bool> {
        let url = "https://api.github.com/user/emails";
        let res = delete_with_data(url,
                                   self.get_headers().clone(),
                                   serde_json::to_string(&emails)? )?;
        Ok(res == StatusCode::NoContent)
    }
}