Passed
Pull Request — master (#41)
by Stefano
02:42
created

client.decorateTransportWithHeadersDecorator   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
package client
2
3
import (
4
	"errors"
5
	"net/http"
6
)
7
8
func decorateTransportWithHeadersDecorator(decorated http.RoundTripper, headers map[string]string) (*headersTransportDecorator, error) {
9
	if decorated == nil {
10
		return nil, errors.New("decorated round tripper is nil")
11
	}
12
13
	if headers == nil {
14
		return nil, errors.New("headers is nil")
15
	}
16
17
	return &headersTransportDecorator{decorated: decorated, headers: headers}, nil
18
}
19
20
type headersTransportDecorator struct {
21
	decorated http.RoundTripper
22
	headers   map[string]string
23
}
24
25
func (h *headersTransportDecorator) RoundTrip(r *http.Request) (*http.Response, error) {
26
	for key, value := range h.headers {
27
		r.Header.Set(key, value)
28
	}
29
30
	return h.decorated.RoundTrip(r)
31
}
32