go-twitch/api/users/get_user_active_extensions.go

58 lines
1.7 KiB
Go
Raw 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 GetUserActiveExtensionsParams struct {
// The ID of the broadcaster whose active extensions you want to get.
//
// This parameter is required if you specify an app access token and is optional if you specify a user access token.
// If you specify a user access token and dont specify this parameter, the API uses the user ID from the access token.
UserID string `url:"user_id,omitempty"`
}
type GetUserActiveExtensionsResponse struct {
// The active extensions that the broadcaster has installed.
Data []ActiveExtension `json:"data"`
}
// Gets the active extensions that the broadcaster has installed for each configuration.
//
// NOTE: To include extensions that you have under development,
// you must specify a user access token that includes the user:read:broadcast or user:edit:broadcast scope.
//
// Requires an app access token or user access token.
func (u *Users) GetUserActiveExtensions(ctx context.Context, params *GetUserActiveExtensionsParams) (*GetUserActiveExtensionsResponse, error) {
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(u.baseUrl, "users/extensions", 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 active extensions (%d)", res.StatusCode)
}
2024-03-03 18:34:26 -05:00
var data GetUserActiveExtensionsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}