mirror of
https://github.com/packwiz/packwiz.git
synced 2025-11-19 01:24:32 +01:00
github: standardize language + fix api requests
language: - funcs, vars, log messages, etc. api requests: - make all requests use the makeGet() function - include packwiz user agent Signed-off-by: unilock <unilock@fennet.rentals>
This commit is contained in:
@@ -3,6 +3,8 @@ package github
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/packwiz/packwiz/core"
|
||||
)
|
||||
|
||||
const ghApiServer = "api.github.com"
|
||||
@@ -13,21 +15,40 @@ type ghApiClient struct {
|
||||
|
||||
var ghDefaultClient = ghApiClient{&http.Client{}}
|
||||
|
||||
func (c *ghApiClient) makeGet(slug string) (*http.Response, error) {
|
||||
req, err := http.NewRequest("GET", "https://" + ghApiServer + "/repos/" + slug + "/releases", nil)
|
||||
func (c *ghApiClient) makeGet(url string) (*http.Response, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", core.UserAgent)
|
||||
req.Header.Set("Accept", "application/vnd.github+json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("invalid response status: %v", resp.Status)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *ghApiClient) getRepo(slug string) (*http.Response, error) {
|
||||
resp, err := c.makeGet("https://" + ghApiServer + "/repos/" + slug)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *ghApiClient) getReleases(slug string) (*http.Response, error) {
|
||||
resp, err := c.getRepo(slug + "/releases")
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user