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
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
|
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-03-03 18:34:26 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UpdateUserExtensionsRequest struct {
|
|
|
|
|
// The extensions to update. The data field is a dictionary of extension types.
|
|
|
|
|
// The dictionary’s possible keys are: panel, overlay, or component.
|
|
|
|
|
// The key’s value is a dictionary of extensions.
|
|
|
|
|
//
|
|
|
|
|
// For the extension’s dictionary, the key is a sequential number beginning with 1.
|
|
|
|
|
// For panel and overlay extensions, the key’s value is an object that contains the following fields:
|
|
|
|
|
// active (true/false), id (the extension’s ID), and version (the extension’s version).
|
|
|
|
|
//
|
|
|
|
|
// For component extensions, the key’s value includes the above fields plus the x and y fields,
|
|
|
|
|
// which identify the coordinate where the extension is placed.
|
|
|
|
|
Data map[string]map[string]interface{} `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UpdateUserExtensionsResponse struct {
|
|
|
|
|
// The extensions that the broadcaster updated.
|
|
|
|
|
Data []ActiveExtension `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Updates an installed extension’s information. You can update the extension’s activation state, ID, and version number.
|
|
|
|
|
// The user ID in the access token identifies the broadcaster whose extensions you’re updating.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: If you try to activate an extension under multiple extension types, the last write wins (and there is no guarantee of write order).
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the user:edit:broadcast scope.
|
|
|
|
|
func (u *Users) UpdateUserExtensions(ctx context.Context, body *UpdateUserExtensionsRequest) (*UpdateUserExtensionsResponse, error) {
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
if err := json.NewEncoder(w).Encode(body); err != nil {
|
|
|
|
|
w.CloseWithError(err)
|
|
|
|
|
} else {
|
|
|
|
|
w.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(u.baseUrl, "users/extensions"), r)
|
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 update user extensions (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 18:34:26 -05:00
|
|
|
|
var data UpdateUserExtensionsResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|