Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
add api version header to api requests from the cli (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
krancour authored Feb 7, 2018
1 parent 52b7c1e commit 975d319
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 81 deletions.
18 changes: 0 additions & 18 deletions contrib/pkg/client/auth.go

This file was deleted.

12 changes: 2 additions & 10 deletions contrib/pkg/client/bind.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -33,16 +32,9 @@ func Bind(
if err != nil {
return "", nil, fmt.Errorf("error encoding request body: %s", err)
}
req, err := http.NewRequest(
http.MethodPut,
url,
bytes.NewBuffer(json),
)
req, err := newRequest(http.MethodPut, url, username, password, json)
if err != nil {
return "", nil, fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
addAuthHeader(req, username, password)
return "", nil, err
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
Expand Down
7 changes: 2 additions & 5 deletions contrib/pkg/client/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ func GetCatalog(
password string,
) (service.Catalog, error) {
url := fmt.Sprintf("%s/v2/catalog", getBaseURL(host, port))
req, err := http.NewRequest(http.MethodGet, url, nil)
req, err := newRequest(http.MethodGet, url, username, password, nil)
if err != nil {
return nil, fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
addAuthHeader(req, username, password)
return nil, err
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
Expand Down
24 changes: 23 additions & 1 deletion contrib/pkg/client/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
package client

import "fmt"
import (
"bytes"
"fmt"
"net/http"
)

func getBaseURL(host string, port int) string {
return fmt.Sprintf("http://%s:%d", host, port)
}

func newRequest(
method string,
url string,
username string,
password string,
body []byte,
) (*http.Request, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
req.SetBasicAuth(username, password)
}
req.Header.Add("X-Broker-API-Version", "2.13")
return req, nil
}
11 changes: 2 additions & 9 deletions contrib/pkg/client/deprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ func Deprovision(
getBaseURL(host, port),
instanceID,
)
req, err := http.NewRequest(
http.MethodDelete,
url,
nil,
)
req, err := newRequest(http.MethodDelete, url, username, password, nil)
if err != nil {
return fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
addAuthHeader(req, username, password)
return err
}
q := req.URL.Query()
q.Add("accepts_incomplete", "true")
Expand Down
11 changes: 2 additions & 9 deletions contrib/pkg/client/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ func Poll(
getBaseURL(host, port),
instanceID,
)
req, err := http.NewRequest(
http.MethodGet,
url,
nil,
)
req, err := newRequest(http.MethodGet, url, username, password, nil)
if err != nil {
return "", fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
addAuthHeader(req, username, password)
return "", err
}
q := req.URL.Query()
q.Add("operation", operation)
Expand Down
12 changes: 2 additions & 10 deletions contrib/pkg/client/provision.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"bytes"
"fmt"
"net/http"

Expand Down Expand Up @@ -36,16 +35,9 @@ func Provision(
if err != nil {
return "", fmt.Errorf("error encoding request body: %s", err)
}
req, err := http.NewRequest(
http.MethodPut,
url,
bytes.NewBuffer(json),
)
req, err := newRequest(http.MethodPut, url, username, password, json)
if err != nil {
return "", fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
addAuthHeader(req, username, password)
return "", err
}
q := req.URL.Query()
q.Add("accepts_incomplete", "true")
Expand Down
11 changes: 2 additions & 9 deletions contrib/pkg/client/unbind.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,9 @@ func Unbind(
instanceID,
bindingID,
)
req, err := http.NewRequest(
http.MethodDelete,
url,
nil,
)
req, err := newRequest(http.MethodDelete, url, username, password, nil)
if err != nil {
return fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
addAuthHeader(req, username, password)
return err
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
Expand Down
12 changes: 2 additions & 10 deletions contrib/pkg/client/update.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"bytes"
"fmt"
"net/http"

Expand Down Expand Up @@ -33,16 +32,9 @@ func Update(
if err != nil {
return fmt.Errorf("error encoding request body: %s", err)
}
req, err := http.NewRequest(
http.MethodPatch,
url,
bytes.NewBuffer(json),
)
req, err := newRequest(http.MethodPatch, url, username, password, json)
if err != nil {
return fmt.Errorf("error building request: %s", err)
}
if username != "" || password != "" {
addAuthHeader(req, username, password)
return err
}
q := req.URL.Query()
q.Add("accepts_incomplete", "true")
Expand Down

0 comments on commit 975d319

Please sign in to comment.