From 4663f3ae455f7a6d2c31b360d82831bd53b40b0f Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 3 Dec 2015 00:16:21 -0500 Subject: [PATCH] public key --- repo_keys.go | 9 +++++++-- user_keys.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 user_keys.go diff --git a/repo_keys.go b/repo_keys.go index 0246476..dfd15c9 100644 --- a/repo_keys.go +++ b/repo_keys.go @@ -26,12 +26,17 @@ func (c *Client) ListDeployKeys(user, repo string) ([]*DeployKey, error) { return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys", user, repo), nil, nil, &keys) } -type CreateDeployKeyOption struct { +func (c *Client) GetDeployKey(user, repo string, keyID int64) (*DeployKey, error) { + key := new(DeployKey) + return key, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys/%d", user, repo, keyID), nil, nil, &key) +} + +type CreateKeyOption struct { Title string `json:"title" binding:"Required"` Key string `json:"key" binding:"Required"` } -func (c *Client) CreateDeployKey(user, repo string, opt CreateDeployKeyOption) (*DeployKey, error) { +func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err diff --git a/user_keys.go b/user_keys.go new file mode 100644 index 0000000..cb21757 --- /dev/null +++ b/user_keys.go @@ -0,0 +1,51 @@ +// Copyright 2015 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" + "time" +) + +type PublicKey struct { + ID int64 `json:"id"` + Key string `json:"key"` + URL string `json:"url,omitempty"` + Title string `json:"title,omitempty"` + Created time.Time `json:"created_at,omitempty"` +} + +func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) { + keys := make([]*PublicKey, 0, 10) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys) +} + +func (c *Client) ListMyPublicKeys(user string) ([]*PublicKey, error) { + keys := make([]*PublicKey, 0, 10) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys", user), nil, nil, &keys) +} + +func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) { + key := new(PublicKey) + return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key) +} + +func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + key := new(PublicKey) + return key, c.getParsedResponse("POST", "/user/keys", + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) +} + +func (c *Client) DeletePublicKey(keyID int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil) + return err +}