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

72 lines
2.1 KiB
Go
Raw Permalink 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 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 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 (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
}