-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically add Bearer auth scheme if not provided (#14)
* Add test coverage for WithAuth option * Replace WithAuth option with custom HTTP transport that automatically injects headers * Automatically add Bearer auth scheme if not provided Extract HTTP transport into internal package * Use string template instead of concatenation
- Loading branch information
Showing
4 changed files
with
132 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package internal | ||
|
||
import "net/http" | ||
|
||
// HeaderTransport is a custom RoundTripper that adds default headers to requests | ||
type HeaderTransport struct { | ||
Base http.RoundTripper | ||
Headers http.Header | ||
} | ||
|
||
func (t *HeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
for key, values := range t.Headers { | ||
for _, value := range values { | ||
req.Header.Add(key, value) | ||
} | ||
} | ||
base := t.Base | ||
if base == nil { | ||
base = http.DefaultTransport | ||
} | ||
return base.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters