go-twitch/api/users/get_user_block_list.go

73 lines
2.1 KiB
Go
Raw Permalink Normal View History

2024-03-03 18:34:26 -05:00
package users
import (
"context"
"encoding/json"
"fmt"
2024-03-03 18:34:26 -05:00
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 18:34:26 -05:00
"go.fifitido.net/twitch/api/types"
)
type GetUserBlockListParams struct {
// The ID of the broadcaster whose list of blocked users you want to get.
BroadcasterID string `url:"broadcaster_id"`
// The maximum number of items to return per page in the response.
// The minimum page size is 1 item per page and the maximum is 100.
// The default is 20.
First *int `url:"first,omitempty"`
// The cursor used to get the next page of results.
// The Pagination object in the response contains the cursors value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
After *types.Cursor `url:"after,omitempty"`
}
type GetUserBlockListResponse struct {
// The list of blocked users. The list is in descending order by when the user was blocked.
Data []struct {
// An ID that identifies the blocked user.
UserID string `json:"user_id"`
// The blocked users login name.
UserLogin string `json:"user_login"`
// The blocked users display name.
DisplayName string `json:"display_name"`
} `json:"data"`
}
// Gets the list of users that the broadcaster has blocked.
// Read More: https://help.twitch.tv/s/article/how-to-manage-harassment-in-chat?language=en_US#BlockWhispersandMessagesfromStrangers
//
// Requires a user access token that includes the user:read:blocked_users scope.
func (u *Users) GetUserBlockList(ctx context.Context, params *GetUserBlockListParams) (*GetUserBlockListResponse, error) {
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(u.baseUrl, "users/blocks", v), nil)
2024-03-03 18:34:26 -05:00
if err != nil {
return nil, err
}
res, err := u.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get user block list (%d)", res.StatusCode)
}
2024-03-03 18:34:26 -05:00
var data GetUserBlockListResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}