github.com/qiniu/go-sdk/v7@v7.27.0/internal/clientv2/context.go (about)

     1  package clientv2
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"sort"
     7  )
     8  
     9  type intercetorsContextKey struct{}
    10  
    11  func WithInterceptors(req *http.Request, interceptors ...Interceptor) *http.Request {
    12  	newInterceptors, ok := req.Context().Value(intercetorsContextKey{}).(interceptorList)
    13  	if !ok {
    14  		newInterceptors = interceptorList(interceptors)
    15  	} else {
    16  		newInterceptors = append(newInterceptors, interceptors...)
    17  	}
    18  	return req.WithContext(context.WithValue(req.Context(), intercetorsContextKey{}, newInterceptors))
    19  }
    20  
    21  func getIntercetorsFromRequest(req *http.Request) interceptorList {
    22  	if req == nil {
    23  		return interceptorList{}
    24  	}
    25  	interceptors, ok := req.Context().Value(intercetorsContextKey{}).(interceptorList)
    26  	if !ok {
    27  		return interceptorList{}
    28  	}
    29  	sort.Sort(interceptors)
    30  	return interceptors
    31  }