init commit
This commit is contained in:
parent
2b1076c830
commit
5aab4fe1f6
5 changed files with 186 additions and 3 deletions
10
README.md
10
README.md
|
@ -1,4 +1,8 @@
|
|||
go-gogs-client
|
||||
==============
|
||||
Gogs API client in Go
|
||||
=====================
|
||||
|
||||
Gogs API client in Go.
|
||||
This package is still in experiment.
|
||||
|
||||
## License
|
||||
|
||||
This project is under the MIT License. See the [LICENSE](https://github.com/gogits/gogs/blob/master/LICENSE) file for the full license text.
|
78
gogs.go
Normal file
78
gogs.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
// 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 (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Version() string {
|
||||
return "0.0.1"
|
||||
}
|
||||
|
||||
// Client represents a Gogs API client.
|
||||
type Client struct {
|
||||
url string
|
||||
accessToken string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// NewClient initializes and returns a API client.
|
||||
func NewClient(url, token string) *Client {
|
||||
return &Client{
|
||||
url: strings.TrimSuffix(url, "/"),
|
||||
accessToken: token,
|
||||
client: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) {
|
||||
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "token "+c.accessToken)
|
||||
for k, v := range header {
|
||||
req.Header[k] = v
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode == 404 {
|
||||
return nil, errors.New("page not found")
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 && resp.StatusCode != 201 {
|
||||
errMap := make(map[string]interface{})
|
||||
if err = json.Unmarshal(data, &errMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New(errMap["message"].(string))
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (c *Client) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) error {
|
||||
data, err := c.getResponse(method, path, header, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, obj)
|
||||
}
|
32
repo.go
Normal file
32
repo.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
// 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
|
||||
|
||||
// Permission represents a API permission.
|
||||
type Permission struct {
|
||||
Admin bool `json:"admin"`
|
||||
Push bool `json:"push"`
|
||||
Pull bool `json:"pull"`
|
||||
}
|
||||
|
||||
// Repository represents a API repository.
|
||||
type Repository struct {
|
||||
Id int64 `json:"id"`
|
||||
Owner User `json:"owner"`
|
||||
FullName string `json:"full_name"`
|
||||
Private bool `json:"private"`
|
||||
Fork bool `json:"fork"`
|
||||
HtmlUrl string `json:"html_url"`
|
||||
CloneUrl string `json:"clone_url"`
|
||||
SshUrl string `json:"ssh_url"`
|
||||
Permissions Permission `json:"permissions"`
|
||||
}
|
||||
|
||||
// ListMyRepos lists all repositories for the authenticated user that has access to.
|
||||
func (c *Client) ListMyRepos() ([]*Repository, error) {
|
||||
repos := make([]*Repository, 0, 10)
|
||||
err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
|
||||
return repos, err
|
||||
}
|
57
repo_hooks.go
Normal file
57
repo_hooks.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
// 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/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Hook struct {
|
||||
Id int64 `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Events []string `json:"events"`
|
||||
Active bool `json:"active"`
|
||||
Config map[string]string `json:"config"`
|
||||
}
|
||||
|
||||
func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
|
||||
hooks := make([]*Hook, 0, 10)
|
||||
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
|
||||
return hooks, err
|
||||
}
|
||||
|
||||
type CreateHookOption struct {
|
||||
Type string `json:"type"`
|
||||
Config map[string]string `json:"config"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) error {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return 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
|
||||
}
|
||||
|
||||
type EditHookOption struct {
|
||||
Config map[string]string `json:"config"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id),
|
||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
|
||||
return err
|
||||
}
|
12
user.go
Normal file
12
user.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
// 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
|
||||
|
||||
// User represents a API user.
|
||||
type User struct {
|
||||
Id int64 `json:"id"`
|
||||
UserName string `json:"username"`
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
}
|
Loading…
Reference in a new issue