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