-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket_message.gleam
215 lines (189 loc) · 5.45 KB
/
websocket_message.gleam
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import gleam/dynamic.{type Decoder, type Dynamic}
import gleam/json.{type DecodeError}
import gleam/option.{type Option}
import gleam/result
import gleam/uri.{type Uri}
import glitch/extended/dynamic_ext
import glitch/types/event.{type Event}
import glitch/types/subscription.{type Subscription, type SubscriptionType}
pub type WebSocketMessage {
Close
NotificationMessage(
metadata: SubscriptionMetadata,
payload: NotificationMessagePayload,
)
SessionKeepaliveMessage(metadata: Metadata)
UnhandledMessage(raw_message: String)
WelcomeMessage(metadata: Metadata, payload: WelcomeMessagePayload)
}
pub fn from_json(json_string: String) -> Result(WebSocketMessage, DecodeError) {
json.decode(json_string, decoder())
}
pub fn decoder() -> Decoder(WebSocketMessage) {
dynamic.any([
welcome_message_decoder(),
notification_message_decoder(),
session_keepalive_message_decoder(),
])
}
fn welcome_message_decoder() -> Decoder(WebSocketMessage) {
dynamic.decode2(
WelcomeMessage,
dynamic.field("metadata", metadata_decoder()),
dynamic.field("payload", welcome_message_payload_decoder()),
)
}
// TODO: Figure out how to handle "strict", i.e. not allowing additional filds
fn session_keepalive_message_decoder() -> Decoder(WebSocketMessage) {
dynamic.decode1(
SessionKeepaliveMessage,
dynamic.field("metadata", metadata_decoder()),
)
}
fn notification_message_decoder() -> Decoder(WebSocketMessage) {
dynamic.decode2(
NotificationMessage,
dynamic.field("metadata", subscription_metadata_decoder()),
dynamic.field("payload", notification_message_payload_decoder()),
)
}
pub type MessageType {
Notification
SessionWelcome
SessionKeepalive
SessionReconnect
Revocation
}
pub fn message_type_to_string(message_type: MessageType) -> String {
case message_type {
Notification -> "notification"
SessionWelcome -> "session_welcome"
SessionKeepalive -> "session_keepalive"
SessionReconnect -> "sessions_reconnect"
Revocation -> "revocation"
}
}
pub fn message_type_from_string(string: String) -> Result(MessageType, Nil) {
case string {
"notification" -> Ok(Notification)
"session_welcome" -> Ok(SessionWelcome)
"session_keepalive" -> Ok(SessionKeepalive)
"sessions_reconnect" -> Ok(SessionReconnect)
"revocation" -> Ok(Revocation)
_ -> Error(Nil)
}
}
fn message_type_decoder() -> Decoder(MessageType) {
fn(data: Dynamic) {
use string <- result.try(dynamic.string(data))
string
|> message_type_from_string
|> result.replace_error([
dynamic.DecodeError(
expected: "MessageType",
found: "String(" <> string <> ")",
path: [],
),
])
}
}
pub type Metadata {
Metadata(
message_id: String,
message_type: MessageType,
message_timestamp: String,
)
}
fn metadata_decoder() -> Decoder(Metadata) {
dynamic.decode3(
Metadata,
dynamic.field("message_id", dynamic.string),
dynamic.field("message_type", message_type_decoder()),
dynamic.field("message_timestamp", dynamic.string),
)
}
pub type SubscriptionMetadata {
SubscriptionMetadata(
message_id: String,
message_type: MessageType,
message_timestamp: String,
subscription_type: SubscriptionType,
subscription_version: String,
)
}
fn subscription_metadata_decoder() -> Decoder(SubscriptionMetadata) {
dynamic.decode5(
SubscriptionMetadata,
dynamic.field("message_id", dynamic.string),
dynamic.field("message_type", message_type_decoder()),
dynamic.field("message_timestamp", dynamic.string),
dynamic.field("subscription_type", subscription.subscription_type_decoder()),
dynamic.field("subscription_version", dynamic.string),
)
}
pub type SessionStatus {
Connected
}
pub fn session_status_to_string(session_status: SessionStatus) -> String {
case session_status {
Connected -> "connnected"
}
}
pub fn session_status_from_string(string: String) -> Result(SessionStatus, Nil) {
case string {
"connected" -> Ok(Connected)
_ -> Error(Nil)
}
}
fn session_status_decoder() -> Decoder(SessionStatus) {
fn(data: Dynamic) {
use string <- result.try(dynamic.string(data))
string
|> session_status_from_string
|> result.replace_error([
dynamic.DecodeError(
expected: "SessionStatus",
found: "String(" <> string <> ")",
path: [],
),
])
}
}
pub type Session {
Session(
id: String,
status: SessionStatus,
connected_at: String,
keepalive_timeout_seconds: Int,
reconnect_url: Option(Uri),
)
}
fn session_decoder() -> Decoder(Session) {
dynamic.decode5(
Session,
dynamic.field("id", dynamic.string),
dynamic.field("status", session_status_decoder()),
dynamic.field("connected_at", dynamic.string),
dynamic.field("keepalive_timeout_seconds", dynamic.int),
dynamic.field("reconnect_url", dynamic.optional(dynamic_ext.uri)),
)
}
pub type WelcomeMessagePayload {
WelcomeMessagePayload(session: Session)
}
fn welcome_message_payload_decoder() -> Decoder(WelcomeMessagePayload) {
dynamic.decode1(
WelcomeMessagePayload,
dynamic.field("session", session_decoder()),
)
}
pub type NotificationMessagePayload {
NotificationMessagePayload(subscription: Subscription, event: Event)
}
fn notification_message_payload_decoder() -> Decoder(NotificationMessagePayload) {
dynamic.decode2(
NotificationMessagePayload,
dynamic.field("subscription", subscription.decoder()),
dynamic.field("event", event.decoder()),
)
}