mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-19 10:12:54 +02:00
Migrate gitea-sdk to v0.12.0 (#133)
Migrate Update code.gitea.io/sdk/gitea to v0.12.0. Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/133 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
44
vendor/code.gitea.io/sdk/gitea/user_app.go
generated
vendored
44
vendor/code.gitea.io/sdk/gitea/user_app.go
generated
vendored
@ -7,17 +7,10 @@ package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// BasicAuthEncode generate base64 of basic auth head
|
||||
func BasicAuthEncode(user, pass string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
|
||||
}
|
||||
|
||||
// AccessToken represents an API access token.
|
||||
type AccessToken struct {
|
||||
ID int64 `json:"id"`
|
||||
@ -26,11 +19,19 @@ type AccessToken struct {
|
||||
TokenLastEight string `json:"token_last_eight"`
|
||||
}
|
||||
|
||||
// ListAccessTokens lista all the access tokens of user
|
||||
func (c *Client) ListAccessTokens(user, pass string) ([]*AccessToken, error) {
|
||||
tokens := make([]*AccessToken, 0, 10)
|
||||
return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens", user),
|
||||
http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, nil, &tokens)
|
||||
// ListAccessTokensOptions options for listing a users's access tokens
|
||||
type ListAccessTokensOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
// ListAccessTokens lists all the access tokens of user
|
||||
func (c *Client) ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
|
||||
if len(c.username) == 0 {
|
||||
return nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
}
|
||||
opts.setDefaults()
|
||||
tokens := make([]*AccessToken, 0, opts.PageSize)
|
||||
return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", c.username, opts.getURLQuery().Encode()), jsonHeader, nil, &tokens)
|
||||
}
|
||||
|
||||
// CreateAccessTokenOption options when create access token
|
||||
@ -39,22 +40,23 @@ type CreateAccessTokenOption struct {
|
||||
}
|
||||
|
||||
// CreateAccessToken create one access token with options
|
||||
func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOption) (*AccessToken, error) {
|
||||
func (c *Client) CreateAccessToken(opt CreateAccessTokenOption) (*AccessToken, error) {
|
||||
if len(c.username) == 0 {
|
||||
return nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t := new(AccessToken)
|
||||
return t, c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", user),
|
||||
http.Header{
|
||||
"content-type": []string{"application/json"},
|
||||
"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}},
|
||||
bytes.NewReader(body), t)
|
||||
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(user string, keyID int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/users/%s/tokens/%d", user, keyID),
|
||||
http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, c.password)}}, nil)
|
||||
func (c *Client) DeleteAccessToken(keyID int64) 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)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user