|
| 1 | +package helix |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +// SegmentType A segment configuration type |
| 6 | +type ExtensionSegmentType string |
| 7 | + |
| 8 | +// Types of segments datastores for the configuration service |
| 9 | +const ( |
| 10 | + ExtensionConfigrationBroadcasterSegment ExtensionSegmentType = "broadcaster" |
| 11 | + ExtensionConfigurationDeveloperSegment ExtensionSegmentType = "developer" |
| 12 | + ExtensionConfigurationGlobalSegment ExtensionSegmentType = "global" |
| 13 | +) |
| 14 | + |
| 15 | +func (s ExtensionSegmentType) String() string { |
| 16 | + return string(s) |
| 17 | +} |
| 18 | + |
| 19 | +type ExtensionSetConfigurationParams struct { |
| 20 | + Segment ExtensionSegmentType `json:"segment"` |
| 21 | + ExtensionID string `json:"extension-id"` |
| 22 | + BroadcasterID string `json:"broadcaster_id,omitempty"` // populated if segment is of type 'developer' || 'broadcaster' |
| 23 | + Version string `json:"version"` |
| 24 | + Content string `json:"content"` |
| 25 | +} |
| 26 | + |
| 27 | +type ExtensionConfigurationSegment struct { |
| 28 | + Segment ExtensionSegmentType `json:"segment"` |
| 29 | + Version string `json:"version"` |
| 30 | + Content string `json:"content"` |
| 31 | +} |
| 32 | + |
| 33 | +type ExtensionGetConfigurationParams struct { |
| 34 | + ExtensionID string `query:"extension_id"` |
| 35 | + BroadcasterID string `query:"broadcaster_id"` |
| 36 | + Segments []ExtensionSegmentType `query:"segment"` |
| 37 | +} |
| 38 | + |
| 39 | +type ExtensionSetRequiredConfigurationParams struct { |
| 40 | + BroadcasterID string `query:"broadcaster_id" json:"-"` |
| 41 | + ExtensionID string `json:"extension_id"` |
| 42 | + RequiredConfiguration string `json:"required_version"` |
| 43 | + ExtensionVersion string `json:"extension_version"` |
| 44 | + ConfigurationVersion string `json:"configuration_version"` |
| 45 | +} |
| 46 | + |
| 47 | +type ExtensionSetRequiredConfigurationResponse struct { |
| 48 | + ResponseCommon |
| 49 | +} |
| 50 | + |
| 51 | +type ExtensionGetConfigurationSegmentResponse struct { |
| 52 | + ResponseCommon |
| 53 | + Data ManyExtensionConfigurationSegments |
| 54 | +} |
| 55 | + |
| 56 | +type ManyExtensionConfigurationSegments struct { |
| 57 | + Segments []ExtensionConfigurationSegment `json:"data"` |
| 58 | +} |
| 59 | + |
| 60 | +type ExtensionSetConfigurationResponse struct { |
| 61 | + ResponseCommon |
| 62 | +} |
| 63 | + |
| 64 | +// https://dev.twitch.tv/docs/extensions/reference/#set-extension-configuration-segment |
| 65 | +func (c *Client) SetExtensionSegmentConfig(params *ExtensionSetConfigurationParams) (*ExtensionSetConfigurationResponse, error) { |
| 66 | + if params.BroadcasterID != "" { |
| 67 | + switch params.Segment { |
| 68 | + case ExtensionConfigurationDeveloperSegment, ExtensionConfigrationBroadcasterSegment: |
| 69 | + default: |
| 70 | + return nil, fmt.Errorf("error: developer or broadcaster extension configuration segment type must be provided for broadcasters") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + resp, err := c.putAsJSON("/extensions/configurations", &ManyPolls{}, params) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + setExtCnfgResp := &ExtensionSetConfigurationResponse{} |
| 80 | + resp.HydrateResponseCommon(&setExtCnfgResp.ResponseCommon) |
| 81 | + |
| 82 | + return setExtCnfgResp, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (c *Client) GetExtensionConfigurationSegment(params *ExtensionGetConfigurationParams) (*ExtensionGetConfigurationSegmentResponse, error) { |
| 86 | + |
| 87 | + if params.BroadcasterID != "" { |
| 88 | + for _, segment := range params.Segments { |
| 89 | + switch segment { |
| 90 | + case ExtensionConfigurationDeveloperSegment, ExtensionConfigrationBroadcasterSegment: |
| 91 | + default: |
| 92 | + return nil, fmt.Errorf("error: only developer or broadcaster extension configuration segment type must be provided for broadcasters") |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + resp, err := c.get("/extensions/configurations", &ManyExtensionConfigurationSegments{}, params) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + extCfgSegResp := &ExtensionGetConfigurationSegmentResponse{} |
| 103 | + resp.HydrateResponseCommon(&extCfgSegResp.ResponseCommon) |
| 104 | + extCfgSegResp.Data.Segments = resp.Data.(*ManyExtensionConfigurationSegments).Segments |
| 105 | + |
| 106 | + return extCfgSegResp, nil |
| 107 | +} |
| 108 | + |
| 109 | +func (c *Client) SetExtensionRequiredConfiguration(params *ExtensionSetRequiredConfigurationParams) (*ExtensionSetRequiredConfigurationResponse, error) { |
| 110 | + |
| 111 | + resp, err := c.putAsJSON("/extensions/configurations/required_configuration", &ExtensionSetRequiredConfigurationResponse{}, params) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + extReqCfgResp := &ExtensionSetRequiredConfigurationResponse{} |
| 117 | + resp.HydrateResponseCommon(&extReqCfgResp.ResponseCommon) |
| 118 | + |
| 119 | + return extReqCfgResp, nil |
| 120 | +} |
0 commit comments