From 3859b37267ed1d66fccb7591104a1023556a8917 Mon Sep 17 00:00:00 2001 From: unilock Date: Thu, 14 Sep 2023 19:54:37 -0400 Subject: [PATCH] github: Notify user when encountering GitHub API ratelimit Signed-off-by: unilock --- github/request.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/github/request.go b/github/request.go index 5b1a429..6b6b10f 100644 --- a/github/request.go +++ b/github/request.go @@ -3,6 +3,7 @@ package github import ( "fmt" "net/http" + "strconv" "github.com/packwiz/packwiz/core" "github.com/spf13/viper" @@ -35,10 +36,24 @@ func (c *ghApiClient) makeGet(url string) (*http.Response, error) { if err != nil { return nil, err } + + ratelimit, err := strconv.Atoi(resp.Header.Get("x-ratelimit-remaining")) + if err != nil { + return nil, err + } + + if resp.StatusCode == 403 && ratelimit == 0 { + return nil, fmt.Errorf("GitHub API ratelimit exceeded; time of reset: %v", resp.Header.Get("x-ratelimit-reset")) + } if resp.StatusCode != 200 { return nil, fmt.Errorf("invalid response status: %v", resp.Status) } + if ratelimit < 10 { + fmt.Printf("Warning: GitHub API allows %v more requests before ratelimiting\n", ratelimit) + fmt.Println("Specifying a token is recommended; see documentation") + } + return resp, nil }