From 3b1d86c3a838f83c7cc5d0e84c07706e57eaef26 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 18 Nov 2014 11:06:47 -0500 Subject: [PATCH] GetUserInfo, CreateAccessToken, ListAccessTokens --- repo_hooks.go | 11 ++++++----- user.go | 12 ++++++++++++ user_app.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 user_app.go diff --git a/repo_hooks.go b/repo_hooks.go index 2ab444e..93368cc 100644 --- a/repo_hooks.go +++ b/repo_hooks.go @@ -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 { diff --git a/user.go b/user.go index 92981bc..3991685 100644 --- a/user.go +++ b/user.go @@ -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 +} diff --git a/user_app.go b/user_app.go new file mode 100644 index 0000000..152492b --- /dev/null +++ b/user_app.go @@ -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) +}