-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.gleam
124 lines (111 loc) · 3.28 KB
/
user.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
import gleam/dynamic.{type Decoder}
import gleam/list
import gleam/option.{type Option}
import gleam/result
import gleam/uri.{type Uri}
import gleam/json.{type DecodeError, type Json}
import gleam/http/request
import gleam/http/response.{type Response}
import glitch/api/client.{type Client}
import glitch/extended/dynamic_ext
import glitch/extended/json_ext
import glitch/api/api_response
pub type User {
User(
id: String,
login: String,
display_name: String,
user_type: Type,
broadcaster_type: BroadcasterType,
description: String,
profile_image_url: Uri,
offline_image_url: Uri,
view_count: Int,
email: Option(String),
created_at: String,
)
}
fn decoder() -> Decoder(User) {
dynamic_ext.decode11(
User,
dynamic.field("id", dynamic.string),
dynamic.field("login", dynamic.string),
dynamic.field("display_name", dynamic.string),
dynamic.field("type", dynamic.string),
dynamic.field("broadcaster_type", dynamic.string),
dynamic.field("description", dynamic.string),
dynamic.field("profile_image_url", dynamic_ext.uri),
dynamic.field("offline_image_url", dynamic_ext.uri),
dynamic.field("view_count", dynamic.int),
dynamic.optional_field("email", dynamic.string),
dynamic.field("created_at", dynamic.string),
)
}
pub fn to_json(user: User) -> Json {
json.object([
#("id", json.string(user.id)),
#("login", json.string(user.login)),
#("display_name", json.string(user.display_name)),
#("type", json.string(user.user_type)),
#("broadcaster_type", json.string(user.broadcaster_type)),
#("description", json.string(user.description)),
#("profile_image_url", json_ext.uri(user.profile_image_url)),
#("offline_image_url", json_ext.uri(user.offline_image_url)),
#("view_count", json.int(user.view_count)),
#("email", json_ext.option(user.email, json.string)),
#("created_at", json.string(user.created_at)),
])
}
pub fn from_json(json_string: String) -> Result(User, DecodeError) {
json.decode(json_string, decoder())
}
pub type Type =
String
pub type BroadcasterType =
String
pub type GetUsersRequest {
GetUsersRequest(
user_ids: Option(List(String)),
user_logins: Option(List(String)),
)
}
pub fn query_params_from_get_users_request(
req: GetUsersRequest,
) -> List(#(String, String)) {
let to_query_param_list = fn(input: #(String, Option(List(String)))) -> List(
#(String, String),
) {
input.1
|> option.map(fn(values) {
values
|> list.map(fn(value) { #(input.0, value) })
})
|> option.unwrap([])
}
[#("id", req.user_ids), #("login", req.user_logins)]
|> list.map(to_query_param_list)
|> list.flatten
}
pub type GetUsersError {
DecodeError(DecodeError)
RequestError
}
pub fn get_users(
client: Client,
request: GetUsersRequest,
) -> Result(Response(List(User)), GetUsersError) {
let request =
request.new()
|> request.set_body(json.string(""))
|> request.set_query(query_params_from_get_users_request(request))
|> request.set_path("users")
use response <- result.try(
client
|> client.get(request)
|> result.replace_error(RequestError),
)
response
|> response.try_map(api_response.from_json(_, decoder()))
|> result.try(api_response.get_data_from_response)
|> result.map_error(DecodeError)
}