Skip to content

Commit 3c582d0

Browse files
authored
Merge pull request #29 from serverscom/add-verbose
Add verbose
2 parents 75da0de + dc3a9de commit 3c582d0

File tree

8 files changed

+78
-86
lines changed

8 files changed

+78
-86
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ jobs:
77
- uses: actions/checkout@v3
88
- uses: actions/setup-go@v4
99
with:
10-
go-version: '1.20'
10+
go-version: '1.22'
1111
- name: golangci-lint
1212
uses: golangci/golangci-lint-action@v3
1313
with:
14-
version: v1.53
14+
version: v1.63
1515

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.20.x, 1.21.x]
7+
go-version: [1.21.x, 1.22.x]
88
runs-on: ubuntu-latest
99
steps:
1010
- name: Install Go

go.mod

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
module github.com/serverscom/serverscom-go-client
22

3-
go 1.18
3+
go 1.22.0
44

5-
require github.com/onsi/gomega v1.5.0
5+
require (
6+
github.com/go-resty/resty/v2 v2.16.2
7+
github.com/onsi/gomega v1.36.2
8+
)
69

710
require (
8-
golang.org/x/net v0.23.0 // indirect
9-
golang.org/x/text v0.14.0 // indirect
10-
gopkg.in/yaml.v2 v2.2.8 // indirect
11+
github.com/google/go-cmp v0.6.0 // indirect
12+
golang.org/x/net v0.33.0 // indirect
13+
golang.org/x/text v0.21.0 // indirect
14+
gopkg.in/yaml.v3 v3.0.1 // indirect
1115
)

