go-twitch/api/users/get_users.go

66 lines
2.0 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
)
type GetUsersParams struct {
// The ID of the user to get.
// To specify more than one user, include the id parameter for each user to get. For example, id=1234&id=5678.
// The maximum number of IDs you may specify is 100.
IDs []string `url:"id,omitempty"`
// The login name of the user to get.
// To specify more than one user, include the login parameter for each user to get. For example, login=foo&login=bar.
// The maximum number of login names you may specify is 100.
Logins []string `url:"login,omitempty"`
}
type GetUsersResponse struct {
// The list of users.
Data []User `json:"data"`
}
// Gets information about one or more users.
//
// You may look up users using their user ID, login name, or both but the sum total of the number of users you may look up is 100.
// For example, you may specify 50 IDs and 50 names or 100 IDs or names, but you cannot specify 100 IDs and 100 names.
//
// If you dont specify IDs or login names, the request returns information about the user in the access token if you specify a user access token.
//
// To include the users verified email address in the response, you must use a user access token that includes the user:read:email scope.
//
// Requires an app access token or user access token.
func (u *Users) GetUsers(ctx context.Context, params *GetUsersParams) (*GetUsersResponse, error) {
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(u.baseUrl, "users", 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 users (%d)", res.StatusCode)
2024-03-07 18:58:42 -05:00
}
2024-03-03 18:34:26 -05:00
var data GetUsersResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
2024-03-03 18:34:26 -05:00
return nil, err
}
return &data, nil
}