From b0d91b2b9e1ff8ddc6aad21af7f51c398d7f7a47 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Thu, 10 Sep 2020 12:34:32 +0000 Subject: [PATCH] DeleteToken Accept Names too (#394) DeleteAccessToken: accept string too Co-authored-by: lafriks Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/394 Reviewed-by: Andrew Thornton Reviewed-by: lafriks --- gitea/user_app.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/gitea/user_app.go b/gitea/user_app.go index 2205371..e45df4b 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -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 }