-
Notifications
You must be signed in to change notification settings - Fork 1
/
nexus-request.go
99 lines (81 loc) · 2.37 KB
/
nexus-request.go
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
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/rs/zerolog/log"
)
func (p *Plugin) NexusRequest(ctx context.Context, requestUrl string) (*http.Response, error) {
return p.NexusRequestEx(ctx, http.MethodGet, requestUrl, nil, nil)
}
func (p *Plugin) NexusRequestEx(ctx context.Context, requestMethod string, requestUrl string, requestBody io.Reader, requestSetup func(*http.Request)) (*http.Response, error) {
if requestMethod == "" {
log.Panic().Msg("empty request method")
}
if requestUrl == "" {
log.Panic().Msg("empty request url")
}
c := p.HTTPClient()
if c == nil {
log.Panic().Msg("broken HTTP client")
}
req, err := http.NewRequestWithContext(ctx, requestMethod, p.RestUrl+requestUrl, requestBody)
if err != nil {
log.Error().Msgf("unable to create HTTP request: %q %q", requestMethod, "/"+requestUrl)
return nil, err
}
if req == nil {
log.Panic().Msg("nil request")
// make analysis tools happy
panic(1)
}
if requestSetup != nil {
requestSetup(req)
}
req.Header.Set(p.AuthHeader, p.AuthValue)
res, err := c.Do(req)
if err != nil {
log.Error().Msgf("unable to perform HTTP request: %q %q", requestMethod, "/"+requestUrl)
return nil, err
}
if res == nil {
log.Panic().Msg("nil response")
// make analysis tools happy
panic(1)
}
switch res.StatusCode {
case http.StatusUnauthorized:
defer res.Body.Close()
log.Error().Msgf("authentication is declined for HTTP %s %q", requestMethod, "/"+requestUrl)
return nil, errors.New("unauthorized")
case http.StatusForbidden:
defer res.Body.Close()
log.Error().Msgf("insufficient permissions for HTTP %s %q", requestMethod, "/"+requestUrl)
return nil, errors.New("forbidden")
}
return res, err
}
func GenericResponseHandler(response *http.Response) error {
if response == nil {
log.Panic().Msg("nil response")
// make analysis tools happy
panic(1)
}
if (response.StatusCode >= http.StatusOK) && (response.StatusCode < http.StatusMultipleChoices) {
return nil
}
if strings.Contains(response.Status, " ") {
return fmt.Errorf("HTTP %s", response.Status)
}
// "unlikely" branch
s := http.StatusText(response.StatusCode)
if s != "" {
return fmt.Errorf("HTTP %d %s", response.StatusCode, s)
}
return fmt.Errorf("HTTP %d Unknown return code", response.StatusCode)
}