72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package extensions
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
)
|
||
|
||
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 segment’s 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 (e *Extensions) SetExtensionConfigurationSegment(ctx context.Context, body *SetExtensionConfigurationSegmentRequest) 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.MethodPut, endpoint.Make(e.baseUrl, "extensions/configurations"), r)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
res, err := e.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)
|
||
}
|
||
|
||
return nil
|
||
}
|