| Total Lines | 30 |
| Duplicated Lines | 0 % |
| Changes | 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 |