-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontents.v
196 lines (178 loc) · 4.95 KB
/
contents.v
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
module microcms
import json
import net.urllib
import net.http
// GetContentListParams holds information for retrieve the content list.
pub struct GetContentListParams {
pub mut:
endpoint string
draft_key string
limit int
offset int
orders []string
q string
fields []string
ids []string
filters string
depth int
rich_editor_format string
}
// GetContentParams holds information for retrieve a single content.
pub struct GetContentParams {
pub mut:
endpoint string
content_id string
draft_key string
fields []string
depth int
rich_editor_format string
}
// CreateParams holds information for creating content.
// Note: It does not hold the content itself.
pub struct CreateParams {
pub mut:
endpoint string
content_id string
status string
}
// UpdateParams holds information for updating content.
// Note: It does not hold the content itself.
pub struct UpdateParams {
pub mut:
endpoint string
content_id string
}
// DeleteParams holds information for deleting content.
pub struct DeleteParams {
pub mut:
endpoint string
content_id string
}
// CreateResponse holds the response body
// of a successful microCMS content creation.
pub struct CreateResponse {
pub:
id string
}
// UpdateResponse holds the response body
// of a successful microCMS content update.
pub struct UpdateResponse {
pub:
id string
}
fn make_list_query(p GetContentListParams) urllib.Values {
mut v := urllib.new_values()
if p.draft_key.len > 0 {
v.add('draftKey', p.draft_key)
}
if p.limit != 0 {
v.add('limit', p.limit.str())
}
if p.offset != 0 {
v.add('offset', p.offset.str())
}
if p.orders.len > 0 {
v.add('orders', p.orders.join(','))
}
if p.q.len > 0 {
v.add('q', p.q)
}
if p.fields.len > 0 {
v.add('fields', p.fields.join(','))
}
if p.ids.len > 0 {
v.add('ids', p.ids.join(','))
}
if p.filters.len > 0 {
v.add('filters', p.filters)
}
if p.depth != 0 {
v.add('depth', p.depth.str())
}
if p.rich_editor_format.len > 0 {
v.add('richEditorFormat', p.rich_editor_format)
}
return v
}
fn make_get_query(p GetContentParams) urllib.Values {
mut v := urllib.new_values()
if p.draft_key.len > 0 {
v.add('draftKey', p.draft_key)
}
if p.fields.len > 0 {
v.add('fields', p.fields.join(','))
}
if p.depth != 0 {
v.add('depth', p.depth.str())
}
if p.rich_editor_format.len > 0 {
v.add('richEditorFormat', p.rich_editor_format)
}
return v
}
// content_list is a method to retrieve a content list.
// The retrieved content is stored in the type parameter T and returned.
pub fn (c Client) content_list<T>(p GetContentListParams) ?T {
req := c.new_request(.get, p.endpoint, make_list_query(p))?
res := send_request(req)?
return json.decode(T, res.body)
}
// content_list is a method to retrieve a content list.
// If you want to decode your own, use this one.
pub fn (c Client) content_list_str(p GetContentListParams) ?string {
req := c.new_request(.get, p.endpoint, make_list_query(p))?
res := send_request(req)?
return res.body
}
// content is a method to retrieve a single content.
// The retrieved content is stored in the type parameter T and returned.
pub fn (c Client) content<T>(p GetContentParams) ?T {
req := c.new_request(.get, '$p.endpoint/$p.content_id', make_get_query(p))?
res := send_request(req)?
return json.decode(T, res.body)
}
// content_str is a method to retrieve a single content.
// If you want to decode your own, use this one.
pub fn (c Client) content_str(p GetContentParams) ?string {
req := c.new_request(.get, '$p.endpoint/$p.content_id', make_get_query(p))?
res := send_request(req)?
return res.body
}
fn make_create_query(p CreateParams) urllib.Values {
mut v := urllib.new_values()
if p.status.len > 0 {
v.add('status', p.status)
}
return v
}
// create_content is a method to create content.
// It sends an http request with data as the body.
pub fn (c Client) create_content<T>(p CreateParams, data T) ?CreateResponse {
mut req := http.Request{}
query := make_create_query(p)
content := json.encode(data)
println(content)
if p.content_id.len > 0 {
req = c.new_request(.put, '$p.endpoint/$p.content_id', query)?
} else {
req = c.new_request(.post, p.endpoint, query)?
}
req.data = content
res := send_request(req)?
return json.decode(CreateResponse, res.body)
}
// update_content is a method to update content.
// It sends an http request with data as the body.
pub fn (c Client) update_content<T>(p UpdateParams, data T) ?UpdateResponse {
mut req := c.new_request(.patch, '$p.endpoint/$p.content_id', urllib.Values{})?
content := json.encode(data)
req.data = content
res := send_request(req)?
return json.decode(UpdateResponse, res.body)
}
// delete_content is a method to delete content.
pub fn (c Client) delete_content(p DeleteParams) ? {
req := c.new_request(.delete, '$p.endpoint/$p.content_id', urllib.Values{})?
send_request(req)?
return
}