go-twitch/api/extensions/set_extension_required_conf...

68 lines
2.2 KiB
Go
Raw Permalink 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"
"go.fifitido.net/twitch/api/endpoint"
2024-03-02 22:45:59 -05:00
)
type SetExtensionRequiredConfigurationRequest struct {
// The ID of the extension to update.
ExtensionID string `json:"extension_id"`
// The version of the extension to update.
ExtensionVersion string `json:"extension_version"`
// The required_configuration string to use with the extension.
RequiredConfiguration string `json:"required_configuration"`
}
// Updates the extensions required_configuration string.
// Use this endpoint if your extension requires the broadcaster to configure the extension before activating it
// (to require configuration, you must select Custom/My Own Service in Extension Capabilities).
// For more information,
// see Required Configurations: https://dev.twitch.tv/docs/extensions/building#required-configurations
// and Setting Required Configuration: https://dev.twitch.tv/docs/extensions/building#setting-required-configuration-with-the-configuration-service-optional
//
// 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).
// Set the role field to external and the user_id field to the ID of the user that owns the extension.
func (e *Extensions) SetExtensionRequiredConfiguration(ctx context.Context, broadcasterID string, body *SetExtensionRequiredConfigurationRequest) error {
v := url.Values{"broadcaster_id": {broadcasterID}}
2024-03-02 22:45:59 -05:00
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/required_configuration", v), r)
2024-03-02 22:45:59 -05:00
if err != nil {
return err
}
res, err := e.client.Do(req)
2024-03-02 22:45:59 -05:00
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 required configuration (%d)", res.StatusCode)
}
2024-03-02 22:45:59 -05:00
return nil
}