github: Notify user when encountering GitHub API ratelimit

Signed-off-by: unilock <unilock@fennet.rentals>
This commit is contained in:
unilock 2023-09-14 19:54:37 -04:00
parent 3f5b953d00
commit 3859b37267

View File

@ -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
}