72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
package users
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
)
|
||
|
||
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()
|
||
}
|
||
}()
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(u.baseUrl, "users/extensions"), r)
|
||
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 update user extensions (%d)", res.StatusCode)
|
||
}
|
||
|
||
var data UpdateUserExtensionsResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|