-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
172 lines (145 loc) · 4.38 KB
/
client.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// package client groups together all the services that the STACKIT client supports
// Active services are found under `ProductiveServices` whereas new services or services
// that still need to be further developed or tested, can be put under `Incubator`
// All services must be initialized in the `init` method, and the client must be configured
// during initialization
package client
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"time"
"github.com/SchwarzIT/community-stackit-go-client/internal/common"
"github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
"golang.org/x/oauth2"
)
// Client service for managing interactions with STACKIT API
type Client struct {
ctx context.Context
client *http.Client
config Config
Services services
// Legacy
//----------
// Productive services - services that are ready to be used in production
ProductiveServices
// Incubator - services under development or currently being tested
// not ready for production usage
Incubator IncubatorServices
}
// New returns a new client
func New(ctx context.Context, cfg Config) (*Client, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
c := &Client{
config: cfg,
ctx: ctx,
}
c.setHttpClient(c.ctx)
c.initServices()
c.initLegacyServices()
return c, nil
}
// Clone creates a shallow clone of the client
func (c *Client) Clone() common.Client {
nc := *c
return &nc
}
// GetHTTPClient returns the HTTP client
func (c *Client) GetHTTPClient() *http.Client {
return c.client
}
// GetBaseURL returns the base url string
func (c *Client) GetBaseURL() string {
return c.config.BaseUrl.String()
}
// SetBaseURL sets the base url
func (c *Client) SetBaseURL(url string) error {
return c.config.SetURL(url)
}
// GetConfig returns the client config
func (c *Client) GetConfig() Config {
return c.config
}
func (c *Client) SetToken(token string) {
c.config.ServiceAccountToken = token
}
// setHttpClient creates the client's oauth client
func (c *Client) setHttpClient(ctx context.Context) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.config.ServiceAccountToken},
)
hcl := oauth2.NewClient(ctx, ts)
hcl.Timeout = time.Second * 10
c.client = hcl
}
// Request creates a new API request
func (c *Client) Request(ctx context.Context, method, path string, body []byte) (*http.Request, error) {
if ctx != nil && ctx.Err() != nil {
return nil, ctx.Err()
}
pathSplit := strings.Split(path, "?")
rel := &url.URL{Path: pathSplit[0]}
if len(pathSplit) == 2 {
rel.RawQuery = pathSplit[1]
}
u := c.config.BaseUrl.ResolveReference(rel)
payload := strings.NewReader(string(body))
req, err := http.NewRequestWithContext(ctx, method, u.String(), payload)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
return req, nil
}
// LegacyDo performs the request, including retry if set
// To set retry, use WithRetry() which returns a shalow copy of the client
func (c *Client) LegacyDo(req *http.Request, v interface{}, errorHandlers ...func(*http.Response) error) (*http.Response, error) {
return c.do(req, v, errorHandlers...)
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.LegacyDo(req, nil)
}
// Do performs the request and decodes the response if given interface != nil
func (c *Client) do(req *http.Request, v interface{}, errorHandlers ...func(*http.Response) error) (*http.Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
// handle errors in the response
if len(errorHandlers) == 0 {
errorHandlers = append(errorHandlers, validate.DefaultResponseErrorHandler)
}
for _, fn := range errorHandlers {
if err := fn(resp); err != nil {
return resp, err
}
}
// parse response JSON
if v != nil {
err = json.NewDecoder(resp.Body).Decode(v)
defer resp.Body.Close()
}
return resp, err
}
// MockServer mocks STACKIT api server
// and returns a client pointing to it, mux, teardown function and an error indicator
func MockServer() (c *Client, mux *http.ServeMux, teardown func(), err error) {
mux = http.NewServeMux()
server := httptest.NewServer(mux)
teardown = server.Close
u, _ := url.Parse(server.URL)
c, err = New(context.Background(), Config{
BaseUrl: u,
ServiceAccountToken: "token",
ServiceAccountEmail: "sa-id",
})
return
}