added discord api limiting
This commit is contained in:
@@ -43,7 +43,7 @@ func GetOAuthLink(cfg *Config, state, trustedHost string) (string, error) {
|
||||
return fmt.Sprintf("%s?%s", oauthurl, values.Encode()), nil
|
||||
}
|
||||
|
||||
func AuthorizeWithCode(cfg *Config, code, trustedHost string) (*Token, error) {
|
||||
func AuthorizeWithCode(cfg *Config, code, trustedHost string, apiClient *APIClient) (*Token, error) {
|
||||
if code == "" {
|
||||
return nil, errors.New("code cannot be empty")
|
||||
}
|
||||
@@ -53,6 +53,9 @@ func AuthorizeWithCode(cfg *Config, code, trustedHost string) (*Token, error) {
|
||||
if trustedHost == "" {
|
||||
return nil, errors.New("trustedHost cannot be empty")
|
||||
}
|
||||
if apiClient == nil {
|
||||
return nil, errors.New("apiClient cannot be nil")
|
||||
}
|
||||
// Prepare form data
|
||||
data := url.Values{}
|
||||
data.Set("grant_type", "authorization_code")
|
||||
@@ -72,9 +75,8 @@ func AuthorizeWithCode(cfg *Config, code, trustedHost string) (*Token, error) {
|
||||
|
||||
// Set basic auth (client_id and client_secret)
|
||||
req.SetBasicAuth(cfg.ClientID, cfg.ClientSecret)
|
||||
// Execute request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
// Execute request with rate limit handling
|
||||
resp, err := apiClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to execute request")
|
||||
}
|
||||
@@ -96,13 +98,16 @@ func AuthorizeWithCode(cfg *Config, code, trustedHost string) (*Token, error) {
|
||||
return &tokenResp, nil
|
||||
}
|
||||
|
||||
func RefreshToken(cfg *Config, token *Token) (*Token, error) {
|
||||
func RefreshToken(cfg *Config, token *Token, apiClient *APIClient) (*Token, error) {
|
||||
if token == nil {
|
||||
return nil, errors.New("token cannot be nil")
|
||||
}
|
||||
if cfg == nil {
|
||||
return nil, errors.New("config cannot be nil")
|
||||
}
|
||||
if apiClient == nil {
|
||||
return nil, errors.New("apiClient cannot be nil")
|
||||
}
|
||||
// Prepare form data
|
||||
data := url.Values{}
|
||||
data.Set("grant_type", "refresh_token")
|
||||
@@ -121,9 +126,8 @@ func RefreshToken(cfg *Config, token *Token) (*Token, error) {
|
||||
|
||||
// Set basic auth (client_id and client_secret)
|
||||
req.SetBasicAuth(cfg.ClientID, cfg.ClientSecret)
|
||||
// Execute request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
// Execute request with rate limit handling
|
||||
resp, err := apiClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to execute request")
|
||||
}
|
||||
@@ -145,13 +149,16 @@ func RefreshToken(cfg *Config, token *Token) (*Token, error) {
|
||||
return &tokenResp, nil
|
||||
}
|
||||
|
||||
func RevokeToken(cfg *Config, token *Token) error {
|
||||
func RevokeToken(cfg *Config, token *Token, apiClient *APIClient) error {
|
||||
if token == nil {
|
||||
return errors.New("token cannot be nil")
|
||||
}
|
||||
if cfg == nil {
|
||||
return errors.New("config cannot be nil")
|
||||
}
|
||||
if apiClient == nil {
|
||||
return errors.New("apiClient cannot be nil")
|
||||
}
|
||||
// Prepare form data
|
||||
data := url.Values{}
|
||||
data.Set("token", token.AccessToken)
|
||||
@@ -170,9 +177,8 @@ func RevokeToken(cfg *Config, token *Token) error {
|
||||
|
||||
// Set basic auth (client_id and client_secret)
|
||||
req.SetBasicAuth(cfg.ClientID, cfg.ClientSecret)
|
||||
// Execute request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
// Execute request with rate limit handling
|
||||
resp, err := apiClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to execute request")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user