github.com/puellanivis/breton@v0.2.16/lib/files/httpfiles/context.go (about)

     1  package httpfiles
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  )
     7  
     8  type (
     9  	clientKey    struct{}
    10  	userAgentKey struct{}
    11  )
    12  
    13  // WithClient attaches an http.Client to the Context, that will be used by this library as the http.Client
    14  func WithClient(ctx context.Context, cl *http.Client) context.Context {
    15  	return context.WithValue(ctx, clientKey{}, cl)
    16  }
    17  
    18  func getClient(ctx context.Context) (*http.Client, bool) {
    19  	cl, ok := ctx.Value(clientKey{}).(*http.Client)
    20  	return cl, ok
    21  }
    22  
    23  // WithUserAgent attaches an string to the Context, that will be used by this library as the User-Agent in all headers
    24  func WithUserAgent(ctx context.Context, ua string) context.Context {
    25  	return context.WithValue(ctx, userAgentKey{}, ua)
    26  }
    27  
    28  func getUserAgent(ctx context.Context) (string, bool) {
    29  	ua, ok := ctx.Value(userAgentKey{}).(string)
    30  	return ua, ok
    31  }