...

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

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

     1  // Copyright 2017 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  // ListBlockedUsers lists all the blocked users by the authenticated user.
    14  //
    15  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-users-blocked-by-the-authenticated-user
    16  func (s *UsersService) ListBlockedUsers(ctx context.Context, opts *ListOptions) ([]*User, *Response, error) {
    17  	u := "user/blocks"
    18  	u, err := addOptions(u, opts)
    19  	if err != nil {
    20  		return nil, nil, err
    21  	}
    22  
    23  	req, err := s.client.NewRequest("GET", u, nil)
    24  	if err != nil {
    25  		return nil, nil, err
    26  	}
    27  
    28  	// TODO: remove custom Accept header when this API fully launches.
    29  	req.Header.Set("Accept", mediaTypeBlockUsersPreview)
    30  
    31  	var blockedUsers []*User
    32  	resp, err := s.client.Do(ctx, req, &blockedUsers)
    33  	if err != nil {
    34  		return nil, resp, err
    35  	}
    36  
    37  	return blockedUsers, resp, nil
    38  }
    39  
    40  // IsBlocked reports whether specified user is blocked by the authenticated user.
    41  //
    42  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#check-if-a-user-is-blocked-by-the-authenticated-user
    43  func (s *UsersService) IsBlocked(ctx context.Context, user string) (bool, *Response, error) {
    44  	u := fmt.Sprintf("user/blocks/%v", user)
    45  
    46  	req, err := s.client.NewRequest("GET", u, nil)
    47  	if err != nil {
    48  		return false, nil, err
    49  	}
    50  
    51  	// TODO: remove custom Accept header when this API fully launches.
    52  	req.Header.Set("Accept", mediaTypeBlockUsersPreview)
    53  
    54  	resp, err := s.client.Do(ctx, req, nil)
    55  	isBlocked, err := parseBoolResponse(err)
    56  	return isBlocked, resp, err
    57  }
    58  
    59  // BlockUser blocks specified user for the authenticated user.
    60  //
    61  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#block-a-user
    62  func (s *UsersService) BlockUser(ctx context.Context, user string) (*Response, error) {
    63  	u := fmt.Sprintf("user/blocks/%v", user)
    64  
    65  	req, err := s.client.NewRequest("PUT", u, nil)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	// TODO: remove custom Accept header when this API fully launches.
    71  	req.Header.Set("Accept", mediaTypeBlockUsersPreview)
    72  
    73  	return s.client.Do(ctx, req, nil)
    74  }
    75  
    76  // UnblockUser unblocks specified user for the authenticated user.
    77  //
    78  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#unblock-a-user
    79  func (s *UsersService) UnblockUser(ctx context.Context, user string) (*Response, error) {
    80  	u := fmt.Sprintf("user/blocks/%v", user)
    81  
    82  	req, err := s.client.NewRequest("DELETE", u, nil)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	// TODO: remove custom Accept header when this API fully launches.
    88  	req.Header.Set("Accept", mediaTypeBlockUsersPreview)
    89  
    90  	return s.client.Do(ctx, req, nil)
    91  }
    92  

View as plain text