DeleteToken Accept Names too (#394)

DeleteAccessToken: accept string too

Co-authored-by: lafriks <lafriks@noreply.gitea.io>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/394
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
This commit is contained in:
6543 2020-09-10 12:34:32 +00:00
parent dd1ecd4b3d
commit b0d91b2b9e

View file

@ -9,6 +9,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"
)
// AccessToken represents an API access token.
@ -52,11 +53,26 @@ func (c *Client) CreateAccessToken(opt CreateAccessTokenOption) (*AccessToken, e
return t, c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", c.username), jsonHeader, bytes.NewReader(body), t)
}
// DeleteAccessToken delete token with key id
func (c *Client) DeleteAccessToken(keyID int64) error {
// DeleteAccessToken delete token, identified by ID and if not available by name
func (c *Client) DeleteAccessToken(value interface{}) error {
if len(c.username) == 0 {
return fmt.Errorf("\"username\" not set: only BasicAuth allowed")
}
_, err := c.getResponse("DELETE", fmt.Sprintf("/users/%s/tokens/%d", c.username, keyID), jsonHeader, nil)
var token = ""
switch reflect.ValueOf(value).Kind() {
case reflect.Int64:
token = fmt.Sprintf("%d", value.(int64))
case reflect.String:
if err := c.CheckServerVersionConstraint(">= 1.13.0"); err != nil {
return err
}
token = value.(string)
default:
return fmt.Errorf("only string and int64 supported")
}
_, err := c.getResponse("DELETE", fmt.Sprintf("/users/%s/tokens/%s", c.username, token), jsonHeader, nil)
return err
}