Skip to content

Commit e3cabcb

Browse files
committed
Introduce nilaway to CI checks
1 parent f59ee69 commit e3cabcb

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test:
2020
errcheck ./...
2121
gocritic check -disable='#experimental,#opinionated' [email protected] 3 ./...
2222
revive -set_exit_status ./...
23+
nilaway ./...
2324
go test -v -cover ./...
2425
gosec -exclude-dir=tests ./...
2526
govulncheck ./...
@@ -33,6 +34,7 @@ setup:
3334
go install github.com/kisielk/errcheck@latest
3435
go install github.com/mgechev/revive@latest
3536
go install github.com/securego/gosec/v2/cmd/gosec@latest
37+
go install go.uber.org/nilaway/cmd/nilaway@latest
3638
go install golang.org/x/vuln/cmd/govulncheck@latest
3739
go install honnef.co/go/tools/cmd/staticcheck@latest
3840
go install mvdan.cc/gofumpt@latest

internal/api/mod.go

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,29 @@ func buildURL(baseStr string, endpoint string) *url.URL {
2727
return base.ResolveReference(u)
2828
}
2929

30-
// Request sends a HTTP request to the server.
31-
func Request(base, endpoint, method, proxy, username, password string, hasBody bool, data interface{}) (interface{}, error) {
32-
transport := http.DefaultTransport
33-
34-
if len(proxy) > 0 {
35-
log.Printf("Using proxy '%s'", proxy)
36-
37-
proxyURL, err := url.Parse(proxy)
38-
if err != nil {
39-
return nil, err
40-
}
41-
42-
transport = &http.Transport{
43-
Proxy: http.ProxyURL(proxyURL),
44-
}
30+
func createTransport(proxy string) (http.RoundTripper, error) {
31+
if len(proxy) == 0 {
32+
return http.DefaultTransport, nil
4533
}
4634

47-
client := &http.Client{
48-
Transport: transport,
35+
log.Printf("Using proxy '%s'", proxy)
36+
proxyURL, err := url.Parse(proxy)
37+
if err != nil {
38+
return nil, err
4939
}
5040

51-
url := buildURL(base, endpoint)
41+
return &http.Transport{
42+
Proxy: http.ProxyURL(proxyURL),
43+
}, nil
44+
}
5245

46+
func createRequest(url *url.URL, method, username, password string, hasBody bool, data interface{}) (*http.Request, error) {
5347
var reqBodyReader io.Reader
54-
reqBodyReader = nil
55-
5648
if hasBody {
5749
reqBody, err := json.Marshal(data)
5850
if err != nil {
5951
return nil, err
6052
}
61-
6253
reqBodyReader = strings.NewReader(string(reqBody))
6354
}
6455

@@ -69,12 +60,14 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
6960

7061
req.Header.Set("Accept", "application/json")
7162
req.Header.Set("Content-Type", "application/json")
72-
7363
req.SetBasicAuth(username, password)
7464

75-
resp, err := client.Do(req)
76-
if err != nil {
77-
log.Fatal(err)
65+
return req, nil
66+
}
67+
68+
func handleResponse(resp *http.Response) (interface{}, error) {
69+
if resp == nil {
70+
return nil, fmt.Errorf("received nil response")
7871
}
7972
defer handling.Close(resp.Body)
8073

@@ -88,7 +81,6 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
8881
}
8982

9083
var obj interface{}
91-
9284
err = json.Unmarshal(bodyText, &obj)
9385
if err != nil {
9486
return nil, err
@@ -97,6 +89,29 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
9789
return obj, nil
9890
}
9991

92+
// Request sends a HTTP request to the server.
93+
func Request(base, endpoint, method, proxy, username, password string, hasBody bool, data interface{}) (interface{}, error) {
94+
transport, err := createTransport(proxy)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
client := &http.Client{Transport: transport}
100+
url := buildURL(base, endpoint)
101+
102+
req, err := createRequest(url, method, username, password, hasBody, data)
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
resp, err := client.Do(req)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
return handleResponse(resp)
113+
}
114+
100115
// Delete sends a HTTP DELETE request to the server.
101116
func Delete(base, endpoint, proxy, username, password string) (interface{}, error) {
102117
return Request(base, endpoint, "DELETE", proxy, username, password, false, nil)

0 commit comments

Comments
 (0)