keys.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // Copyright 2018, Patrick Webster
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package gitlab
  17. import (
  18. "fmt"
  19. "net/url"
  20. "time"
  21. )
  22. // KeysService handles communication with the
  23. // keys related methods of the GitLab API.
  24. //
  25. // GitLab API docs:
  26. // https://docs.gitlab.com/ee/api/keys.html
  27. type KeysService struct {
  28. client *Client
  29. }
  30. // Key represents a GitLab user's SSH key.
  31. //
  32. // GitLab API docs:
  33. // https://docs.gitlab.com/ee/api/keys.html
  34. type Key struct {
  35. ID int `json:"id"`
  36. Title string `json:"title"`
  37. Key string `json:"key"`
  38. CreatedAt *time.Time `json:"created_at"`
  39. User User `json:"user"`
  40. }
  41. // GetKeyWithUser gets a single key by id along with the associated
  42. // user information.
  43. //
  44. // GitLab API docs:
  45. // https://docs.gitlab.com/ee/api/keys.html#get-ssh-key-with-user-by-id-of-an-ssh-key
  46. func (s *KeysService) GetKeyWithUser(kid interface{}, options ...OptionFunc) (*Key, *Response, error) {
  47. key, err := parseID(kid)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. u := fmt.Sprintf("keys/%s", url.QueryEscape(key))
  52. req, err := s.client.NewRequest("GET", u, nil, options)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. k := new(Key)
  57. resp, err := s.client.Do(req, k)
  58. if err != nil {
  59. return nil, resp, err
  60. }
  61. return k, resp, err
  62. }