-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
X-Goog-Request-Reason
header if environment variable `CLOUDSDK_…
…CORE_REQUEST_REASON` is set (#4356) (#8220) * add header transport layer * some changes * set transport to be headertransport * export file for tpgc * update * revert ordering change to parameter initialization... default scopes need to be retained for token initialization on vcr tests Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
0a20c2c
commit 07ae6ae
Showing
3 changed files
with
44 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:none | ||
|
||
``` |
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,35 @@ | ||
package google | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
) | ||
|
||
// adapted from https://stackoverflow.com/questions/51325704/adding-a-default-http-header-in-go | ||
type headerTransportLayer struct { | ||
http.Header | ||
baseTransit http.RoundTripper | ||
} | ||
|
||
func newTransportWithHeaders(baseTransit http.RoundTripper) headerTransportLayer { | ||
if baseTransit == nil { | ||
baseTransit = http.DefaultTransport | ||
} | ||
|
||
headers := make(http.Header) | ||
if requestReason := os.Getenv("CLOUDSDK_CORE_REQUEST_REASON"); requestReason != "" { | ||
headers.Set("X-Goog-Request-Reason", requestReason) | ||
} | ||
|
||
return headerTransportLayer{Header: headers, baseTransit: baseTransit} | ||
} | ||
|
||
func (h headerTransportLayer) RoundTrip(req *http.Request) (*http.Response, error) { | ||
for key, value := range h.Header { | ||
// only set headers that are not previously defined | ||
if _, ok := req.Header[key]; !ok { | ||
req.Header[key] = value | ||
} | ||
} | ||
return h.baseTransit.RoundTrip(req) | ||
} |