...

Source file src/github.com/google/go-github/v33/github/users_followers.go

Documentation: github.com/google/go-github/v33/github

     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  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  )
    12  
    13  // ListFollowers lists the followers for a user. Passing the empty string will
    14  // fetch followers for the authenticated user.
    15  //
    16  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-followers-of-the-authenticated-user
    17  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-followers-of-a-user
    18  func (s *UsersService) ListFollowers(ctx context.Context, user string, opts *ListOptions) ([]*User, *Response, error) {
    19  	var u string
    20  	if user != "" {
    21  		u = fmt.Sprintf("users/%v/followers", user)
    22  	} else {
    23  		u = "user/followers"
    24  	}
    25  	u, err := addOptions(u, opts)
    26  	if err != nil {
    27  		return nil, nil, err
    28  	}
    29  
    30  	req, err := s.client.NewRequest("GET", u, nil)
    31  	if err != nil {
    32  		return nil, nil, err
    33  	}
    34  
    35  	var users []*User
    36  	resp, err := s.client.Do(ctx, req, &users)
    37  	if err != nil {
    38  		return nil, resp, err
    39  	}
    40  
    41  	return users, resp, nil
    42  }
    43  
    44  // ListFollowing lists the people that a user is following. Passing the empty
    45  // string will list people the authenticated user is following.
    46  //
    47  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-the-people-the-authenticated-user-follows
    48  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-the-people-a-user-follows
    49  func (s *UsersService) ListFollowing(ctx context.Context, user string, opts *ListOptions) ([]*User, *Response, error) {
    50  	var u string
    51  	if user != "" {
    52  		u = fmt.Sprintf("users/%v/following", user)
    53  	} else {
    54  		u = "user/following"
    55  	}
    56  	u, err := addOptions(u, opts)
    57  	if err != nil {
    58  		return nil, nil, err
    59  	}
    60  
    61  	req, err := s.client.NewRequest("GET", u, nil)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  
    66  	var users []*User
    67  	resp, err := s.client.Do(ctx, req, &users)
    68  	if err != nil {
    69  		return nil, resp, err
    70  	}
    71  
    72  	return users, resp, nil
    73  }
    74  
    75  // IsFollowing checks if "user" is following "target". Passing the empty
    76  // string for "user" will check if the authenticated user is following "target".
    77  //
    78  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#check-if-a-person-is-followed-by-the-authenticated-user
    79  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#check-if-a-user-follows-another-user
    80  func (s *UsersService) IsFollowing(ctx context.Context, user, target string) (bool, *Response, error) {
    81  	var u string
    82  	if user != "" {
    83  		u = fmt.Sprintf("users/%v/following/%v", user, target)
    84  	} else {
    85  		u = fmt.Sprintf("user/following/%v", target)
    86  	}
    87  
    88  	req, err := s.client.NewRequest("GET", u, nil)
    89  	if err != nil {
    90  		return false, nil, err
    91  	}
    92  
    93  	resp, err := s.client.Do(ctx, req, nil)
    94  	following, err := parseBoolResponse(err)
    95  	return following, resp, err
    96  }
    97  
    98  // Follow will cause the authenticated user to follow the specified user.
    99  //
   100  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#follow-a-user
   101  func (s *UsersService) Follow(ctx context.Context, user string) (*Response, error) {
   102  	u := fmt.Sprintf("user/following/%v", user)
   103  	req, err := s.client.NewRequest("PUT", u, nil)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	return s.client.Do(ctx, req, nil)
   109  }
   110  
   111  // Unfollow will cause the authenticated user to unfollow the specified user.
   112  //
   113  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#unfollow-a-user
   114  func (s *UsersService) Unfollow(ctx context.Context, user string) (*Response, error) {
   115  	u := fmt.Sprintf("user/following/%v", user)
   116  	req, err := s.client.NewRequest("DELETE", u, nil)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	return s.client.Do(ctx, req, nil)
   122  }
   123  

View as plain text