-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (187 loc) · 5.07 KB
/
index.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const fetch = require('node-fetch')
// Maps server keys to client keys; whitelists attributes
const STREAM_DATA_MAP = {
'id': 'id',
'source': 'source',
'platform': 'platform',
'link': 'link',
'status': 'status',
'title': 'title',
'isPinned': 'isPinned',
'isExpired': 'isExpired',
'checkedAt': 'checkedAt',
'liveAt': 'liveAt',
'embedLink': 'embedLink',
'postedBy': 'postedBy',
'city': 'city',
'region': 'region',
'createdAt': 'createdAt',
'updatedAt': 'updatedAt',
}
class Client {
#DEFAULT_BASE_URI = "https://streams.streamwall.io"
constructor(jwt, base_uri) {
if (!jwt) {
throw new Error("You must pass an authentication token")
}
this.jwt = jwt
this.base_uri = base_uri || this.#DEFAULT_BASE_URI
}
async getStreamData(id) {
if (!id) {
throw new Error("getStreamData requires an id")
}
const endpoint = `${this.base_uri}/streams/${id}`
return fetch(endpoint)
.then(response => response.json())
.then(response => {
const data = response.data
return data.map(streamData => {
const mappedStream = {}
for (let [sourceKey, destinationKey] of Object.entries(STREAM_DATA_MAP)) {
stream[destinationKey] = streamData[sourceKey]
}
return mappedStream
})
})
.catch(error => {
console.error(`Could not fetch stream id ${id}: `, endpoint, error)
return []
})
}
async getStreamsData(options) {
const endpoint = `${this.base_uri}/streams`
const url = new URL(endpoint)
const params = new URLSearchParams(options)
url.search = params
return fetch(url)
.then(async response => {
return response.json()
})
.then(async response => {
const data = response.data
const mappedStreams = []
data.forEach(streamData => {
const stream = {}
for (let [sourceKey, destinationKey] of Object.entries(STREAM_DATA_MAP)) {
stream[destinationKey] = streamData[sourceKey]
}
mappedStreams.push(stream)
})
return mappedStreams
})
.catch(error => {
console.log("Could not fetch streams: ", endpoint, error)
return []
})
}
async createStream({ link, city, region, source, postedBy, platform, status }) {
if (!link) {
throw new Error("createStream requires a link")
}
const endpoint = `${this.base_uri}/streams`
const streamData = {
link,
city,
region,
source,
postedBy,
platform,
status
}
const jsonStreamData = JSON.stringify(streamData)
// console.log("Sending stream data: ", jsonStreamData)
return fetch(
endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.jwt}`
},
body: jsonStreamData
})
.then(response => {
return {
status: response.status,
data: response.json()
}
})
.then(({ status, data }) => {
return {
status,
data
}
})
.catch(error => {
console.error("Error creating stream: ", endpoint, streamData, error)
return
})
}
async updateStream({ id, source, platform, link, title, status, city, region, postedBy, checkedAt, liveAt, embedLink }) {
if (!id) {
throw new Error("updateStream requires an id")
}
const endpoint = `${this.base_uri}/streams/${id}`
const streamData = {
source,
platform,
link,
title,
status,
city,
region,
postedBy,
checkedAt,
liveAt,
embedLink
}
const jsonStreamData = JSON.stringify(streamData)
// console.log(`Updating stream id ${id}`, jsonStreamData)
return fetch(
endpoint, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.jwt}`
},
body: jsonStreamData
})
.then(response => {
return response.json()
})
.then(response => {
const data = response.data
// console.log(`Updated stream ${id}`, data)
return data
})
.catch(error => {
console.error(`Error updating stream ${id}`, endpoint, streamData, error)
return false
})
}
async expireStream(streamId) {
if (!streamId) {
throw new Error("Must provide a stream id")
}
const endpoint = `${this.base_uri}/streams/${streamId}`
return fetch(
endpoint, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.jwt}`
},
})
.then(response => {
if (response.status != 204) {
throw new Error("non-204 response", response)
}
return true
})
.catch(error => {
console.error("Error expiring stream: ", endpoint, error)
return false
})
}
}
module.exports.StreamsourceClient = Client