user_key.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gogs
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. type PublicKey struct {
  12. ID int64 `json:"id"`
  13. Key string `json:"key"`
  14. URL string `json:"url,omitempty"`
  15. Title string `json:"title,omitempty"`
  16. Created time.Time `json:"created_at,omitempty"`
  17. }
  18. func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) {
  19. keys := make([]*PublicKey, 0, 10)
  20. return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys)
  21. }
  22. func (c *Client) ListMyPublicKeys() ([]*PublicKey, error) {
  23. keys := make([]*PublicKey, 0, 10)
  24. return keys, c.getParsedResponse("GET", "/user/keys", nil, nil, &keys)
  25. }
  26. func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) {
  27. key := new(PublicKey)
  28. return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key)
  29. }
  30. func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
  31. body, err := json.Marshal(&opt)
  32. if err != nil {
  33. return nil, err
  34. }
  35. key := new(PublicKey)
  36. return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
  37. }
  38. func (c *Client) DeletePublicKey(keyID int64) error {
  39. _, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil)
  40. return err
  41. }