diff --git a/connector.go b/connector.go index 7df883b9..110d116f 100644 --- a/connector.go +++ b/connector.go @@ -86,6 +86,31 @@ type WapiRequestBuilder struct { authCfg AuthConfig } +type WapiRequestBuilderWithHeaders struct { + HttpRequestBuilder + header http.Header +} + +func NewWapiRequestBuilderWithHeaders(wrb *WapiRequestBuilder, header http.Header) (*WapiRequestBuilderWithHeaders, error) { + return &WapiRequestBuilderWithHeaders{ + HttpRequestBuilder: wrb, + header: header, + }, nil +} + +func (wrbh *WapiRequestBuilderWithHeaders) BuildRequest(r RequestType, obj IBObject, ref string, queryParams *QueryParams) (req *http.Request, err error) { + req, err = wrbh.HttpRequestBuilder.BuildRequest(r, obj, ref, queryParams) + if err != nil { + return req, err + } + for h, values := range wrbh.header { + for _, v := range values { + req.Header.Add(h, v) + } + } + return req, nil +} + type WapiHttpRequestor struct { client http.Client } diff --git a/connector_test.go b/connector_test.go index 7faecd29..644f3676 100644 --- a/connector_test.go +++ b/connector_test.go @@ -275,6 +275,43 @@ var _ = Describe("Connector", func() { }) }) + Describe("WapiRequestBuilderWithHeaders", func() { + host := "172.22.18.66" + version := "2.2" + port := "443" + username := "myname" + password := "mysecrete!" + hostCfg := HostConfig{ + Host: host, + Version: version, + Port: port, + } + authCfg := AuthConfig{ + Username: username, + Password: password, + } + + header := make(http.Header) + header.Add("x", "1") + header.Add("y", "2") + + wrb, _ := NewWapiRequestBuilder(hostCfg, authCfg) + wrbh, err := NewWapiRequestBuilderWithHeaders(wrb, header) + if err != nil { + panic("NewWapiRequestBuilderWithHeaders() is not expected to return an error") + } + + Describe("BuildRequest", func() { + It("should set given headers to request", func() { + var obj IBObject + req, _ := wrbh.BuildRequest(GET, obj, "ref", nil) + for k := range header { + Expect(header.Get(k)).To(Equal(req.Header.Get(k))) + } + }) + }) + }) + Describe("Connector Object Methods", func() { host := "172.22.18.66"