Skip to content

Commit

Permalink
refactor: do not set contenttype for client (#74)
Browse files Browse the repository at this point in the history
* refactor: do not set contenttype for client

expose method for dispatching request object

* update `performJSONRequest` to use `PerformRequest`

* fix: return error from PerformRequest
  • Loading branch information
Touko Hallasmaa authored Nov 19, 2020
1 parent eb04dfd commit af59376
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
29 changes: 11 additions & 18 deletions upcloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ const (

// Client represents an API client
type Client struct {
userName string
password string
httpClient *http.Client
contentType string
userName string
password string
httpClient *http.Client
}

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

func (c *Client) SetContentType(ct string) {
c.contentType = ct
}

func (c *Client) GetContentType() string {
if c.contentType == "" {
return "application/json"
}
return c.contentType
}

// Adds common headers to the specified request
func (c *Client) addJSONRequestHeaders(request *http.Request) *http.Request {
func (c *Client) AddRequestHeaders(request *http.Request) *http.Request {
request.SetBasicAuth(c.userName, c.password)
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", c.GetContentType())
request.Header.Set("Content-Type", "application/json")

return request
}

// Performs the specified HTTP request and returns the response through handleResponse()
func (c *Client) performJSONRequest(request *http.Request) ([]byte, error) {
c.addJSONRequestHeaders(request)
c.AddRequestHeaders(request)
return c.PerformRequest(request)
}

// Performs the specified HTTP request and returns the response through handleResponse()
func (c *Client) PerformRequest(request *http.Request) ([]byte, error) {
response, err := c.httpClient.Do(request)

if err != nil {
Expand Down
14 changes: 8 additions & 6 deletions upcloud/service/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"time"

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

curContentType := s.client.GetContentType()
if r.ContentType != "" {
s.client.SetContentType(r.ContentType)
}
_, err = s.client.PerformJSONPutUploadRequest(storageImport.DirectUploadURL, bodyReader)
req, err := http.NewRequest(http.MethodPut, storageImport.DirectUploadURL, bodyReader)
if err != nil {
return nil, err
}
s.client.SetContentType(curContentType)

s.client.AddRequestHeaders(req)
req.Header.Add("Content-Type", r.ContentType)
if _, err := s.client.PerformRequest(req); err != nil {
return nil, err
}

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

0 comments on commit af59376

Please sign in to comment.