github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/context/context.go (about)

     1  package context
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/pkg/version"
     7  )
     8  
     9  const (
    10  	// RequestID is the unique ID for each http request
    11  	RequestID = "request-id"
    12  
    13  	// APIVersion is the client's requested API version
    14  	APIVersion = "api-version"
    15  )
    16  
    17  // Context is just our own wrapper for the golang 'Context' - mainly
    18  // so we can add our over version of the funcs.
    19  type Context struct {
    20  	context.Context
    21  }
    22  
    23  // Background creates a new Context based on golang's default one.
    24  func Background() Context {
    25  	return Context{context.Background()}
    26  }
    27  
    28  // WithValue will return a Context that has this new key/value pair
    29  // associated with it. Just uses the golang version but then wraps it.
    30  func WithValue(ctx Context, key, value interface{}) Context {
    31  	return Context{context.WithValue(ctx, key, value)}
    32  }
    33  
    34  // RequestID is a utility func to make it easier to get the
    35  // request ID associated with this Context/request.
    36  func (ctx Context) RequestID() string {
    37  	val := ctx.Value(RequestID)
    38  	if val == nil {
    39  		return ""
    40  	}
    41  
    42  	id, ok := val.(string)
    43  	if !ok {
    44  		// Ideally we shouldn't panic but we also should never get here
    45  		panic("Context RequestID isn't a string")
    46  	}
    47  	return id
    48  }
    49  
    50  // Version is a utility func to make it easier to get the
    51  // API version string associated with this Context/request.
    52  func (ctx Context) Version() version.Version {
    53  	val := ctx.Value(APIVersion)
    54  	if val == nil {
    55  		return version.Version("")
    56  	}
    57  
    58  	ver, ok := val.(version.Version)
    59  	if !ok {
    60  		// Ideally we shouldn't panic but we also should never get here
    61  		panic("Context APIVersion isn't a version.Version")
    62  	}
    63  	return ver
    64  }