GetUserInfo, CreateAccessToken, ListAccessTokens

This commit is contained in:
Unknwon 2014-11-18 11:06:47 -05:00
parent a5c3262f5e
commit 3b1d86c3a8
3 changed files with 64 additions and 5 deletions

View File

@ -37,14 +37,15 @@ type CreateHookOption struct {
Active bool `json:"active"`
}
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) error {
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
body, err := json.Marshal(&opt)
if err != nil {
return err
return nil, err
}
_, err = c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
return err
h := new(Hook)
err = c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
return h, err
}
type EditHookOption struct {

12
user.go
View File

@ -4,9 +4,21 @@
package gogs
import (
"fmt"
)
// User represents a API user.
type User struct {
Id int64 `json:"id"`
UserName string `json:"username"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarUrl string `json:"avatar_url"`
}
func (c *Client) GetUserInfo(user string) (*User, error) {
u := new(User)
err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
return u, err
}

46
user_app.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gogs
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
)
func BasicAuthEncode(user, pass string) string {
return base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
}
// AccessToken represents a API access token.
type AccessToken struct {
Name string `json:"name"`
Sha1 string `json:"sha1"`
}
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)
}
type CreateAccessTokenOption struct {
Name string `json:"name"`
}
func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOption) (*AccessToken, error) {
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)
}