go-twitch/api/extensions/set_extension_configuration...

72 lines
2.2 KiB
Go
Raw Normal View History

2024-03-02 22:45:59 -05:00
package extensions
import (
"context"
"encoding/json"
"fmt"
2024-03-02 22:45:59 -05:00
"io"
"net/http"
"net/url"
)
type SetExtensionConfigurationSegmentRequest struct {
// The ID of the extension to update.
ExtensionID string `json:"extension_id"`
// The configuration segment to update.
Segment ConfigurationSegment `json:"segment"`
// The ID of the broadcaster that installed the extension.
// Include this field only if the segment is set to developer or broadcaster.
BroadcasterID *string `json:"broadcaster_id,omitempty"`
// The contents of the segment.
// This string may be a plain-text string or a string-encoded JSON object.
Content *string `json:"content,omitempty"`
// The version number that identifies this definition of the segments data.
// If not specified, the latest definition is updated.
Version *string `json:"version,omitempty"`
}
// Updates a configuration segment. The segment is limited to 5 KB. Extensions that are active on a channel do not receive the updated configuration.
//
// Rate Limits: You may update the configuration a maximum of 20 times per minute.
//
// Requires a signed JSON Web Token (JWT) created by an EBS. For signing requirements,
// see Signing the JWT: https://dev.twitch.tv/docs/extensions/building/#signing-the-jwt
// The signed JWT must include the role, user_id, and exp fields
// (see JWT Schema: https://dev.twitch.tv/docs/extensions/reference/#jwt-schema).
// The role field must be set to external.
func (c *Extensions) SetExtensionConfigurationSegment(ctx context.Context, body *SetExtensionConfigurationSegmentRequest) error {
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "extensions/configurations"})
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.MethodPut, endpoint.String(), r)
if err != nil {
return err
}
res, err := c.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return fmt.Errorf("failed to set extension configuration segment: %s", res.Status)
}
2024-03-02 22:45:59 -05:00
return nil
}