feat: use github token if present in env

This commit is contained in:
2026-02-10 22:26:41 +00:00
parent 315c0ad8e4
commit 3cf6280c43
2 changed files with 23 additions and 4 deletions
+22 -3
View File
@@ -5,9 +5,12 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
var rateLimitError = fmt.Errorf("github rate limit exceeded. You can export GH_TOKEN=token for higher limits")
// now the nice thing is that I don't NEED to define a model always,
// go can be "loose" if you want it to be
// THis is unlike the geodata package where I made a model and all...
@@ -22,7 +25,15 @@ func getPushPatches(user string) ([]string, error) {
// another neat way is that you don't NEED a Client
// it's good to have it for production for doing timeouts etc though
// this uses the global shared default client
resp, err := http.Get(url)
// update: this comment above was meant for an older http.Get(url) based commit
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
addAuthHeaderIfTokenExists(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
@@ -30,7 +41,7 @@ func getPushPatches(user string) ([]string, error) {
if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusTooManyRequests {
if resp.Header.Get("X-RateLimit-Remaining") == "0" {
return nil, fmt.Errorf("github rate limit exceeded")
return nil, rateLimitError
}
}
@@ -80,6 +91,7 @@ func getUniqueTzFromPatches(patchUrls []string) ([]string, error) {
// Refer: https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#get-a-commit
// this is how YOU do custom stuff
req.Header.Set("Accept", "application/vnd.github.patch")
addAuthHeaderIfTokenExists(req)
// can potentially run for INIFINITY as no timeout
resp, err := http.DefaultClient.Do(req)
@@ -90,7 +102,7 @@ func getUniqueTzFromPatches(patchUrls []string) ([]string, error) {
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusTooManyRequests {
if resp.Header.Get("X-RateLimit-Remaining") == "0" {
return nil, fmt.Errorf("github rate limit exceeded")
return nil, rateLimitError
}
}
@@ -141,6 +153,13 @@ func getUniqueTzFromPatches(patchUrls []string) ([]string, error) {
return tzs[:end], nil
}
func addAuthHeaderIfTokenExists(req *http.Request) {
token := os.Getenv("GH_TOKEN")
if token != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
}
}
func GetTzsForUser(username string) ([]string, error) {
patches, err := getPushPatches(username)
if err != nil {
+1 -1
View File
@@ -115,7 +115,7 @@ func (m Model) View() string {
}
if len(m.preds) == 0 {
return AppStyle.Render(SubtleStyle.Render("No valid tzs found. Are you sure it's correct?"))
return AppStyle.Render(SubtleStyle.Render("No valid tzs found. Are you sure tz is correct/user is active?"))
}
var header string