Skip to content

Commit c31e44e

Browse files
authored
Update helper.lua - Make authenticated GitHub API request
1 parent dc6cab5 commit c31e44e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lib/helper.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,45 @@ end
2525
function M.get_platform()
2626
return M.get_os() .. "-" .. M.get_arch()
2727
end
28+
29+
-- Make authenticated GitHub API request
30+
function M.github_request(url)
31+
local http = require("http")
32+
local headers = {}
33+
34+
-- Try to get GitHub token from environment
35+
local token = os.getenv("GITHUB_TOKEN") or os.getenv("GITHUB_API_TOKEN") or os.getenv("GH_TOKEN")
36+
if token then
37+
headers["Authorization"] = "Bearer " .. token
38+
end
39+
40+
-- Set User-Agent (required by GitHub API)
41+
headers["User-Agent"] = "mise-vfox-eca-plugin"
42+
43+
local resp, err = http.get({
44+
url = url,
45+
headers = headers
46+
})
47+
48+
if err then
49+
return nil, "HTTP request failed: " .. err
50+
end
51+
52+
if resp.status_code == 403 then
53+
local err_msg = "GitHub API rate limit exceeded. "
54+
if not token then
55+
err_msg = err_msg .. "Please set GITHUB_TOKEN environment variable to authenticate and increase rate limit."
56+
else
57+
err_msg = err_msg .. "Authenticated request failed with 403."
58+
end
59+
return nil, err_msg
60+
end
61+
62+
if resp.status_code ~= 200 then
63+
return nil, "GitHub API returned status code: " .. resp.status_code
64+
end
65+
66+
return resp, nil
67+
end
68+
2869
return M

0 commit comments

Comments
 (0)