-
Notifications
You must be signed in to change notification settings - Fork 16
/
profitbricks.go
150 lines (128 loc) · 4.01 KB
/
profitbricks.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package profitbricks
import (
"context"
"github.com/ionos-cloud/sdk-go/v5"
"strconv"
"time"
)
type Client struct {
// *resty.Client
CoreSdk *ionoscloud.APIClient
// AuthApiUrl will be used by methods talking to the auth api by sending absolute urls
AuthApiUrl string
CloudApiUrl string
Config Configuration
}
type Configuration struct {
Timeout time.Duration
}
const (
DefaultApiUrl = "https://api.ionos.com/cloudapi/v5"
DefaultAuthUrl = "https://api.ionos.com/auth/v1"
Version = "5.1.0"
)
func RestyClient(username, password, token string) *Client {
c := &Client{
// Client: resty.New(),
CoreSdk: ionoscloud.NewAPIClient(ionoscloud.NewConfiguration(username, password, token)),
AuthApiUrl: DefaultAuthUrl,
CloudApiUrl: DefaultApiUrl,
}
/*
if token == "" {
c.SetBasicAuth(username, password)
} else {
c.SetAuthToken(token)
}*/
// c.SetHostURL(DefaultApiUrl)
c.SetDepth(10)
c.SetTimeout(3 * time.Minute)
c.SetUserAgent("ionos-enterprise-sdk-go-compat " + Version)
c.SetRetryCount(3)
c.SetRetryMaxWaitTime(10 * time.Minute)
c.SetRetryWaitTime(1 * time.Second)
/*
c.SetRetryAfter(func(cl *resty.Client, r *resty.Response) (time.Duration, error) {
switch r.StatusCode() {
case http.StatusTooManyRequests:
if retryAfterSeconds := r.Header().Get("Retry-After"); retryAfterSeconds != "" {
return time.ParseDuration(retryAfterSeconds + "s")
}
}
return cl.RetryWaitTime, nil
})
c.AddRetryCondition(
func(r *resty.Response, err error) bool {
switch r.StatusCode() {
case http.StatusTooManyRequests,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout,
http.StatusBadGateway:
return true
}
return false
})
*/
return c
}
func (c *Client) SetTimeout(t time.Duration) {
c.Config.Timeout = t
}
// SetDebug activates/deactivates resty's debug mode. For better readability
// the pretty print feature is also enabled.
func (c *Client) SetDebug(debug bool) {
// c.Client.SetDebug(debug)
c.SetPretty(debug)
}
// SetDepth sets the depth of information that will be retrieved by api calls. The
// API accepts values from 0 to 10, a low depth means mostly only IDs and hrefs will be
// returned. Therefore nested structures may be nil.
func (c *Client) SetDepth(depth int) {
c.CoreSdk.GetConfig().AddDefaultQueryParam("depth", strconv.Itoa(depth))
}
// SetPretty toggles if the data retrieved from the api will be delivered pretty printed.
// Usually this does not make sense from an sdk perspective, but for debugging it's nice
// therefore it is also set to true, if debug is enabled.
func (c *Client) SetPretty(pretty bool) {
c.CoreSdk.GetConfig().AddDefaultQueryParam("pretty", strconv.FormatBool(pretty))
}
// NewClient is a constructor for Client object
func NewClient(username, password string) *Client {
return RestyClient(username, password, "")
}
// NewClientbyToken is a constructor for Client object using bearer tokens for
// authentication instead of username, password
func NewClientbyToken(token string) *Client {
return RestyClient("", "", token)
}
// SetUserAgent sets User-Agent request header for all API calls
func (c *Client) SetUserAgent(agent string) {
c.CoreSdk.GetConfig().UserAgent = agent
}
// GetUserAgent gets User-Agent header
func (c *Client) GetUserAgent() string {
return c.CoreSdk.GetConfig().UserAgent
}
// SetCloudApiURL sets Cloud API url
func (c *Client) SetCloudApiURL(url string) {
c.CoreSdk.GetConfig().Host = url
}
// SetAuthApiUrl sets the Auth API url
func (c *Client) SetAuthApiUrl(url string) {
c.AuthApiUrl = url
}
func (c *Client) GetContext() (context.Context, context.CancelFunc) {
if c.Config.Timeout > 0 {
return context.WithTimeout(context.Background(), c.Config.Timeout)
}
return context.Background(), nil
}
func (c *Client) SetRetryCount(count int) {
c.CoreSdk.GetConfig().MaxRetries = count
}
func (c *Client) SetRetryMaxWaitTime(t time.Duration) {
c.CoreSdk.GetConfig().MaxWaitTime = t
}
func (c *Client) SetRetryWaitTime(t time.Duration) {
c.CoreSdk.GetConfig().WaitTime = t
}