go-twitch/api/extensions/get_extension_configuration...

87 lines
3.1 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
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-03-02 22:45:59 -05:00
)
type GetExtensionConfigurationSegmentParams struct {
// The ID of the broadcaster that installed the extension.
// This parameter is required if you set the segment parameter to broadcaster or developer.
// Do not specify this parameter if you set segment to global.
BroadcasterID *string `url:"broadcaster_id,omitempty"`
// The ID of the extension that contains the configuration segment you want to get.
ExtensionID string `url:"extension_id"`
// The type of configuration segment to get. Possible case-sensitive values are:
//
// broadcaster, developer, global
//
// You may specify one or more segments. To specify multiple segments, include the segment parameter for each segment to get.
// For example, segment=broadcaster&segment=developer. Ignores duplicate segments.
Segment []ConfigurationSegment `url:"segment"`
}
type GetExtensionConfigurationSegmentResponse struct {
// The list of requested configuration segments.
// The list is returned in the same order that you specified the list of segments in the request.
Data []ConfigurationSegmentData `json:"data"`
}
type ConfigurationSegmentData struct {
// The type of segment.
Segment ConfigurationSegment `json:"segment"`
// The ID of the broadcaster that installed the extension.
// The object includes this field only if the segment query parameter 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"`
// The version number that identifies this definition of the segments data.
Version string `json:"version"`
}
// Gets the specified configuration segment from the specified extension.
//
// Rate Limits: You may retrieve each segment 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) GetExtensionConfigurationSegment(ctx context.Context, params *GetExtensionConfigurationSegmentParams) (*GetExtensionConfigurationSegmentResponse, error) {
2024-03-02 22:45:59 -05:00
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(e.baseUrl, "extensions/configurations", v), nil)
2024-03-02 22:45:59 -05:00
if err != nil {
return nil, err
}
res, err := e.client.Do(req)
2024-03-02 22:45:59 -05:00
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 get extension configuration segment (%d)", res.StatusCode)
}
2024-03-02 22:45:59 -05:00
var data GetExtensionConfigurationSegmentResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}