2024-03-03 18:34:26 -05:00
|
|
|
|
package users
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-03-03 18:34:26 -05:00
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"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 don’t 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 user’s 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)
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
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()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
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
|
2024-03-07 19:41:05 -05:00
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
2024-03-03 18:34:26 -05:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|