Skip to content

Commit 188c5ad

Browse files
author
postables
committed
enable supplying extra headers to the authenticate transport
1 parent 4e0549c commit 188c5ad

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

shell.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ func NewShellWithClient(url string, c *gohttp.Client) *Shell {
115115
// WithAuthorization(token).
116116
// Cat(hash)
117117
//
118-
func (s *Shell) WithAuthorization(token string) *Shell {
118+
func (s *Shell) WithAuthorization(token string, extraHeaders map[string]string) *Shell {
119119
return &Shell{
120120
url: s.url,
121121
httpcli: gohttp.Client{
122-
Transport: newAuthenticatedTransport(s.httpcli.Transport, token),
122+
Transport: newAuthenticatedTransport(s.httpcli.Transport, token, extraHeaders),
123123
},
124124
}
125125
}

shell_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const (
2323

2424
func TestAdd(t *testing.T) {
2525
is := is.New(t)
26-
s := NewShell(shellUrl)
26+
s := NewShell(shellUrl).WithAuthorization("", map[string]string{
27+
"X-Hold-Time": "1",
28+
})
2729

2830
mhash, err := s.Add(bytes.NewBufferString("Hello IPFS Shell tests"))
2931
is.Nil(err)

transport.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@ package shell
33
import "net/http"
44

55
type transport struct {
6-
token string
7-
httptr http.RoundTripper
6+
token string
7+
httptr http.RoundTripper
8+
extraHeaders map[string]string
89
}
910

10-
func newAuthenticatedTransport(tr http.RoundTripper, token string) *transport {
11+
func newAuthenticatedTransport(tr http.RoundTripper, token string, extraHeaders map[string]string) *transport {
1112
return &transport{
12-
token: token,
13-
httptr: tr,
13+
token: token,
14+
httptr: tr,
15+
extraHeaders: extraHeaders,
1416
}
1517
}
1618

1719
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
20+
if t.extraHeaders != nil {
21+
for k, v := range t.extraHeaders {
22+
req.Header.Set(k, v)
23+
}
24+
}
1825
req.Header.Set("Authorization", "Bearer "+t.token)
1926
return t.httptr.RoundTrip(req)
2027
}

0 commit comments

Comments
 (0)