-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptions.go
More file actions
220 lines (193 loc) · 6.19 KB
/
options.go
File metadata and controls
220 lines (193 loc) · 6.19 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* StatusCake API
*
* Copyright (c) 2023
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* API version: 1.2.0
* Contact: support@statuscake.com
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package statuscake
import (
"fmt"
"net/http"
"runtime"
"strings"
"time"
"github.com/StatusCakeDev/statuscake-go/backoff"
)
var defaultOptions = options{
backoff: backoff.Constant{BaseDelay: 1 * time.Second},
client: http.DefaultClient,
headers: http.Header{},
maxRetries: 1,
userAgent: "statuscake/go/" + runtime.Version(),
servers: ServerConfigurations{
ServerConfiguration{
URL: "https://api.statuscake.com/v1",
Description: "No description provided",
},
},
operationServers: map[string]ServerConfigurations{},
}
// ServerVariable stores the information about a server variable.
type ServerVariable struct {
Description string
DefaultValue string
EnumValues []string
}
// ServerConfiguration stores the information about a server.
type ServerConfiguration struct {
URL string
Description string
Variables map[string]ServerVariable
}
// ServerConfigurations stores multiple ServerConfiguration items.
type ServerConfigurations []ServerConfiguration
// URL formats template on a index using given variables.
func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
if index < 0 || index > len(sc) {
return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
}
server := sc[index]
url := server.URL
// Go through variables and replace placeholders.
for name, variable := range server.Variables {
if value, ok := variables[name]; ok {
found := bool(len(variable.EnumValues) == 0)
for _, enumValue := range variable.EnumValues {
if value == enumValue {
found = true
}
}
if !found {
return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
}
url = strings.Replace(url, "{"+name+"}", value, -1)
} else {
url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
}
}
return url, nil
}
// Strategy describes a common interface to satisfy a backoff strategy when a
// request fails.
type Strategy interface {
Backoff(idx int) time.Duration
}
// RequestCredentials describes a common interface for attaching security
// credentials to every outbound request.
type RequestCredentials interface {
AddCredentials(r *http.Request) *http.Request
}
// options configures a HTTP client and connnection. Options are set by the
// Option values passed to NewClient.
type options struct {
backoff Strategy
client *http.Client
credentials RequestCredentials
debug bool
disableRetry bool
headers http.Header
host string
maxRetries int
operationServers map[string]ServerConfigurations
servers ServerConfigurations
userAgent string
}
// Option consigures a HTTP client and connection.
type Option func(*options)
// WithBackoff configures the backoff strategy after failed requests.
func WithBackoff(s Strategy) Option {
return func(o *options) {
o.backoff = s
}
}
// WithHTTPClient configures the HTTP client used to perform requests
func WithHTTPClient(c *http.Client) Option {
return func(o *options) {
o.client = c
}
}
// WithRequestCredentials configures the HTTP client to forward authentication
// credentials.
func WithRequestCredentials(creds RequestCredentials) Option {
return func(o *options) {
o.credentials = creds
}
}
// WithDebug configures the client to output debug information when making
// requests.
func WithDebug() Option {
return func(o *options) {
o.debug = true
}
}
// WithDisableRetry configures the client to disable request retries. Retry
// support is currently enabled by default and will make `maxRetries` attempts
// before failure.
func WithDisableRetry() Option {
return func(o *options) {
o.disableRetry = true
}
}
// WithHeaders configures the client to add supplied headers to each request.
// These headers will be appended to existing headers set by other client
// options and endpoint specific header parameters.
func WithHeaders(headers http.Header) Option {
return func(o *options) {
o.headers = headers
}
}
// WithHeader configures the client to add the supplied header to each request.
// This header will be appended to an existing header set by other client
// options and endpoint specific header parameters.
func WithHeader(name, value string) Option {
return func(o *options) {
o.headers.Add(name, value)
}
}
// WithHost configures the base host address for the API client.
func WithHost(host string) Option {
return func(o *options) {
o.host = host
}
}
// WithMaxRetries configures the maximum number of retries that can be made
// before the request is considered to have failed.
func WithMaxRetries(retries int) Option {
return func(o *options) {
o.maxRetries = retries
}
}
// WithUserAgent configures the HTTP client user agent.
func WithUserAgent(s string) Option {
return func(o *options) {
o.userAgent = s
}
}
func applyOptions(opts []Option) options {
cfg := defaultOptions
for _, opt := range opts {
opt(&cfg)
}
return cfg
}