github.com/searKing/golang/go@v1.2.117/net/http/roundtrip.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http
     6  
     7  import "net/http"
     8  
     9  // RoundTripFunc is an adapter to allow the use of
    10  // ordinary functions as HTTP Transport handlers. If f is a function
    11  // with the appropriate signature, RoundTripFunc(f) is a
    12  // Handler that calls f.
    13  type RoundTripFunc func(req *http.Request) (resp *http.Response, err error)
    14  
    15  func (f RoundTripFunc) RoundTrip(req *http.Request) (resp *http.Response, err error) {
    16  	return f(req)
    17  }
    18  
    19  // RoundTripDecorator is an interface representing the ability to decorate or wrap a http.RoundTripper.
    20  type RoundTripDecorator interface {
    21  	WrapRoundTrip(rt http.RoundTripper) http.RoundTripper
    22  }
    23  
    24  // RoundTripDecorators defines a RoundTripDecorator slice.
    25  type RoundTripDecorators []RoundTripDecorator
    26  
    27  func (chain RoundTripDecorators) WrapRoundTrip(next http.RoundTripper) http.RoundTripper {
    28  	for i := range chain {
    29  		h := chain[len(chain)-1-i]
    30  		if h != nil {
    31  			next = h.WrapRoundTrip(next)
    32  		}
    33  	}
    34  	return next
    35  }