Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
allow HttpClient to be created as secure or non secure
Browse files Browse the repository at this point in the history
1. added useHttps field so that client can use http or https scheme on demand
2. added NewHttpsClient(...) that returns a secure https client
  • Loading branch information
maximilien committed Apr 29, 2016
1 parent 9e73ca4 commit 85659de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
26 changes: 21 additions & 5 deletions client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ type HttpClient struct {
username string
password string

useHttps bool

apiUrl string

nonVerbose bool

templatePath string
}

func NewHttpClient(username, password, apiUrl, templatePath string) *HttpClient {
func NewHttpsClient(username, password, apiUrl, templatePath string) *HttpClient {
return NewHttpClient(username, password, apiUrl, templatePath, true)
}

func NewHttpClient(username, password, apiUrl, templatePath string, useHttps bool) *HttpClient {
pwd, err := os.Getwd()
if err != nil {
panic(err)
Expand All @@ -39,6 +45,8 @@ func NewHttpClient(username, password, apiUrl, templatePath string) *HttpClient
username: username,
password: password,

useHttps: useHttps,

apiUrl: apiUrl,

templatePath: filepath.Join(pwd, templatePath),
Expand All @@ -54,7 +62,7 @@ func NewHttpClient(username, password, apiUrl, templatePath string) *HttpClient
// Public methods

func (slc *HttpClient) DoRawHttpRequestWithObjectMask(path string, masks []string, requestType string, requestBody *bytes.Buffer) ([]byte, int, error) {
url := fmt.Sprintf("https://%s:%s@%s/%s", slc.username, slc.password, slc.apiUrl, path)
url := fmt.Sprintf("%s://%s:%s@%s/%s", slc.scheme(), slc.username, slc.password, slc.apiUrl, path)

url += "?objectMask="
for i := 0; i < len(masks); i++ {
Expand All @@ -68,14 +76,14 @@ func (slc *HttpClient) DoRawHttpRequestWithObjectMask(path string, masks []strin
}

func (slc *HttpClient) DoRawHttpRequestWithObjectFilter(path string, filters string, requestType string, requestBody *bytes.Buffer) ([]byte, int, error) {
url := fmt.Sprintf("https://%s:%s@%s/%s", slc.username, slc.password, slc.apiUrl, path)
url := fmt.Sprintf("%s://%s:%s@%s/%s", slc.scheme(), slc.username, slc.password, slc.apiUrl, path)
url += "?objectFilter=" + filters

return slc.makeHttpRequest(url, requestType, requestBody)
}

func (slc *HttpClient) DoRawHttpRequestWithObjectFilterAndObjectMask(path string, masks []string, filters string, requestType string, requestBody *bytes.Buffer) ([]byte, int, error) {
url := fmt.Sprintf("https://%s:%s@%s/%s", slc.username, slc.password, slc.apiUrl, path)
url := fmt.Sprintf("%s://%s:%s@%s/%s", slc.scheme(), slc.username, slc.password, slc.apiUrl, path)

url += "?objectFilter=" + filters

Expand All @@ -92,7 +100,7 @@ func (slc *HttpClient) DoRawHttpRequestWithObjectFilterAndObjectMask(path string
}

func (slc *HttpClient) DoRawHttpRequest(path string, requestType string, requestBody *bytes.Buffer) ([]byte, int, error) {
url := fmt.Sprintf("https://%s:%s@%s/%s", slc.username, slc.password, slc.apiUrl, path)
url := fmt.Sprintf("%s://%s:%s@%s/%s", slc.scheme(), slc.username, slc.password, slc.apiUrl, path)
return slc.makeHttpRequest(url, requestType, requestBody)
}

Expand Down Expand Up @@ -133,6 +141,14 @@ func (slc *HttpClient) CheckForHttpResponseErrors(data []byte) error {

// Private methods

func (slc *HttpClient) scheme() string {
if !slc.useHttps {
return "http"
}

return "https"
}

func (slc *HttpClient) makeHttpRequest(url string, requestType string, requestBody *bytes.Buffer) ([]byte, int, error) {
req, err := http.NewRequest(requestType, url, requestBody)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions client/http_client_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package client_test

//TODO: need a fake Golang HTTPClient to implement this test.

//Public methods
// DoRawHttpRequestWithObjectMask
// DoRawHttpRequestWithObjectFilter
// DoRawHttpRequestWithObjectFilterAndObjectMask
// DoRawHttpRequest
// GenerateRequestBody
// HasErrors
// CheckForHttpResponseErrors
2 changes: 1 addition & 1 deletion client/softlayer_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type SoftLayerClient struct {

func NewSoftLayerClient(username, apiKey string) *SoftLayerClient {
slc := &SoftLayerClient{
HttpClient: NewHttpClient(username, apiKey, SOFTLAYER_API_URL, TEMPLATE_ROOT_PATH),
HttpClient: NewHttpsClient(username, apiKey, SOFTLAYER_API_URL, TEMPLATE_ROOT_PATH),

softLayerServices: map[string]softlayer.Service{},
}
Expand Down

0 comments on commit 85659de

Please sign in to comment.