generated from jfrog/jfrog-cli-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
url_metrics_fetcher.go
127 lines (110 loc) · 3.22 KB
/
url_metrics_fetcher.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package provider
import (
"encoding/base64"
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
"io/ioutil"
"net/http"
"strings"
)
type UrlMetricsFetcher interface {
Get() ([]byte, error)
}
func NewArtifactoryMetricsFetcher(rtDetails *config.ServerDetails) (*artifactoryMetricsFetcher, error) {
const useDefaultRetries = -1
sm, err := utils.CreateServiceManager(rtDetails, useDefaultRetries, false)
if err != nil {
return nil, err
}
authConfig, err := rtDetails.CreateArtAuthConfig()
if err != nil {
return nil, err
}
clientDetails := authConfig.CreateHttpClientDetails()
return &artifactoryMetricsFetcher{
url: fmt.Sprintf("%s/api/v1/metrics", strings.TrimSuffix(rtDetails.ArtifactoryUrl, "/")),
client: sm.Client(),
clientDetails: &clientDetails,
}, nil
}
type artifactoryMetricsFetcher struct {
url string
client *jfroghttpclient.JfrogHttpClient
clientDetails *httputils.HttpClientDetails
}
func (f *artifactoryMetricsFetcher) Get() ([]byte, error) {
res, body, _, err := f.client.SendGet(f.url, true, f.clientDetails)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status: %s", res.Status)
}
if len(body) == 0 {
return nil, fmt.Errorf("response body is empty")
}
return body, nil
}
func (f artifactoryMetricsFetcher) String() string {
return fmt.Sprintf("url: %s, user: %s", f.url, f.clientDetails.User)
}
func NewUrlMetricsFetcher(url string, authenticator Authenticator) *urlMetricsFetcher {
return &urlMetricsFetcher{
url: url,
authenticator: authenticator,
}
}
type urlMetricsFetcher struct {
url string
authenticator Authenticator
}
func (f urlMetricsFetcher) String() string {
if f.authenticator == nil {
return fmt.Sprintf("url: %s", f.url)
}
return fmt.Sprintf("url: %s, auth-by-%s", f.url, f.authenticator)
}
func (f urlMetricsFetcher) Get() ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, f.url, nil)
if err != nil {
return nil, err
}
if f.authenticator != nil {
f.authenticator.Authorize(req)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status: %s", res.Status)
}
return ioutil.ReadAll(res.Body)
}
type Authenticator interface {
Authorize(req *http.Request)
}
type UserPassAuthenticator struct {
Username string
Password string
}
func (a UserPassAuthenticator) Authorize(req *http.Request) {
credsEncoded := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.Username, a.Password)))
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", credsEncoded))
}
func (a UserPassAuthenticator) String() string {
return fmt.Sprintf("user: %s", a.Username)
}
type AccessTokenAuthenticator struct {
Token string
}
func (a AccessTokenAuthenticator) Authorize(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
}
func (a AccessTokenAuthenticator) String() string {
return fmt.Sprintf("token: *****")
}