Skip to content

Commit

Permalink
WithAPIVersion handler and default handlers implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcampbell committed Nov 29, 2023
1 parent c352388 commit bed03f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
14 changes: 14 additions & 0 deletions github/authentication/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ func (r *Request) WithUserAgent(userAgent string) {
func (r *Request) WithDefaultUserAgent() {
r.WithUserAgent(UserAgentValue)
}

// WithAPIVersion sets the API version header for each request
func (r *Request) WithAPIVersion(version string) {
if r.Headers.ContainsKey(APIVersionKey) {
r.Headers.Remove(APIVersionKey)
}
r.Headers.Add(APIVersionKey, version)
}

// WithDefaultAPIVersion sets the API version header to the default (the version used
// to generate the code) for each request
func (r *Request) WithDefaultAPIVersion() {
r.WithAPIVersion(APIVersionValue)
}
38 changes: 26 additions & 12 deletions github/authentication/token_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,39 @@ type TokenProvider struct {

type TokenProviderOption func(*TokenProvider, *Request)

// WithDefaultUserAgent sets the User-Agent string sent for requests to the default
// for this SDK.
func WithDefaultUserAgent() TokenProviderOption {
return func(t *TokenProvider, r *Request) {
r.WithDefaultUserAgent()
}
}

// WithUserAgent sets the User-Agent string sent with each request.
func WithUserAgent(userAgent string) TokenProviderOption {
return func(t *TokenProvider, r *Request) {
r.WithUserAgent(userAgent)
}
}

// WithDefaultAPIVersion sets the API version header sent with each request.
func WithDefaultAPIVersion() TokenProviderOption {
return func(t *TokenProvider, r *Request) {
r.WithDefaultAPIVersion()
}
}

// WithAPIVersion sets the API version header sent with each request.
func WithAPIVersion(version string) TokenProviderOption {
return func(t *TokenProvider, r *Request) {
r.WithAPIVersion(version)
}
}

// TODO(kfcampbell): implement new constructor with allowedHosts

// NewTokenProvider creates an instance of TokenProvider with the specified token and options.
// An error is returned when the token present is an empty string.
func NewTokenProvider(apiToken string, options ...TokenProviderOption) (*TokenProvider, error) {
if apiToken == "" {
return nil, fmt.Errorf("API token must not be an empty string")
Expand All @@ -40,25 +60,19 @@ func NewTokenProvider(apiToken string, options ...TokenProviderOption) (*TokenPr
return provider, nil
}

var defaultHandlers = []TokenProviderOption{WithDefaultUserAgent(), WithDefaultAPIVersion()}

// AuthenticateRequest applies the default options for each request, then the user's options
// if present in the TokenProvider.
func (t *TokenProvider) AuthenticateRequest(context context.Context, request *abs.RequestInformation, additionalAuthenticationContext map[string]interface{}) error {
reqWrapper := &Request{RequestInformation: request}

if reqWrapper.Headers == nil {
reqWrapper.Headers = abs.NewRequestHeaders()
}

// TODO(kfcampbell): cut this if chain over to new TokenProviderOption pattern
// will need to implement functionality in request.go and here for the below options
if !request.Headers.ContainsKey(AuthorizationKey) {
request.Headers.Add(AuthorizationKey, fmt.Sprintf("%v %v", AuthType, t.token))
}

if !request.Headers.ContainsKey(UserAgentKey) {
request.Headers.Add(UserAgentKey, UserAgentValue)
}

if !request.Headers.ContainsKey(APIVersionKey) {
request.Headers.Add(APIVersionKey, APIVersionValue)
for _, option := range defaultHandlers {
option(t, reqWrapper)
}

// apply user options after defaults
Expand Down

0 comments on commit bed03f9

Please sign in to comment.