forked from softprops/hubcaps
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
853 lines (763 loc) · 24.3 KB
/
lib.rs
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! Hubcaps provides a set of building blocks for interacting with the Github API
//!
//! # Examples
//!
//! Typical use will require instantiation of a Github client. Which requires
//! a user agent string, a `hyper::Client`, and set of `hubcaps::Credentials`.
//!
//! The hyper client should be configured with tls.
//!
//! ```no_run
//! extern crate hubcaps;
//! extern crate hyper;
//! extern crate hyper_native_tls;
//!
//! use hubcaps::{Credentials, Github};
//! use hyper::Client;
//! use hyper::net::HttpsConnector;
//! use hyper_native_tls::NativeTlsClient;
//!
//! fn main() {
//! let github = Github::new(
//! String::from("user-agent-name"),
//! Client::with_connector(
//! HttpsConnector::new(
//! NativeTlsClient::new().unwrap()
//! )
//! ),
//! Credentials::Token(
//! String::from("personal-access-token")
//! )
//! );
//! }
//! ```
//!
//! Github enterprise users will want to create a client with the
//! [Github#host](struct.Github.html#method.host) method
//!
//! Access to various services are provided via methods on instances of the `Github` type.
//!
//! The convention for executing operations typically looks like
//! `github.repo(.., ..).service().operation(OperationOptions)` where operation may be `create`,
//! `delete`, etc.
//! Services and their types are packaged under their own module namespace.
//! A service interface will provide access to operations and operations may access options types
//! that define the various parameter options available for the operation. Most operation option
//! types expose `builder()` methods for a builder oriented style of constructing options.
//!
//! # Errors
//!
//! Operations typically result in a `hubcaps::Result` Type which is an alias for Rust's
//! built-in Result with the Err Type fixed to the
//! [hubcaps::Error](errors/enum.Error.html) enum type.
//!
#![allow(missing_docs)] // todo: make this a deny eventually
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate log;
#[macro_use]
extern crate hyper;
#[macro_use]
extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate serde_json;
extern crate url;
extern crate frank_jwt;
// all the modules!
use serde::de::DeserializeOwned;
pub mod branches;
pub mod git;
pub mod users;
pub mod comments;
pub mod review_comments;
pub mod pull_commits;
pub mod keys;
pub mod gists;
pub mod deployments;
#[allow(deprecated)]
pub mod errors;
pub mod hooks;
pub mod issues;
pub mod labels;
pub mod releases;
pub mod repositories;
pub mod review_requests;
pub mod statuses;
pub mod pulls;
pub mod search;
pub mod teams;
pub mod organizations;
pub mod app;
pub mod checks;
pub use errors::{Error, ErrorKind, Result};
use gists::{Gists, UserGists};
use search::Search;
use hyper::Client;
use hyper::client::RequestBuilder;
use hyper::method::Method;
use hyper::header::{qitem, Accept, Authorization, UserAgent};
use hyper::mime::Mime;
use hyper::status::StatusCode;
use repositories::{Repository, Repositories, UserRepositories, OrganizationRepositories};
use organizations::{Organization, Organizations, UserOrganizations};
use users::Users;
use app::App;
use std::fmt;
use url::Url;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time;
use std::sync::Mutex;
use std::sync::Arc;
use frank_jwt::{Algorithm, encode};
/// Link header type
header! { (Link, "Link") => [String] }
const DEFAULT_HOST: &'static str = "https://api.github.com";
// 9 minutes so we always come below github's 10minmax even if their or
// our clocks are slightly off
const MAX_JWT_TOKEN_LIFE: time::Duration = time::Duration::from_secs(60 * 9);
// 8 minutes so we refresh sooner than it actually expires
const JWT_TOKEN_REFRESH_PERIOD: time::Duration = time::Duration::from_secs(60 * 8);
/// alias for Result that infers hubcaps::Error as Err
// pub type Result<T> = std::result::Result<T, Error>;
/// Github defined Media types
/// See [this doc](https://developer.github.com/v3/media/) for more for more information
#[derive(Clone, Copy)]
pub enum MediaType {
/// Return json (the default)
Json,
/// Return json in preview form
Preview(&'static str),
}
impl Default for MediaType {
fn default() -> MediaType {
MediaType::Json
}
}
impl From<MediaType> for Mime {
fn from(media: MediaType) -> Mime {
match media {
MediaType::Json => "application/vnd.github.v3+json".parse().unwrap(),
MediaType::Preview(codename) => {
format!("application/vnd.github.{}-preview+json", codename)
.parse()
.unwrap()
}
}
}
}
/// enum representation of Github list sorting options
#[derive(Clone, Debug, PartialEq)]
pub enum SortDirection {
/// Sort in ascending order (the default)
Asc,
/// Sort in descending order
Desc,
}
impl fmt::Display for SortDirection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SortDirection::Asc => "asc",
SortDirection::Desc => "desc",
}.fmt(f)
}
}
impl Default for SortDirection {
fn default() -> SortDirection {
SortDirection::Asc
}
}
/// Various forms of authentication credentials supported by Github
#[derive(Debug, PartialEq)]
pub enum Credentials {
/// No authentication (the default)
None,
/// Oauth token string
/// https://developer.github.com/v3/#oauth2-token-sent-in-a-header
Token(String),
/// Oauth client id and secret
/// https://developer.github.com/v3/#oauth2-keysecret
Client(String, String),
/// JWT token exchange, to be performed transparently in the
/// background. app-id, key-file.
/// https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/
JWT(JWTCredentials),
/// JWT-based App Installation Token
/// https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/
InstallationToken(InstallationTokenGenerator),
}
impl Default for Credentials {
fn default() -> Credentials {
Credentials::None
}
}
/// JSON Web Token authentication mechanism
///
/// The GitHub client methods are all &self, but the dynamically
/// generated JWT token changes regularly. The token is also a bit
/// expensive to regenerate, so we do want to have a mutable cache.
///
/// We use a token inside a Mutex so we can have interior mutability
/// even though JWTCredentials is not mutable.
#[derive(Debug, Clone)]
pub struct JWTCredentials {
pub app_id: i32,
pub private_key_file: PathBuf,
cache: Arc<Mutex<ExpiringJWTCredential>>
}
impl JWTCredentials {
pub fn new<A, P>(app_id: A, private_key_file: P) -> JWTCredentials
where
P: Into<PathBuf>,
A: Into<i32>
{
let app_id = app_id.into();
let private_key_file = private_key_file.into();
let creds = ExpiringJWTCredential::calculate(
&app_id,
&private_key_file
);
JWTCredentials {
app_id: app_id,
private_key_file: private_key_file.into(),
cache: Arc::new(Mutex::new(creds)),
}
}
fn is_stale(&self) -> bool {
self.cache.lock().unwrap().is_stale()
}
/// Fetch a valid JWT token, regenerating it if necessary
pub fn token(&self) -> String {
let mut expiring = self.cache.lock().unwrap();
if expiring.is_stale() {
* expiring = ExpiringJWTCredential::calculate(
&self.app_id,
&self.private_key_file,
);
}
expiring.token.clone()
}
}
impl PartialEq for JWTCredentials {
fn eq(&self, other: &JWTCredentials) -> bool {
self.app_id == other.app_id &&
self.private_key_file == other.private_key_file
}
}
#[derive(Debug)]
struct ExpiringJWTCredential {
token: String,
created_at: time::Instant
}
impl ExpiringJWTCredential {
fn calculate(app_id: &i32, private_key_file: &PathBuf) -> ExpiringJWTCredential {
// SystemTime can go backwards, Instant can't, so always use
// Instant for ensuring regular cycling.
let created_at = time::Instant::now();
let now = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap();
let expires = now + MAX_JWT_TOKEN_LIFE;
let payload = json!({
"iat" : now.as_secs(),
"exp" : expires.as_secs(),
"iss": app_id,
});
let header = json!({});
let jwt = encode(header, private_key_file, &payload,
Algorithm::RS256)
.expect(&format!("key {:?} may not exist", private_key_file));
ExpiringJWTCredential {
created_at: created_at,
token: jwt,
}
}
fn is_stale(&self) -> bool {
self.created_at.elapsed() >= JWT_TOKEN_REFRESH_PERIOD
}
}
/// A caching token "generator" which contains JWT credentials.
///
/// The authentication mechanism in the GitHub client library
/// determines if the token is stale, and if so, uses the contained
/// JWT credentials to fetch a new installation token.
///
/// The Mutex<Option> access key is for interior mutability.
#[derive(Debug)]
pub struct InstallationTokenGenerator {
pub installation_id: i32,
pub jwt_credential: JWTCredentials,
access_key: Mutex<Option<String>>
}
impl InstallationTokenGenerator {
pub fn new(installation_id: i32, creds: JWTCredentials) -> InstallationTokenGenerator {
InstallationTokenGenerator {
installation_id: installation_id,
jwt_credential: creds,
access_key: Mutex::new(None),
}
}
fn set_token(&self, token: app::AccessToken) {
*self.access_key.lock().unwrap() = Some(token.token);
}
/// Fetch the contained token
///
/// Panics if the token is stale: always check staleness before
/// fetching the token.
fn token(&self) -> String {
if self.is_stale() {
panic!("Token is stale!");
}
let keylock = self.access_key.lock().unwrap().clone();
keylock.unwrap().clone()
}
fn is_stale(&self) -> bool {
self.jwt_credential.is_stale()
|| self.access_key.lock().unwrap().is_none()
}
}
impl PartialEq for InstallationTokenGenerator {
fn eq(&self, other: &InstallationTokenGenerator) -> bool {
self.installation_id == other.installation_id &&
self.jwt_credential == other.jwt_credential
}
}
/// Entry point interface for interacting with Github API
pub struct Github {
host: String,
agent: String,
client: Client,
credentials: Mutex<Option<Credentials>>,
}
impl Github {
/// Create a new Github instance. This will typically be how you interface with all
/// other operations
pub fn new<A>(agent: A, client: Client, credentials: Credentials) -> Github
where
A: Into<String>,
{
Github::host(DEFAULT_HOST, agent, client, credentials)
}
/// Create a new Github instance hosted at a custom location.
/// Useful for github enterprise installations ( yourdomain.com/api/v3/ )
pub fn host<H, A>(host: H, agent: A, client: Client, credentials: Credentials) -> Github
where
H: Into<String>,
A: Into<String>,
{
Github {
host: host.into(),
agent: agent.into(),
client: client,
credentials: Mutex::new(Some(credentials)),
}
}
/// Return a reference to a Github repository
pub fn repo<O, R>(&self, owner: O, repo: R) -> Repository
where
O: Into<String>,
R: Into<String>,
{
Repository::new(self, owner, repo)
}
/// Return a reference to the collection of repositories owned by and
/// associated with an owner
pub fn user_repos<S>(&self, owner: S) -> UserRepositories
where
S: Into<String>,
{
UserRepositories::new(self, owner)
}
/// Return a reference to the collection of repositories owned by the user
/// associated with the current authentication credentials
pub fn repos(&self) -> Repositories {
Repositories::new(self)
}
pub fn org<O>(&self, org: O) -> Organization
where
O: Into<String>,
{
Organization::new(self, org)
}
/// Return a reference to the collection of organizations that the user
/// associated with the current authentication credentials is in
pub fn orgs(&self) -> Organizations {
Organizations::new(self)
}
/// Return a reference to an interface that provides access
/// to user information.
pub fn users(&self) -> Users {
Users::new(self)
}
/// Return a reference to the collection of organizations a user
/// is publicly associated with
pub fn user_orgs<U>(&self, user: U) -> UserOrganizations
where
U: Into<String>,
{
UserOrganizations::new(self, user)
}
/// Return a reference to an interface that provides access to a user's gists
pub fn user_gists<O>(&self, owner: O) -> UserGists
where
O: Into<String>,
{
UserGists::new(self, owner)
}
/// Return a reference to an interface that provides access to the
/// gists belonging to the owner of the token used to configure this client
pub fn gists(&self) -> Gists {
Gists::new(self)
}
/// Return a reference to an interface that provides access to search operations
pub fn search(&self) -> Search {
Search::new(self)
}
/// Return a reference to the collection of repositories owned by and
/// associated with an organization
pub fn org_repos<O>(&self, org: O) -> OrganizationRepositories
where
O: Into<String>,
{
OrganizationRepositories::new(self, org)
}
/// Return a reference to GitHub Apps
pub fn app(&self) -> App
{
App::new(self)
}
fn authenticate(&self, method: Method, url: String) -> RequestBuilder {
// We complete remove the credentials from the client, because
// if we are using the Installation token method we may need
// to replace the client's credentials and issue another
// request.
//
// At the end of this function, we will put the credentials
// back.
//
// I hope this is safe.
let mut creds_lock = self.credentials.lock().unwrap();
let creds_orig = creds_lock.take().unwrap();
drop(creds_lock);
let ret;
match creds_orig {
Credentials::Token(ref token) => {
ret = self.client.request(method, &url).header(Authorization(
format!("token {}", token),
));
}
Credentials::Client(ref id, ref secret) => {
let mut parsed = Url::parse(&url).unwrap();
parsed
.query_pairs_mut()
.append_pair("client_id", id)
.append_pair("client_secret", secret);
ret = self.client.request(method, parsed);
}
Credentials::JWT(ref jwt) => {
let header = format!("Bearer {}", jwt.token());
ret = self.client.request(method, &url).header(Authorization(
header
));
}
Credentials::InstallationToken(ref apptoken) => {
if apptoken.is_stale() {
// Plunk in the JWT credentials for refreshing our
// installation token.
debug!("App token is stale, refreshing");
let mut creds = self.credentials.lock().unwrap();
*creds = Some(Credentials::JWT(apptoken.jwt_credential.clone()));
drop(creds);
let token = self.app().make_access_token(apptoken.installation_id).unwrap();
apptoken.set_token(token);
}
ret = self.client.request(method, &url).header(Authorization(
format!("token {}", apptoken.token()),
));
}
Credentials::None => {
ret = self.client.request(method, &url);
}
}
let mut creds = self.credentials.lock().unwrap();
*creds = Some(creds_orig);
return ret;
}
fn iter<'a, D, I>(&'a self, uri: String, into_items: fn(D) -> Vec<I>) -> Result<Iter<'a, D, I>>
where
D: DeserializeOwned,
{
self.iter_media(uri, into_items, MediaType::Json)
}
fn iter_media<'a, D, I>(
&'a self,
uri: String,
into_items: fn(D) -> Vec<I>,
media_type: MediaType,
) -> Result<Iter<'a, D, I>>
where
D: DeserializeOwned,
{
Iter::new(self, self.host.clone() + &uri, into_items, media_type)
}
fn request<D>(
&self,
method: Method,
uri: String,
body: Option<&[u8]>,
media_type: MediaType,
) -> Result<(Option<Links>, D)>
where
D: DeserializeOwned,
{
let builder = self.authenticate(method, uri)
.header(UserAgent(self.agent.to_owned()))
.header(Accept(vec![qitem(From::from(media_type))]));
let res = (match body {
Some(ref bod) => builder.body(*bod).send(),
_ => builder.send(),
})?;
let links = res.headers.get::<Link>().map(|&Link(ref value)| {
Links::new(value.to_owned())
});
debug!("rec response {:?}", res);
match res.status {
StatusCode::Conflict |
StatusCode::BadRequest |
StatusCode::UnprocessableEntity |
StatusCode::Unauthorized |
StatusCode::NotFound |
StatusCode::Forbidden => {
Err(
ErrorKind::Fault {
code: res.status,
error: serde_json::from_reader(res)?,
}.into(),
)
}
_ => Ok((links, serde_json::from_reader(res)?)),
}
}
fn request_entity<D>(
&self,
method: Method,
uri: String,
body: Option<&[u8]>,
media_type: MediaType,
) -> Result<D>
where
D: DeserializeOwned,
{
self.request(method, uri, body, media_type).map(
|(_, entity)| {
entity
},
)
}
fn get<D>(&self, uri: &str) -> Result<D>
where
D: DeserializeOwned,
{
self.get_media(uri, MediaType::Json)
}
fn get_media<D>(&self, uri: &str, media: MediaType) -> Result<D>
where
D: DeserializeOwned,
{
self.request_entity(Method::Get, self.host.clone() + uri, None, media)
}
fn delete(&self, uri: &str) -> Result<()> {
match self.request_entity::<()>(
Method::Delete,
self.host.clone() + uri,
None,
MediaType::Json,
) {
Err(Error(ErrorKind::Codec(_), _)) => Ok(()),
otherwise => otherwise,
}
}
fn delete_message(&self, uri: &str, message: &[u8]) -> Result<()> {
match self.request_entity::<()>(
Method::Delete,
self.host.clone() + uri,
Some(message),
MediaType::Json,
) {
Err(Error(ErrorKind::Codec(_), _)) => Ok(()),
otherwise => otherwise,
}
}
fn post<D>(&self, uri: &str, message: &[u8]) -> Result<D>
where
D: DeserializeOwned,
{
self.post_media(
uri,
message,
MediaType::Json,
)
}
fn post_media<D>(&self, uri: &str, message: &[u8], media: MediaType) -> Result<D>
where
D: DeserializeOwned,
{
self.request_entity(
Method::Post,
self.host.clone() + uri,
Some(message),
media,
)
}
fn patch_media<D>(&self, uri: &str, message: &[u8], media: MediaType) -> Result<D>
where
D: DeserializeOwned,
{
self.request_entity(Method::Patch, self.host.clone() + uri, Some(message), media)
}
fn patch<D>(&self, uri: &str, message: &[u8]) -> Result<D>
where
D: DeserializeOwned,
{
self.patch_media(uri, message, MediaType::Json)
}
fn put_no_response(&self, uri: &str, message: &[u8]) -> Result<()> {
match self.put(uri, message) {
Err(Error(ErrorKind::Codec(_), _)) => Ok(()),
otherwise => otherwise,
}
}
fn put<D>(&self, uri: &str, message: &[u8]) -> Result<D>
where
D: DeserializeOwned,
{
self.request_entity(
Method::Put,
self.host.clone() + uri,
Some(message),
MediaType::Json,
)
}
}
/// An abstract type used for iterating over result sets
pub struct Iter<'a, D, I> {
github: &'a Github,
next_link: Option<String>,
into_items: fn(D) -> Vec<I>,
items: Vec<I>,
media_type: MediaType,
}
impl<'a, D, I> Iter<'a, D, I>
where
D: DeserializeOwned,
{
/// creates a new instance of an Iter
pub fn new(
github: &'a Github,
uri: String,
into_items: fn(D) -> Vec<I>,
media_type: MediaType,
) -> Result<Iter<'a, D, I>> {
let (links, payload) = github.request::<D>(Method::Get, uri, None, media_type)?;
let mut items = into_items(payload);
items.reverse(); // we pop from the tail
Ok(Iter {
github: github,
next_link: links.and_then(|l| l.next()),
into_items: into_items,
items: items,
media_type: media_type,
})
}
fn set_next(&mut self, next: Option<String>) {
self.next_link = next;
}
}
impl<'a, D, I> Iterator for Iter<'a, D, I>
where
D: DeserializeOwned,
{
type Item = I;
fn next(&mut self) -> Option<I> {
self.items.pop().or_else(|| {
self.next_link.clone().and_then(|ref next_link| {
self.github
.request::<D>(Method::Get, next_link.to_owned(), None, self.media_type)
.ok()
.and_then(|(links, payload)| {
let mut next_items = (self.into_items)(payload);
next_items.reverse(); // we pop() from the tail
self.set_next(links.and_then(|l| l.next()));
self.items = next_items;
self.next()
})
})
})
}
}
/// An abstract collection of Link header urls
/// Exposes interfaces to access link relations github typically
/// sends as headers
#[derive(Debug)]
pub struct Links {
values: HashMap<String, String>,
}
impl Links {
/// Creates a new Links instance given a raw header string value
pub fn new<V>(value: V) -> Links
where
V: Into<String>,
{
let values = value
.into()
.split(",")
.map(|link| {
let parts = link.split(";").collect::<Vec<_>>();
(
parts[1].to_owned().replace(" rel=\"", "").replace("\"", ""),
parts[0]
.to_owned()
.replace("<", "")
.replace(">", "")
.replace(" ", ""),
)
})
.fold(HashMap::new(), |mut acc, (rel, link)| {
acc.insert(rel, link);
acc
});
Links { values: values }
}
/// Returns next link url, when available
pub fn next(&self) -> Option<String> {
self.values.get("next").map(|s| s.to_owned())
}
/// Returns prev link url, when available
pub fn prev(&self) -> Option<String> {
self.values.get("prev").map(|s| s.to_owned())
}
/// Returns last link url, when available
pub fn last(&self) -> Option<String> {
self.values.get("last").map(|s| s.to_owned())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_links() {
let links = Links::new(r#"<linknext>; rel="next", <linklast>; rel="last""#);
assert_eq!(links.next(), Some("linknext".to_owned()));
assert_eq!(links.last(), Some("linklast".to_owned()));
}
#[test]
fn default_sort_direction() {
let default: SortDirection = Default::default();
assert_eq!(default, SortDirection::Asc)
}
#[test]
fn default_credentials() {
let default: Credentials = Default::default();
assert_eq!(default, Credentials::None)
}
}