-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.gleam
92 lines (83 loc) · 2.4 KB
/
transport.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
import gleam/dynamic.{type Decoder}
import gleam/json.{type DecodeError as JsonDecodeError, type Json}
import gleam/option.{type Option}
import gleam/result
import glitch/extended/json_ext
pub type Transport {
Transport(
method: Method,
callback: Option(String),
// WebHook only
secret: Option(String),
// WebSocket only
session_id: Option(String),
// WebSocket only & Response only
connected_at: Option(String),
// WebSocket only & Response only
disconnected_at: Option(String),
// Conduit only & Response only
conduit_id: Option(String),
)
}
pub fn decoder() -> Decoder(Transport) {
dynamic.decode7(
Transport,
dynamic.field("method", method_decoder()),
dynamic.optional_field("callback", dynamic.string),
dynamic.optional_field("secret", dynamic.string),
dynamic.optional_field("session_id", dynamic.string),
dynamic.optional_field("connected_at", dynamic.string),
dynamic.optional_field("disconnected_at", dynamic.string),
dynamic.optional_field("conduit_id", dynamic.string),
)
}
pub fn from_json(json_string: String) -> Result(Transport, JsonDecodeError) {
json.decode(json_string, decoder())
}
pub fn to_json(transport: Transport) -> Json {
json.object([
#("method", json.string(method_to_string(transport.method))),
#("callback", json_ext.option(transport.callback, json.string)),
#("secret", json_ext.option(transport.secret, json.string)),
#("session_id", json_ext.option(transport.session_id, json.string)),
#("connected_at", json_ext.option(transport.connected_at, json.string)),
#(
"disconnected_at",
json_ext.option(transport.disconnected_at, json.string),
),
])
}
pub type Method {
Conduit
WebHook
WebSocket
}
pub fn method_to_string(method: Method) -> String {
case method {
Conduit -> "conduit"
WebHook -> "webhook"
WebSocket -> "websocket"
}
}
pub fn method_from_string(string: String) -> Result(Method, Nil) {
case string {
"conduit" -> Ok(Conduit)
"webhook" -> Ok(WebHook)
"websocket" -> Ok(WebSocket)
_ -> Error(Nil)
}
}
pub fn method_decoder() -> Decoder(Method) {
fn(data: dynamic.Dynamic) {
use string <- result.try(dynamic.string(data))
string
|> method_from_string
|> result.replace_error([
dynamic.DecodeError(
expected: "Method",
found: "String(" <> string <> ")",
path: [],
),
])
}
}