aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/github/rest_client.go
blob: 6fdd31cca032425fee54a6bab900769614b70a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
}