本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.2KB

  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 (
  7. "context"
  8. "fmt"
  9. )
  10. // ListFollowers lists the followers for a user. Passing the empty string will
  11. // fetch followers for the authenticated user.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/users/followers/#list-followers-of-a-user
  14. func (s *UsersService) ListFollowers(ctx context.Context, user string, opt *ListOptions) ([]*User, *Response, error) {
  15. var u string
  16. if user != "" {
  17. u = fmt.Sprintf("users/%v/followers", user)
  18. } else {
  19. u = "user/followers"
  20. }
  21. u, err := addOptions(u, opt)
  22. if err != nil {
  23. return nil, nil, err
  24. }
  25. req, err := s.client.NewRequest("GET", u, nil)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. var users []*User
  30. resp, err := s.client.Do(ctx, req, &users)
  31. if err != nil {
  32. return nil, resp, err
  33. }
  34. return users, resp, nil
  35. }
  36. // ListFollowing lists the people that a user is following. Passing the empty
  37. // string will list people the authenticated user is following.
  38. //
  39. // GitHub API docs: https://developer.github.com/v3/users/followers/#list-users-followed-by-another-user
  40. func (s *UsersService) ListFollowing(ctx context.Context, user string, opt *ListOptions) ([]*User, *Response, error) {
  41. var u string
  42. if user != "" {
  43. u = fmt.Sprintf("users/%v/following", user)
  44. } else {
  45. u = "user/following"
  46. }
  47. u, err := addOptions(u, opt)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. req, err := s.client.NewRequest("GET", u, nil)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. var users []*User
  56. resp, err := s.client.Do(ctx, req, &users)
  57. if err != nil {
  58. return nil, resp, err
  59. }
  60. return users, resp, nil
  61. }
  62. // IsFollowing checks if "user" is following "target". Passing the empty
  63. // string for "user" will check if the authenticated user is following "target".
  64. //
  65. // GitHub API docs: https://developer.github.com/v3/users/followers/#check-if-you-are-following-a-user
  66. func (s *UsersService) IsFollowing(ctx context.Context, user, target string) (bool, *Response, error) {
  67. var u string
  68. if user != "" {
  69. u = fmt.Sprintf("users/%v/following/%v", user, target)
  70. } else {
  71. u = fmt.Sprintf("user/following/%v", target)
  72. }
  73. req, err := s.client.NewRequest("GET", u, nil)
  74. if err != nil {
  75. return false, nil, err
  76. }
  77. resp, err := s.client.Do(ctx, req, nil)
  78. following, err := parseBoolResponse(err)
  79. return following, resp, err
  80. }
  81. // Follow will cause the authenticated user to follow the specified user.
  82. //
  83. // GitHub API docs: https://developer.github.com/v3/users/followers/#follow-a-user
  84. func (s *UsersService) Follow(ctx context.Context, user string) (*Response, error) {
  85. u := fmt.Sprintf("user/following/%v", user)
  86. req, err := s.client.NewRequest("PUT", u, nil)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return s.client.Do(ctx, req, nil)
  91. }
  92. // Unfollow will cause the authenticated user to unfollow the specified user.
  93. //
  94. // GitHub API docs: https://developer.github.com/v3/users/followers/#unfollow-a-user
  95. func (s *UsersService) Unfollow(ctx context.Context, user string) (*Response, error) {
  96. u := fmt.Sprintf("user/following/%v", user)
  97. req, err := s.client.NewRequest("DELETE", u, nil)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return s.client.Do(ctx, req, nil)
  102. }
上海开阖软件有限公司 沪ICP备12045867号-1