Skip to content

Commit

Permalink
Move header constants to their own package
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcampbell committed Nov 30, 2023
1 parent bdb6e32 commit 71e83e8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
35 changes: 12 additions & 23 deletions github/authentication/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@ import (
"fmt"

abs "github.com/microsoft/kiota-abstractions-go"
"github.com/octokit/go-sdk/github/headers"
)

const AuthorizationKey = "Authorization"
const AuthType = "bearer"
const UserAgentKey = "User-Agent"

// TODO(kfcampbell): get the version and binary name from build settings rather than hard-coding
const UserAgentValue = "go-sdk@v0.0.0"

const APIVersionKey = "X-GitHub-Api-Version"

// TODO(kfcampbell): get the version from the generated code somehow
const APIVersionValue = "2022-11-28"

// Request provides a wrapper around Kiota's abs.RequestInformation type
type Request struct {
*abs.RequestInformation
Expand All @@ -26,35 +15,35 @@ type Request struct {
// WithAuthorization sets the Authorization header to the given token,
// prepended by the AuthType
func (r *Request) WithAuthorization(token string) {
if r.Headers.ContainsKey(AuthorizationKey) {
r.Headers.Remove(AuthorizationKey)
if r.Headers.ContainsKey(headers.AuthorizationKey) {
r.Headers.Remove(headers.AuthorizationKey)
}
r.Headers.Add(AuthorizationKey, fmt.Sprintf("%v %v", AuthType, token))
r.Headers.Add(headers.AuthorizationKey, fmt.Sprintf("%v %v", headers.AuthType, token))
}

// WithUserAgent allows the caller to set the User-Agent string for each request
func (r *Request) WithUserAgent(userAgent string) {
if r.Headers.ContainsKey(UserAgentKey) {
r.Headers.Remove(UserAgentKey)
if r.Headers.ContainsKey(headers.UserAgentKey) {
r.Headers.Remove(headers.UserAgentKey)
}
r.Headers.Add(UserAgentKey, userAgent)
r.Headers.Add(headers.UserAgentKey, userAgent)
}

// WithDefaultUserAgent sets the default User-Agent string for each request
func (r *Request) WithDefaultUserAgent() {
r.WithUserAgent(UserAgentValue)
r.WithUserAgent(headers.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)
if r.Headers.ContainsKey(headers.APIVersionKey) {
r.Headers.Remove(headers.APIVersionKey)
}
r.Headers.Add(APIVersionKey, version)
r.Headers.Add(headers.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)
r.WithAPIVersion(headers.APIVersionValue)
}
27 changes: 14 additions & 13 deletions github/authentication/token_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
abstractions "github.com/microsoft/kiota-abstractions-go"
http "github.com/microsoft/kiota-http-go"
"github.com/octokit/go-sdk/github/authentication"
"github.com/octokit/go-sdk/github/headers"
"github.com/octokit/go-sdk/github/octokit"
"github.com/octokit/go-sdk/github/octokit/user"
)
Expand All @@ -27,11 +28,11 @@ func TestTokenIsSetInAuthenticatedRequest(t *testing.T) {
t.Errorf("there should be no error when calling AuthenticateRequest")
}

if len(reqInfo.Headers.Get(authentication.AuthorizationKey)) != 1 {
if len(reqInfo.Headers.Get(headers.AuthorizationKey)) != 1 {
t.Errorf("there should be exactly one authorization key")
}

receivedToken := reqInfo.Headers.Get(authentication.AuthorizationKey)[0]
receivedToken := reqInfo.Headers.Get(headers.AuthorizationKey)[0]
if !strings.Contains(receivedToken, token) {
t.Errorf("received token doesn't match up with given token")
}
Expand All @@ -49,21 +50,21 @@ func TestDefaultRequestOptions(t *testing.T) {
t.Errorf("there should be no error when calling AuthenticateRequest")
}

apiVersions := reqInfo.Headers.Get(authentication.APIVersionKey)
apiVersions := reqInfo.Headers.Get(headers.APIVersionKey)
if len(apiVersions) != 1 {
t.Errorf("exactly one API version should be present in the request")
}

if apiVersions[0] != authentication.APIVersionValue {
if apiVersions[0] != headers.APIVersionValue {
t.Errorf("default API version is set incorrectly")
}

userAgents := reqInfo.Headers.Get(authentication.UserAgentKey)
userAgents := reqInfo.Headers.Get(headers.UserAgentKey)
if len(userAgents) != 1 {
t.Errorf("exactly one user agent string should be present in the request")
}

if userAgents[0] != authentication.UserAgentValue {
if userAgents[0] != headers.UserAgentValue {
t.Errorf("default user agent string is set incorrectly")
}
}
Expand All @@ -85,7 +86,7 @@ func TestOverwritingDefaultRequestOptions(t *testing.T) {
t.Errorf("should be no error when calling authenticated request")
}

apiVersions := reqInfo.Headers.Get(authentication.APIVersionKey)
apiVersions := reqInfo.Headers.Get(headers.APIVersionKey)
if len(apiVersions) != 1 {
t.Errorf("exactly one API version should be present in the request")
}
Expand All @@ -94,7 +95,7 @@ func TestOverwritingDefaultRequestOptions(t *testing.T) {
t.Errorf("default API version is set incorrectly")
}

userAgents := reqInfo.Headers.Get(authentication.UserAgentKey)
userAgents := reqInfo.Headers.Get(headers.UserAgentKey)
if len(userAgents) != 1 {
t.Errorf("exactly one user agent string should be present in the request")
}
Expand All @@ -115,7 +116,7 @@ func TestAnonymousAuthIsAllowed(t *testing.T) {
t.Errorf("should be no error when calling authenticated request")
}

authorizations := reqInfo.Headers.Get(authentication.AuthorizationKey)
authorizations := reqInfo.Headers.Get(headers.AuthorizationKey)
if len(authorizations) != 0 {
t.Errorf("no authorization header should be present in the request")
}
Expand All @@ -128,18 +129,18 @@ func TestTokenSetInRequestIsNotOverwritten(t *testing.T) {
)

requestToken := "dit dit dit dit / dit / dit dat dit dit / dit dat dat dit"
headers := abstractions.NewRequestHeaders()
headers.Add(authentication.AuthType, requestToken)
requestHeaders := abstractions.NewRequestHeaders()
requestHeaders.Add(headers.AuthType, requestToken)

reqInfo := abstractions.NewRequestInformation()
reqInfo.Headers = headers
reqInfo.Headers = requestHeaders
addtlContext := make(map[string]interface{})

err := provider.AuthenticateRequest(context.Background(), reqInfo, addtlContext)
if err != nil {
t.Errorf("AuthenticateRequest should not error")
}
reqInfoToken := reqInfo.Headers.Get(authentication.AuthorizationKey)[0]
reqInfoToken := reqInfo.Headers.Get(headers.AuthorizationKey)[0]

if !strings.Contains(reqInfoToken, providerToken) {
t.Errorf("received token doesn't match up with given token")
Expand Down
13 changes: 13 additions & 0 deletions github/headers/header_contents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package headers

const AuthorizationKey = "Authorization"
const AuthType = "bearer"
const UserAgentKey = "User-Agent"

// TODO(kfcampbell): get the version and binary name from build settings rather than hard-coding
const UserAgentValue = "go-sdk@v0.0.0"

const APIVersionKey = "X-GitHub-Api-Version"

// TODO(kfcampbell): get the version from the generated code somehow
const APIVersionValue = "2022-11-28"

0 comments on commit 71e83e8

Please sign in to comment.