github.com/qiniu/go-sdk/v7@v7.27.0/sms/client/transport.go (about) 1 package client 2 3 import ( 4 "encoding/base64" 5 "net/http" 6 ) 7 8 // Mac qiniu mac type 9 type Mac struct { 10 AccessKey string 11 SecretKey []byte 12 } 13 14 // Transport with qiniu mac 15 type Transport struct { 16 mac Mac 17 Transport http.RoundTripper 18 } 19 20 // RoundTrip transport round trip method 21 func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) { 22 sign, err := SignRequest(t.mac.SecretKey, req) 23 if err != nil { 24 return 25 } 26 27 auth := "Qiniu " + t.mac.AccessKey + ":" + base64.URLEncoding.EncodeToString(sign) 28 req.Header.Set("Authorization", auth) 29 return t.Transport.RoundTrip(req) 30 } 31 32 // NestedObject return transport 33 func (t *Transport) NestedObject() interface{} { 34 return t.Transport 35 } 36 37 // NewTransport return transport with qiniu mac 38 func NewTransport(mac *Mac, transport http.RoundTripper) *Transport { 39 if transport == nil { 40 transport = http.DefaultTransport 41 } 42 43 t := &Transport{Transport: transport} 44 t.mac = *mac 45 46 return t 47 } 48 49 // NewClient return qiniu mac client 50 func NewClient(mac *Mac, transport http.RoundTripper) *http.Client { 51 t := NewTransport(mac, transport) 52 return &http.Client{Transport: t} 53 }