user_key.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package gitote
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. )
  8. type PublicKey struct {
  9. ID int64 `json:"id"`
  10. Key string `json:"key"`
  11. URL string `json:"url,omitempty"`
  12. Title string `json:"title,omitempty"`
  13. Created time.Time `json:"created_at,omitempty"`
  14. }
  15. func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) {
  16. keys := make([]*PublicKey, 0, 10)
  17. return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys)
  18. }
  19. func (c *Client) ListMyPublicKeys() ([]*PublicKey, error) {
  20. keys := make([]*PublicKey, 0, 10)
  21. return keys, c.getParsedResponse("GET", "/user/keys", nil, nil, &keys)
  22. }
  23. func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) {
  24. key := new(PublicKey)
  25. return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key)
  26. }
  27. func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
  28. body, err := json.Marshal(&opt)
  29. if err != nil {
  30. return nil, err
  31. }
  32. key := new(PublicKey)
  33. return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
  34. }
  35. func (c *Client) DeletePublicKey(keyID int64) error {
  36. _, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil)
  37. return err
  38. }