1
|
|
|
// Copyright 2018 Fedir RYKHTIK. All rights reserved. |
2
|
|
|
// Use of this source code is governed by the GNU GPL 3.0 |
3
|
|
|
// license that can be found in the LICENSE file. |
4
|
|
|
|
5
|
|
|
package github |
6
|
|
|
|
7
|
|
|
import ( |
8
|
|
|
"encoding/json" |
9
|
|
|
"fmt" |
10
|
|
|
"log" |
11
|
|
|
|
12
|
|
|
"github.com/fedir/ghstat/httpcache" |
13
|
|
|
"github.com/fedir/ghstat/timing" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
// CheckAndPrintRateLimit accesses Github's API to check current limits |
17
|
|
|
func CheckAndPrintRateLimit() { |
18
|
|
|
type RateLimits struct { |
19
|
|
|
Resources struct { |
20
|
|
|
Core struct { |
21
|
|
|
Limit int `json:"limit"` |
22
|
|
|
Remaining int `json:"remaining"` |
23
|
|
|
Reset int `json:"reset"` |
24
|
|
|
} `json:"core"` |
25
|
|
|
Search struct { |
26
|
|
|
Limit int `json:"limit"` |
27
|
|
|
Remaining int `json:"remaining"` |
28
|
|
|
Reset int `json:"reset"` |
29
|
|
|
} `json:"search"` |
30
|
|
|
GraphQL struct { |
31
|
|
|
Limit int `json:"limit"` |
32
|
|
|
Remaining int `json:"remaining"` |
33
|
|
|
Reset int `json:"reset"` |
34
|
|
|
} `json:"graphql"` |
35
|
|
|
} `json:"resources"` |
36
|
|
|
Rate struct { |
37
|
|
|
Limit int `json:"limit"` |
38
|
|
|
Remaining int `json:"remaining"` |
39
|
|
|
Reset int `json:"reset"` |
40
|
|
|
} `json:"rate"` |
41
|
|
|
} |
42
|
|
|
url := "https://api.github.com/rate_limit" |
43
|
|
|
resp, statusCode, err := httpcache.MakeHTTPRequest(url) |
44
|
|
|
if err != nil { |
45
|
|
|
log.Fatalf("Error during checking rate limit : %d %v#", statusCode, err) |
46
|
|
|
} |
47
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(resp) |
48
|
|
|
rateLimits := RateLimits{} |
49
|
|
|
json.Unmarshal(jsonResponse, &rateLimits) |
50
|
|
|
fmt.Printf("Core: %d/%d (reset in %d minutes)\n", rateLimits.Resources.Core.Remaining, rateLimits.Resources.Core.Limit, timing.GetRelativeTime(rateLimits.Resources.Core.Reset)) |
51
|
|
|
fmt.Printf("Search: %d/%d (reset in %d minutes)\n", rateLimits.Resources.Search.Remaining, rateLimits.Resources.Search.Limit, timing.GetRelativeTime(rateLimits.Resources.Search.Reset)) |
52
|
|
|
fmt.Printf("GraphQL: %d/%d (reset in %d minutes)\n", rateLimits.Resources.GraphQL.Remaining, rateLimits.Resources.GraphQL.Limit, timing.GetRelativeTime(rateLimits.Resources.GraphQL.Reset)) |
53
|
|
|
fmt.Printf("Rate: %d/%d (reset in %d minutes)\n", rateLimits.Rate.Remaining, rateLimits.Rate.Limit, timing.GetRelativeTime(rateLimits.Rate.Reset)) |
54
|
|
|
} |
55
|
|
|
|