Repository: nicklaw5/helix (go)

Issue:
How can I use current access token with ValidateToken?
I have an app that runs while I'm streaming, using this library for Twitch integration (thanks!). I'd like to check if the access token is valid before making API calls, since it's quite likely that my token may expire so I can use the refresh token to update it in that case.

How can I get my current access token from the client in order to check if it is valid? Or is there a way to ask the method itself to use the current token rather than a provided one? Is this a feature that would be useful to others?

Pull request:
Refactor access token methods
\*\*\****Breaking Change***\*\*\*

Changes:
- Renamed `GetUserAccessToken` method to `RequestUserAccessToken`
- Renamed `GetAppAccessToken` method to `RequestAppAccessToken`
- Add two new methods `GetUserAccessToken` and `GetAppAccessToken` for returning the currently set access tokens

Closes #56 

Patch:
diff --git a/authentication.go b/authentication.go
index a9e872e..4aacb17 100644
--- a/authentication.go
+++ b/authentication.go
@@ -55,8 +55,8 @@ type AppAccessTokenResponse struct {
 	Data AccessCredentials
 }
 
-// GetAppAccessToken ...
-func (c *Client) GetAppAccessToken(scopes []string) (*AppAccessTokenResponse, error) {
+// RequestAppAccessToken ...
+func (c *Client) RequestAppAccessToken(scopes []string) (*AppAccessTokenResponse, error) {
 	opts := c.opts
 	data := &accessTokenRequestData{
 		ClientID:     opts.ClientID,
@@ -96,8 +96,8 @@ type accessTokenRequestData struct {
 	Scopes       []string `query:"scope"`
 }
 
-// GetUserAccessToken ...
-func (c *Client) GetUserAccessToken(code string) (*UserAccessTokenResponse, error) {
+// RequestUserAccessToken ...
+func (c *Client) RequestUserAccessToken(code string) (*UserAccessTokenResponse, error) {
 	opts := c.opts
 	data := &accessTokenRequestData{
 		Code:         code,
diff --git a/docs/authentication_docs.md b/docs/authentication_docs.md
index 410e08a..067f3b1 100644
--- a/docs/authentication_docs.md
+++ b/docs/authentication_docs.md
@@ -45,7 +45,7 @@ if err != nil {
 
 code := "your-authentication-code"
 
-resp, err := client.GetUserAccessToken(code)
+resp, err := client.RequestUserAccessToken(code)
 if err != nil {
     // handle error
 }
@@ -91,7 +91,7 @@ if err != nil {
     // handle error
 }
 
-userAccessToken := "your-user-access-token-to-revoke"
+userAccessToken := client.GetUserAccessToken()
 
 resp, err := client.RevokeUserAccessToken(userAccessToken)
 if err != nil {
@@ -113,7 +113,7 @@ if err != nil {
     // handle error
 }
 
-userAccessToken := "your-user-access-token-to-validate"
+userAccessToken := client.GetUserAccessToken()
 
 isValid, resp, err := client.ValidateToken(userAccessToken)
 if err != nil {
@@ -140,7 +140,7 @@ if err != nil {
     // handle error
 }
 
-resp, err := client.GetAppAccessToken([]string{"user:read:email"})
+resp, err := client.RequestAppAccessToken([]string{"user:read:email"})
 if err != nil {
     // handle error
 }
diff --git a/helix.go b/helix.go
index 88f0899..00e4c14 100644
--- a/helix.go
+++ b/helix.go
@@ -404,6 +404,11 @@ func setResponseStatusCode(v interface{}, fieldName string, code int) {
 	field.SetInt(int64(code))
 }
 
+// GetAppAccessToken returns the current app access token.
+func (c *Client) GetAppAccessToken() string {
+	return c.opts.AppAccessToken
+}
+
 // SetAppAccessToken ...
 func (c *Client) SetAppAccessToken(accessToken string) {
 	c.mu.Lock()
@@ -411,6 +416,11 @@ func (c *Client) SetAppAccessToken(accessToken string) {
 	c.opts.AppAccessToken = accessToken
 }
 
+// GetUserAccessToken returns the current user access token.
+func (c *Client) GetUserAccessToken() string {
+	return c.opts.UserAccessToken
+}
+
 // SetUserAccessToken ...
 func (c *Client) SetUserAccessToken(accessToken string) {
 	c.mu.Lock()


Test patch:
diff --git a/authentication_test.go b/authentication_test.go
index 6934de7..a18d020 100644
--- a/authentication_test.go
+++ b/authentication_test.go
@@ -56,7 +56,7 @@ func TestGetAuthorizationURL(t *testing.T) {
 	}
 }
 
-func TestGetAppAccessToken(t *testing.T) {
+func TestRequestAppAccessToken(t *testing.T) {
 	t.Parallel()
 
 	testCases := []struct {
@@ -97,7 +97,7 @@ func TestGetAppAccessToken(t *testing.T) {
 	for _, testCase := range testCases {
 		c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
 
-		resp, err := c.GetAppAccessToken([]string{"some-scope"})
+		resp, err := c.RequestAppAccessToken([]string{"some-scope"})
 		if err != nil {
 			t.Error(err)
 		}
@@ -130,7 +130,7 @@ func TestGetAppAccessToken(t *testing.T) {
 	}
 }
 
-func TestGetUserAccessToken(t *testing.T) {
+func TestRequestUserAccessToken(t *testing.T) {
 	t.Parallel()
 
 	testCases := []struct {
@@ -218,7 +218,7 @@ func TestGetUserAccessToken(t *testing.T) {
 	for _, testCase := range testCases {
 		c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
 
-		resp, err := c.GetUserAccessToken(testCase.code)
+		resp, err := c.RequestUserAccessToken(testCase.code)
 		if err != nil {
 			t.Error(err)
 		}
diff --git a/helix_test.go b/helix_test.go
index fa7633b..7bd3345 100644
--- a/helix_test.go
+++ b/helix_test.go
@@ -387,6 +387,25 @@ func TestDecodingBadJSON(t *testing.T) {
 	}
 }
 
+func TestGetAppAccessToken(t *testing.T) {
+	t.Parallel()
+
+	accessToken := "my-app-access-token"
+
+	client, err := NewClient(&Options{
+		ClientID: "cid",
+	})
+	if err != nil {
+		t.Errorf("Did not expect an error, got \"%s\"", err.Error())
+	}
+
+	client.SetAppAccessToken(accessToken)
+
+	if client.GetAppAccessToken() != accessToken {
+		t.Errorf("expected GetAppAccessToken to return \"%s\", got \"%s\"", accessToken, client.GetAppAccessToken())
+	}
+}
+
 func TestSetAppAccessToken(t *testing.T) {
 	t.Parallel()
 
@@ -406,6 +425,25 @@ func TestSetAppAccessToken(t *testing.T) {
 	}
 }
 
+func TestGetUserAccessToken(t *testing.T) {
+	t.Parallel()
+
+	accessToken := "my-user-access-token"
+
+	client, err := NewClient(&Options{
+		ClientID: "cid",
+	})
+	if err != nil {
+		t.Errorf("Did not expect an error, got \"%s\"", err.Error())
+	}
+
+	client.SetUserAccessToken(accessToken)
+
+	if client.GetUserAccessToken() != accessToken {
+		t.Errorf("expected GetUserAccessToken to return \"%s\", got \"%s\"", accessToken, client.GetUserAccessToken())
+	}
+}
+
 func TestSetUserAccessToken(t *testing.T) {
 	t.Parallel()