diff options
author | Slack Coder <slackcoder@server.ky> | 2024-08-05 04:48:55 -0500 |
---|---|---|
committer | Slack Coder <slackcoder@server.ky> | 2024-08-05 04:48:55 -0500 |
commit | d7b3c49d5cf467b90ce6f3399e77caa630f01a49 (patch) | |
tree | 4b825dc642cb6eb9a060e54bf8d69288fbee4904 /internal/github/rest_client.go | |
parent | 8161ec7d53a4c921c61b3e6e936daac63ca06d9e (diff) | |
download | mirror-d7b3c49d5cf467b90ce6f3399e77caa630f01a49.tar.xz |
Set To Do branch
Diffstat (limited to 'internal/github/rest_client.go')
-rw-r--r-- | internal/github/rest_client.go | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/internal/github/rest_client.go b/internal/github/rest_client.go deleted file mode 100644 index 6fdd31c..0000000 --- a/internal/github/rest_client.go +++ /dev/null @@ -1,115 +0,0 @@ -package github - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" -) - -type HTTPRequester interface { - Do(req *http.Request) (*http.Response, error) -} - -type BearerAuthClient struct { - HTTPRequester - Username string - Password string -} - -func WithBearerAuth( - cli HTTPRequester, - username, password string, -) *BearerAuthClient { - return &BearerAuthClient{cli, username, password} -} - -func (s *BearerAuthClient) Do(req *http.Request) (*http.Response, error) { - if s.Username != "" && s.Password != "" { - value := fmt.Sprintf("Bearer %s:%s", s.Username, s.Password) - req.Header.Set("Authorization", value) - } - return s.HTTPRequester.Do(req) -} - -type jsonClient struct { - Client HTTPRequester - basePath string -} - -func newJSONClient( - cli HTTPRequester, - basePath string, -) *jsonClient { - return &jsonClient{ - Client: cli, - basePath: basePath, - } -} - -func newHTTPJSONReq( - method string, - url string, - req interface{}, -) (*http.Request, error) { - body := &bytes.Buffer{} - if req != nil { - buf, err := json.Marshal(req) - if err != nil { - return nil, err - } - body = bytes.NewBuffer(buf) - } - httpReq, err := http.NewRequest(method, url, body) - if err != nil { - return nil, err - } - httpReq.Header.Set("Content-Type", "application/json") - httpReq.Header.Set("Accept", "application/json") - return httpReq, nil -} - -func decodeJSONResponse(httpResp *http.Response, resp interface{}) error { - if httpResp.StatusCode/100 != 2 { - return fmt.Errorf( - "received HTTP status code %d (%s)", - httpResp.StatusCode, - httpResp.Status, - ) - } - if resp == nil { - return nil - } - err := json.NewDecoder(httpResp.Body).Decode(resp) - if err != nil { - return err - } - return err -} - -func (s *jsonClient) Request( - method string, - statusCode *int, - path string, - req, resp interface{}, -) (int, error) { - url := fmt.Sprintf("%s/%s", s.basePath, path) - httpReq, err := newHTTPJSONReq(method, url, req) - if err != nil { - return 0, err - } - httpResp, err := s.Client.Do(httpReq) - if err != nil { - return 0, err - } - defer httpResp.Body.Close() - - err = decodeJSONResponse(httpResp, resp) - if err != nil { - return httpResp.StatusCode, err - } - if statusCode != nil && httpResp.StatusCode != *statusCode { - return httpResp.StatusCode, fmt.Errorf("expected status code %d but got %d", *statusCode, httpResp.StatusCode) - } - return httpResp.StatusCode, nil -} |