go-twitch/api/users/update_user_extensions.go

73 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package users
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type UpdateUserExtensionsRequest struct {
// The extensions to update. The data field is a dictionary of extension types.
// The dictionarys possible keys are: panel, overlay, or component.
// The keys value is a dictionary of extensions.
//
// For the extensions dictionary, the key is a sequential number beginning with 1.
// For panel and overlay extensions, the keys value is an object that contains the following fields:
// active (true/false), id (the extensions ID), and version (the extensions version).
//
// For component extensions, the keys 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 extensions information. You can update the extensions activation state, ID, and version number.
// The user ID in the access token identifies the broadcaster whose extensions youre 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) {
endpoint := u.baseUrl.ResolveReference(&url.URL{Path: "users/extensions"})
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.String(), 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
}