github.com/optim-corp/cios-golang-sdk@v0.5.1/ctx/ciosctx.go (about)

     1  package ciosctx
     2  
     3  import (
     4  	"context"
     5  	"regexp"
     6  
     7  	"github.com/fcfcqloow/go-advance/check"
     8  
     9  	"github.com/optim-corp/cios-golang-sdk/cios"
    10  )
    11  
    12  type RequestCtx context.Context
    13  
    14  func Background() RequestCtx {
    15  	return context.Background()
    16  }
    17  
    18  func WithToken(ctx RequestCtx, token string) RequestCtx {
    19  	if check.IsNil(ctx) {
    20  		ctx = Background()
    21  	}
    22  	if token == "" {
    23  		return ctx
    24  	}
    25  	return context.WithValue(ctx, cios.ContextAccessToken, regexp.MustCompile(`^bearer|Bearer| `).ReplaceAllString(token, ""))
    26  }
    27  
    28  func Token(ctx RequestCtx) (token *string) {
    29  	if !check.IsNil(ctx) {
    30  		if _token, ok := ctx.Value(cios.ContextAccessToken).(string); ok {
    31  			token = &_token
    32  		}
    33  	}
    34  	return
    35  }