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
//! Helper functions for end users for GitHub response Headers
use std::str;
use std::str::FromStr;
use hyper::header::{ ETag, Headers, LastModified, UserAgent };

/// Checks to see if a received payload from GitHub contains
/// the GitHub-Hookshot header in the `UserAgent`.
pub fn has_github_hookshot(head: &Headers) -> bool {
    head.get::<UserAgent>()
        .map_or(false, |user_agent| {
            user_agent.starts_with("GitHub-Hookshot")
        })
}

/// Extract an `ETag` from the Headers if it exists
pub fn etag(head: &Headers) -> Option<ETag> {
    head.get::<ETag>()
        .map(|tag| tag.to_owned())
}

/// Extract the Last-Modified from the Headers if it exists
pub fn last_modified(head: &Headers) -> Option<LastModified> {
    head.get::<LastModified>()
        .map(|modified| modified.to_owned())
}


/// Extract however many requests the authenticated user can
/// do from the Headers
pub fn rate_limit_remaining(head: &Headers) -> Option<u32> {
    head.get_raw("X-RateLimit-Remaining")
        .map(|limit| {
            u32::from_str(
                str::from_utf8(limit.one()
                                    .unwrap_or(&[]))
                .unwrap_or("")
            ).ok()
        })
        .unwrap_or(None)
}

/// Extract however many requests the authenticated user can
/// make from the Headers
pub fn rate_limit(head: &Headers) -> Option<u32> {
    head.get_raw("X-RateLimit-Limit")
        .map(|limit| {
            u32::from_str(
                str::from_utf8(limit.one()
                                    .unwrap_or(&[]))
                .unwrap_or("")
            ).ok()
        })
        .unwrap_or(None)
}

/// Extract when the requests limit for the authenticated user
/// is reset from the Headers
pub fn rate_limit_reset(head: &Headers) -> Option<u32> {
    head.get_raw("X-RateLimit-Reset")
        .map(|limit| {
            u32::from_str(
                str::from_utf8(limit.one()
                                    .unwrap_or(&[]))
                .unwrap_or("")
            ).ok()
        })
        .unwrap_or(None)
}