Browse Source

Add a LoginWithToken method which is a cheaper way to login. Closes #89.

Eventually we should consider allowing Login/LoginWithToken to mutate
s.Token, it would probably simplify the API a bit.
Chris Rhodes 9 years ago
parent
commit
3561ad1fa6
1 changed files with 14 additions and 2 deletions
  1. 14 2
      restapi.go

+ 14 - 2
restapi.go

@@ -128,8 +128,8 @@ func unmarshal(data []byte, v interface{}) error {
 // Functions specific to Discord Sessions
 // ------------------------------------------------------------------------------------------------
 
-// Login asks the Discord server for an authentication token
-func (s *Session) Login(email string, password string) (token string, err error) {
+// Login asks the Discord server for an authentication token.
+func (s *Session) Login(email, password string) (token string, err error) {
 
 	data := struct {
 		Email    string `json:"email"`
@@ -154,6 +154,18 @@ func (s *Session) Login(email string, password string) (token string, err error)
 	return
 }
 
+// LoginWithToken will verify a login token, or return a new one if it is invalid.
+// This is the preferred way to login, as it uses less rate limiting quota.
+func (s *Session) LoginWithToken(email, password, token string) (token string, err error) {
+
+	old := s.Token
+	s.Token = token
+	token, err = s.Login(email, password)
+	s.Token = old
+
+	return
+}
+
 // Register sends a Register request to Discord, and returns the authentication token
 // Note that this account is temporary and should be verified for future use.
 // Another option is to save the authentication token external, but this isn't recommended.