1 Star 0 Fork 0

yangx282441848/scm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bearer.go 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package transport
import "net/http"
// BearerToken is an http.RoundTripper that makes HTTP
// requests, wrapping a base RoundTripper and adding an
// Authorization header with the Bearer Token.
type BearerToken struct {
Base http.RoundTripper
Token string // Bearer token
}
// RoundTrip adds the Authorization header to the request.
func (t *BearerToken) RoundTrip(r *http.Request) (*http.Response, error) {
// Do not overwrite the authorization header if exists.
if r.Header.Get("Authorization") != "" {
return t.base().RoundTrip(r)
}
r2 := cloneRequest(r)
r2.Header.Set("Authorization", "Bearer "+t.Token)
return t.base().RoundTrip(r2)
}
// base returns the base transport. If no base transport
// is configured, the default transport is returned.
func (t *BearerToken) base() http.RoundTripper {
if t.Base != nil {
return t.Base
}
return http.DefaultTransport
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yangx282441848/scm.git
git@gitee.com:yangx282441848/scm.git
yangx282441848
scm
scm
v1.16.8

搜索帮助

0d507c66 1850385 C8b1a773 1850385