users_emails.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import "context"
  7. // UserEmail represents user's email address
  8. type UserEmail struct {
  9. Email *string `json:"email,omitempty"`
  10. Primary *bool `json:"primary,omitempty"`
  11. Verified *bool `json:"verified,omitempty"`
  12. }
  13. // ListEmails lists all email addresses for the authenticated user.
  14. //
  15. // GitHub API docs: https://developer.github.com/v3/users/emails/#list-email-addresses-for-a-user
  16. func (s *UsersService) ListEmails(ctx context.Context, opt *ListOptions) ([]*UserEmail, *Response, error) {
  17. u := "user/emails"
  18. u, err := addOptions(u, opt)
  19. if err != nil {
  20. return nil, nil, err
  21. }
  22. req, err := s.client.NewRequest("GET", u, nil)
  23. if err != nil {
  24. return nil, nil, err
  25. }
  26. var emails []*UserEmail
  27. resp, err := s.client.Do(ctx, req, &emails)
  28. if err != nil {
  29. return nil, resp, err
  30. }
  31. return emails, resp, nil
  32. }
  33. // AddEmails adds email addresses of the authenticated user.
  34. //
  35. // GitHub API docs: https://developer.github.com/v3/users/emails/#add-email-addresses
  36. func (s *UsersService) AddEmails(ctx context.Context, emails []string) ([]*UserEmail, *Response, error) {
  37. u := "user/emails"
  38. req, err := s.client.NewRequest("POST", u, emails)
  39. if err != nil {
  40. return nil, nil, err
  41. }
  42. var e []*UserEmail
  43. resp, err := s.client.Do(ctx, req, &e)
  44. if err != nil {
  45. return nil, resp, err
  46. }
  47. return e, resp, nil
  48. }
  49. // DeleteEmails deletes email addresses from authenticated user.
  50. //
  51. // GitHub API docs: https://developer.github.com/v3/users/emails/#delete-email-addresses
  52. func (s *UsersService) DeleteEmails(ctx context.Context, emails []string) (*Response, error) {
  53. u := "user/emails"
  54. req, err := s.client.NewRequest("DELETE", u, emails)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return s.client.Do(ctx, req, nil)
  59. }