client_monitor.go raw
1 package linodego
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/url"
8 "os"
9 "path"
10
11 "github.com/go-resty/resty/v2"
12 )
13
14 const (
15 // MonitorAPIHost is the default monitor-api host
16 MonitorAPIHost = "monitor-api.linode.com"
17 // MonitorAPIHostVar is the env var to check for the alternate Monitor API URL
18 MonitorAPIHostVar = "MONITOR_API_URL"
19 // MonitorAPIVersion is the default API version to use
20 MonitorAPIVersion = "v2beta"
21 // MonitorAPIVersionVar is the env var to check for the alternate Monitor API version
22 MonitorAPIVersionVar = "MONITOR_API_VERSION"
23 // MonitorAPIEnvVar is the env var to check for Monitor API token
24 MonitorAPIEnvVar = "MONITOR_API_TOKEN"
25 )
26
27 // MonitorClient is a wrapper around the Resty client
28 type MonitorClient struct {
29 resty *resty.Client
30 debug bool
31 apiBaseURL string
32 apiProtocol string
33 apiVersion string
34 userAgent string
35 }
36
37 // NewMonitorClient is the entry point for user to create a new MonitorClient
38 // It utilizes default values and looks for environment variables to initialize a MonitorClient.
39 func NewMonitorClient(hc *http.Client) (mClient MonitorClient) {
40 if hc != nil {
41 mClient.resty = resty.NewWithClient(hc)
42 } else {
43 mClient.resty = resty.New()
44 }
45
46 mClient.SetUserAgent(DefaultUserAgent)
47
48 baseURL, baseURLExists := os.LookupEnv(MonitorAPIHostVar)
49 if baseURLExists {
50 mClient.SetBaseURL(baseURL)
51 } else {
52 mClient.SetBaseURL(MonitorAPIHost)
53 }
54
55 apiVersion, apiVersionExists := os.LookupEnv(MonitorAPIVersionVar)
56 if apiVersionExists {
57 mClient.SetAPIVersion(apiVersion)
58 } else {
59 mClient.SetAPIVersion(MonitorAPIVersion)
60 }
61
62 token, apiTokenExists := os.LookupEnv(MonitorAPIEnvVar)
63 if apiTokenExists {
64 mClient.SetToken(token)
65 }
66
67 mClient.SetDebug(envDebug)
68
69 return mClient
70 }
71
72 // SetUserAgent sets a custom user-agent for HTTP requests
73 func (mc *MonitorClient) SetUserAgent(ua string) *MonitorClient {
74 mc.userAgent = ua
75 mc.resty.SetHeader("User-Agent", mc.userAgent)
76
77 return mc
78 }
79
80 // R wraps resty's R method
81 func (mc *MonitorClient) R(ctx context.Context) *resty.Request {
82 return mc.resty.R().
83 ExpectContentType("application/json").
84 SetHeader("Content-Type", "application/json").
85 SetContext(ctx).
86 SetError(APIError{})
87 }
88
89 // SetDebug sets the debug on resty's client
90 func (mc *MonitorClient) SetDebug(debug bool) *MonitorClient {
91 mc.debug = debug
92 mc.resty.SetDebug(debug)
93
94 return mc
95 }
96
97 // SetLogger allows the user to override the output
98 // logger for debug logs.
99 func (mc *MonitorClient) SetLogger(logger Logger) *MonitorClient {
100 mc.resty.SetLogger(logger)
101
102 return mc
103 }
104
105 // SetBaseURL is the helper function to set base url
106 func (mc *MonitorClient) SetBaseURL(baseURL string) *MonitorClient {
107 baseURLPath, _ := url.Parse(baseURL)
108
109 mc.apiBaseURL = path.Join(baseURLPath.Host, baseURLPath.Path)
110 mc.apiProtocol = baseURLPath.Scheme
111
112 mc.updateMonitorHostURL()
113
114 return mc
115 }
116
117 // SetAPIVersion is the helper function to set api version
118 func (mc *MonitorClient) SetAPIVersion(apiVersion string) *MonitorClient {
119 mc.apiVersion = apiVersion
120
121 mc.updateMonitorHostURL()
122
123 return mc
124 }
125
126 // SetRootCertificate adds a root certificate to the underlying TLS client config
127 func (mc *MonitorClient) SetRootCertificate(path string) *MonitorClient {
128 mc.resty.SetRootCertificate(path)
129 return mc
130 }
131
132 // SetToken sets the API token for all requests from this client
133 func (mc *MonitorClient) SetToken(token string) *MonitorClient {
134 mc.resty.SetHeader("Authorization", fmt.Sprintf("Bearer %s", token))
135 return mc
136 }
137
138 // SetHeader sets a custom header to be used in all API requests made with the current client.
139 // NOTE: Some headers may be overridden by the individual request functions.
140 func (mc *MonitorClient) SetHeader(name, value string) {
141 mc.resty.SetHeader(name, value)
142 }
143
144 func (mc *MonitorClient) updateMonitorHostURL() {
145 apiProto := APIProto
146 baseURL := MonitorAPIHost
147 apiVersion := MonitorAPIVersion
148
149 if mc.apiBaseURL != "" {
150 baseURL = mc.apiBaseURL
151 }
152
153 if mc.apiVersion != "" {
154 apiVersion = mc.apiVersion
155 }
156
157 if mc.apiProtocol != "" {
158 apiProto = mc.apiProtocol
159 }
160
161 mc.resty.SetBaseURL(
162 fmt.Sprintf(
163 "%s://%s/%s",
164 apiProto,
165 baseURL,
166 url.PathEscape(apiVersion),
167 ),
168 )
169 }
170