Skip to content

Commit af59376

Browse files
author
Touko Hallasmaa
authored
refactor: do not set contenttype for client (#74)
* refactor: do not set contenttype for client expose method for dispatching request object * update `performJSONRequest` to use `PerformRequest` * fix: return error from PerformRequest
1 parent eb04dfd commit af59376

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

upcloud/client/client.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ const (
2323

2424
// Client represents an API client
2525
type Client struct {
26-
userName string
27-
password string
28-
httpClient *http.Client
29-
contentType string
26+
userName string
27+
password string
28+
httpClient *http.Client
3029
}
3130

3231
// New creates ands returns a new client configured with the specified user and password
@@ -154,29 +153,23 @@ func (c *Client) PerformJSONPutUploadRequest(url string, requestBody io.Reader)
154153
return c.performJSONRequest(request)
155154
}
156155

157-
func (c *Client) SetContentType(ct string) {
158-
c.contentType = ct
159-
}
160-
161-
func (c *Client) GetContentType() string {
162-
if c.contentType == "" {
163-
return "application/json"
164-
}
165-
return c.contentType
166-
}
167-
168156
// Adds common headers to the specified request
169-
func (c *Client) addJSONRequestHeaders(request *http.Request) *http.Request {
157+
func (c *Client) AddRequestHeaders(request *http.Request) *http.Request {
170158
request.SetBasicAuth(c.userName, c.password)
171159
request.Header.Set("Accept", "application/json")
172-
request.Header.Set("Content-Type", c.GetContentType())
160+
request.Header.Set("Content-Type", "application/json")
173161

174162
return request
175163
}
176164

177165
// Performs the specified HTTP request and returns the response through handleResponse()
178166
func (c *Client) performJSONRequest(request *http.Request) ([]byte, error) {
179-
c.addJSONRequestHeaders(request)
167+
c.AddRequestHeaders(request)
168+
return c.PerformRequest(request)
169+
}
170+
171+
// Performs the specified HTTP request and returns the response through handleResponse()
172+
func (c *Client) PerformRequest(request *http.Request) ([]byte, error) {
180173
response, err := c.httpClient.Do(request)
181174

182175
if err != nil {

upcloud/service/storage.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"net/http"
89
"os"
910
"time"
1011

@@ -314,15 +315,16 @@ func (s *Service) directStorageImport(r *request.CreateStorageImportRequest) (*u
314315
return nil, errors.New("no DirectUploadURL found in response")
315316
}
316317

317-
curContentType := s.client.GetContentType()
318-
if r.ContentType != "" {
319-
s.client.SetContentType(r.ContentType)
320-
}
321-
_, err = s.client.PerformJSONPutUploadRequest(storageImport.DirectUploadURL, bodyReader)
318+
req, err := http.NewRequest(http.MethodPut, storageImport.DirectUploadURL, bodyReader)
322319
if err != nil {
323320
return nil, err
324321
}
325-
s.client.SetContentType(curContentType)
322+
323+
s.client.AddRequestHeaders(req)
324+
req.Header.Add("Content-Type", r.ContentType)
325+
if _, err := s.client.PerformRequest(req); err != nil {
326+
return nil, err
327+
}
326328

327329
storageImport, err = s.GetStorageImportDetails(&request.GetStorageImportDetailsRequest{
328330
UUID: r.StorageUUID,

0 commit comments

Comments
 (0)