-
Notifications
You must be signed in to change notification settings - Fork 89
/
extensions.go
141 lines (118 loc) · 4.8 KB
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package helix
import "fmt"
type ExtensionTransaction struct {
ID string `json:"id"`
Timestamp Time `json:"timestamp"`
BroadcasterID string `json:"broadcaster_id"`
BroadcasterLogin string `json:"broadcaster_login"`
BroadcasterName string `json:"broadcaster_name"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
ProductType string `json:"product_type"`
ProductData struct {
Domain string `json:"domain"`
Broadcast bool `json:"broadcast"`
Expiration string `json:"expiration"`
SKU string `json:"sku"`
Cost struct {
Amount int `json:"amount"`
Type string `json:"type"`
} `json:"cost"`
DisplayName string `json:"displayName"`
InDevelopment bool `json:"inDevelopment"`
} `json:"product_data"`
}
type ManyExtensionTransactions struct {
ExtensionTransactions []ExtensionTransaction `json:"data"`
Pagination Pagination `json:"pagination"`
}
type ExtensionTransactionsResponse struct {
ResponseCommon
Data ManyExtensionTransactions
}
type ExtensionTransactionsParams struct {
ExtensionID string `query:"extension_id"` // Required
ID []string `query:"id"` // Optional, Limit 100
After string `query:"after"` // Optional
First int `query:"first,20"` // Optional, Limit 100
}
type ExtensionSendChatMessageParams struct {
BroadcasterID string `query:"broadcaster_id" json:"-"`
Text string `json:"text"` // Limit 280
ExtensionVersion string `json:"extension_version"`
ExtensionID string `json:"extension_id"`
}
type ExtensionSendChatMessageResponse struct {
ResponseCommon
}
type ExtensionLiveChannel struct {
BroadcasterID string `json:"broadcaster_id"`
BroadcasterName string `json:"broadcaster_name"`
GameName string `json:"game_name"`
GameID string `json:"game_id"`
Title string `json:"title"`
}
type ManyExtensionLiveChannels struct {
LiveChannels []ExtensionLiveChannel `json:"data"`
Pagination string `json:"pagination"`
}
type ExtensionLiveChannelsParams struct {
ExtensionID string `query:"extension_id"` // Required
After string `query:"after"` // Optional
First int `query:"first,20"` // Optional, Limit 100
}
type ExtensionLiveChannelsResponse struct {
ResponseCommon
Data ManyExtensionLiveChannels
}
// GetExtensionTransactions allows extension back end servers to fetch a list of transactions that
// have occurred for their extension across all of Twitch. A transaction is a record of a user
// exchanging Bits for an in-Extension digital good.
//
// See https://dev.twitch.tv/docs/api/reference/#get-extension-transactions
func (c *Client) GetExtensionTransactions(params *ExtensionTransactionsParams) (*ExtensionTransactionsResponse, error) {
resp, err := c.get("/extensions/transactions", &ManyExtensionTransactions{}, params)
if err != nil {
return nil, err
}
extTxnResp := &ExtensionTransactionsResponse{}
resp.HydrateResponseCommon(&extTxnResp.ResponseCommon)
extTxnResp.Data.ExtensionTransactions = resp.Data.(*ManyExtensionTransactions).ExtensionTransactions
extTxnResp.Data.Pagination = resp.Data.(*ManyExtensionTransactions).Pagination
return extTxnResp, nil
}
// SendExtensionChatMessage Sends a specified chat message to a specified channel.
// The message will appear in the channel’s chat as a normal message,
// The author of the message is the Extension name.
//
// see https://dev.twitch.tv/docs/api/reference#send-extension-chat-message
func (c *Client) SendExtensionChatMessage(params *ExtensionSendChatMessageParams) (*ExtensionSendChatMessageResponse, error) {
if len(params.Text) > 280 {
return nil, fmt.Errorf("error: chat message length exceeds 280 characters")
}
if params.BroadcasterID == "" {
return nil, fmt.Errorf("error: broadcaster ID must be specified")
}
resp, err := c.postAsJSON("/extensions/chat", &ExtensionSendChatMessageResponse{}, params)
if err != nil {
return nil, err
}
sndExtMsgResp := &ExtensionSendChatMessageResponse{}
resp.HydrateResponseCommon(&sndExtMsgResp.ResponseCommon)
return sndExtMsgResp, nil
}
func (c *Client) GetExtensionLiveChannels(params *ExtensionLiveChannelsParams) (*ExtensionLiveChannelsResponse, error) {
if params.ExtensionID == "" {
return nil, fmt.Errorf("error: extension ID must be specified")
}
resp, err := c.get("/extensions/live", &ManyExtensionLiveChannels{}, params)
if err != nil {
return nil, err
}
liveChannels := &ExtensionLiveChannelsResponse{}
resp.HydrateResponseCommon(&liveChannels.ResponseCommon)
liveChannels.Data.LiveChannels = resp.Data.(*ManyExtensionLiveChannels).LiveChannels
liveChannels.Data.Pagination = resp.Data.(*ManyExtensionLiveChannels).Pagination
return liveChannels, nil
}