go.sum

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
2-
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3-
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
4-
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
5-
github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
6-
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
7-
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
8-
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
9-
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
10-
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
11-
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
12-
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13-
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
14-
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
15-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
16-
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
17-
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
1+
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
2+
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
3+
github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg=
4+
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
5+
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
6+
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
7+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
8+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
9+
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad h1:a6HEuzUHeKH6hwfN/ZoQgRgVIWFJljSWa/zetS2WTvg=
10+
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
11+
github.com/onsi/ginkgo/v2 v2.22.1 h1:QW7tbJAUDyVDVOM5dFa7qaybo+CRfR7bemlQUN6Z8aM=
12+
github.com/onsi/ginkgo/v2 v2.22.1/go.mod h1:S6aTpoRsSq2cZOd+pssHAlKW/Q/jZt6cPrPlnj4a1xM=
13+
github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8=
14+
github.com/onsi/gomega v1.36.2/go.mod h1:DdwyADRjrc825LhMEkD76cHR5+pUnjhUN8GlHlRPHzY=
15+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
16+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
17+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
18+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
19+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
20+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
21+
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
22+
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
23+
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
24+
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
1825
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1926
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
20-
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
21-
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
22-
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
23-
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
24-
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
25-
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
26-
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
27+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
28+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/collection.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func (col *CollectionHandler[K]) Refresh(ctx context.Context) error {
235235
func (col *CollectionHandler[K]) fireHTTPRequest(ctx context.Context) error {
236236
var accumulatedCollectionElements []K
237237

238+
//nolint:govet
238239
initialURL := col.client.buildURL(col.path)
239240
url := col.client.applyParams(
240241
initialURL,
@@ -252,7 +253,7 @@ func (col *CollectionHandler[K]) fireHTTPRequest(ctx context.Context) error {
252253

253254
col.clean = false
254255
col.collection = accumulatedCollectionElements
255-
col.rels = hyperHeaderParser(response.Header)
256+
col.rels = hyperHeaderParser(response.Header())
256257

257258
return nil
258259
}

pkg/serverscom.go

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package serverscom
22

33
import (
4-
"bytes"
54
"context"
65
"encoding/json"
76
"fmt"
8-
"io/ioutil"
97
"net/http"
108
"net/url"
119
"strings"
10+
11+
"github.com/go-resty/resty/v2"
1212
)
1313

1414
const defaultAPIEndpoint string = "https://api.servers.com/v1"
@@ -40,9 +40,7 @@ type Client struct {
4040

4141
KubernetesClusters KubernetesClustersService
4242

43-
token string
44-
45-
client *http.Client
43+
client *resty.Client
4644
}
4745

4846
// NewClient builds a new client with token
@@ -56,13 +54,15 @@ func NewClientWithEndpoint(token, baseURL string) *Client {
5654
Proxy: http.ProxyFromEnvironment,
5755
}
5856

59-
client := &http.Client{Transport: tr}
57+
rClient := resty.NewWithClient(&http.Client{Transport: tr})
58+
59+
rClient.SetAuthToken(token)
60+
rClient.SetHeader("Content-Type", "application/json")
61+
rClient.SetHeader("User-Agent", "go-serverscom-client")
6062

6163
scClient := &Client{
62-
baseURL: baseURL,
63-
UserAgent: "go-serverscom-client",
64-
token: token,
65-
client: client,
64+
baseURL: baseURL,
65+
client: rClient,
6666
}
6767

6868
scClient.configureResources()
@@ -75,6 +75,11 @@ func (cli *Client) SetupUserAgent(userAgent string) {
7575
cli.UserAgent = userAgent
7676
}
7777

78+
// SetVerbose sets debug mode for client
79+
func (cli *Client) SetVerbose(verbose bool) {
80+
cli.client.SetDebug(verbose)
81+
}
82+
7883
func (cli *Client) configureResources() {
7984
cli.CloudComputingInstances = &CloudComputingInstancesHandler{cli}
8085
cli.Hosts = &HostsHandler{cli}
@@ -122,51 +127,31 @@ func (cli *Client) applyParams(endpointURL string, params map[string]string) str
122127
)
123128
}
124129

125-
func (cli *Client) buildAndExecRequestWithResponse(ctx context.Context, method, endpointURL string, body []byte) (*http.Response, []byte, error) {
126-
var req *http.Request
127-
var err error
130+
func (cli *Client) buildAndExecRequestWithResponse(ctx context.Context, method, endpointURL string, body []byte) (*resty.Response, []byte, error) {
131+
request := cli.client.R().SetContext(ctx)
128132

129133
if body != nil {
130-
reader := bytes.NewReader(body)
131-
req, err = http.NewRequest(method, endpointURL, reader)
132-
} else {
133-
req, err = http.NewRequest(method, endpointURL, nil)
134-
}
135-
136-
if err != nil {
137-
return nil, nil, err
134+
request.SetBody(body)
138135
}
139136

140-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", cli.token))
141-
req.Header.Add("Content-Type", "application/json")
142-
req.Header.Set("User-Agent", cli.UserAgent)
143-
144-
resp, err := cli.client.Do(req.WithContext(ctx))
145-
137+
resp, err := request.Execute(method, endpointURL)
146138
if err != nil {
147139
return nil, nil, fmt.Errorf("Client request error: %q", err)
148140
}
149141

150-
defer resp.Body.Close()
151-
152-
contents, err := ioutil.ReadAll(resp.Body)
142+
contents := resp.Body()
153143

154-
if err != nil {
155-
return nil, nil, fmt.Errorf("Client can't read body: %q", err)
156-
}
157-
158-
if resp.StatusCode < 400 {
144+
if resp.StatusCode() < 400 {
159145
return resp, contents, nil
160146
}
161147

162-
contentType := resp.Header.Get("Content-Type")
163-
164-
var responseError = responseErrorWrapper{}
148+
contentType := resp.Header().Get("Content-Type")
149+
var responseError responseErrorWrapper
165150

166151
if strings.HasPrefix(contentType, "application/json") {
167152
if err := json.Unmarshal(contents, &responseError); err != nil {
168153
return nil, nil, newParsingError(
169-
resp.StatusCode,
154+
resp.StatusCode(),
170155
string(contents),
171156
err,
172157
)
@@ -176,23 +161,23 @@ func (cli *Client) buildAndExecRequestWithResponse(ctx context.Context, method,
176161
responseError.Message = string(contents)
177162
}
178163

179-
switch resp.StatusCode {
164+
switch resp.StatusCode() {
180165
case 400:
181-
return nil, nil, newBadRequestError(resp.StatusCode, responseError.Code, responseError.Message)
166+
return nil, nil, newBadRequestError(resp.StatusCode(), responseError.Code, responseError.Message)
182167
case 401:
183-
return nil, nil, newUnauthorizedError(resp.StatusCode, responseError.Code, responseError.Message)
168+
return nil, nil, newUnauthorizedError(resp.StatusCode(), responseError.Code, responseError.Message)
184169
case 403:
185-
return nil, nil, newForbiddenError(resp.StatusCode, responseError.Code, responseError.Message)
170+
return nil, nil, newForbiddenError(resp.StatusCode(), responseError.Code, responseError.Message)
186171
case 404:
187-
return nil, nil, newNotFoundError(resp.StatusCode, responseError.Code, responseError.Message)
172+
return nil, nil, newNotFoundError(resp.StatusCode(), responseError.Code, responseError.Message)
188173
case 409:
189-
return nil, nil, newConflictError(resp.StatusCode, responseError.Code, responseError.Message)
174+
return nil, nil, newConflictError(resp.StatusCode(), responseError.Code, responseError.Message)
190175
case 422:
191-
return nil, nil, newUnprocessableEntityError(resp.StatusCode, responseError.Code, responseError.Message, responseError.Errors)
176+
return nil, nil, newUnprocessableEntityError(resp.StatusCode(), responseError.Code, responseError.Message, responseError.Errors)
192177
case 500:
193-
return nil, nil, newInternalServerError(resp.StatusCode, responseError.Code, responseError.Message)
178+
return nil, nil, newInternalServerError(resp.StatusCode(), responseError.Code, responseError.Message)
194179
default:
195-
return nil, nil, fmt.Errorf("Unexpected response code: %d, with body: %s", resp.StatusCode, string(contents))
180+
return nil, nil, fmt.Errorf("Unexpected response code: %d, with body: %s", resp.StatusCode(), string(contents))
196181
}
197182
}
198183

pkg/test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package serverscom
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httptest"
99
"os"
@@ -103,7 +103,7 @@ func (fs *fakeServer) WithResponseBodyStub(filename string) *fakeServer {
103103
_, currentFile, _, _ := runtime.Caller(1)
104104
stubFilePath := path.Join(path.Dir(currentFile), "..", filename)
105105

106-
stub, err := ioutil.ReadFile(stubFilePath)
106+
stub, err := os.ReadFile(stubFilePath)
107107
if err != nil {
108108
panic(fmt.Sprintf("Stub error: %q", err))
109109
}
@@ -127,7 +127,7 @@ func (fs *fakeServer) WithResponseBodyStubFile(filePath string) *fakeServer {
127127

128128
defer f.Close()
129129

130-
b, err := ioutil.ReadAll(f)
130+
b, err := io.ReadAll(f)
131131
if err != nil {
132132
panic(err)
133133
}
@@ -179,7 +179,7 @@ func (fs *fakeServer) Build() (*fakeServer, *Client) {
179179
}
180180

181181
if currentRequest.RequestBody != "" {
182-
b, err := ioutil.ReadAll(r.Body)
182+
b, err := io.ReadAll(r.Body)
183183
defer r.Body.Close()
184184
if err != nil {
185185
http.Error(w, err.Error(), 500)

pkg/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ type SSHKeyCreateInput struct {
367367

368368
// SSHKeyUpdateInput represents ssh key update input
369369
type SSHKeyUpdateInput struct {
370-
Name string `json:"name"`
370+
Name string `json:"name,omitempty"`
371371
Labels map[string]string `json:"labels,omitempty"`
372372
}
373373

0 commit comments

Comments
 (0)