-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.rsp
203 lines (186 loc) · 4.2 KB
/
api.rsp
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
<%
session/end
if not value? 'api-ctx [
api-ctx: context [
ticket!: context [
summary:
description:
code:
version:
severity:
status:
resolution:
priority:
type:
platform:
created:
modified:
user:
category:
reproduce:
fixed-in:
project:
comments:
history: none
]
query!: [
preset "search"
filter 1
text #[none]
fields 1
tid #[none]
type 0
severity 0
user 0
status 0
hstatus 0
priority 0
results 99999
action search
page 1
orderby 1
version #[none]
fixedin #[none]
cat #[none]
resolv #[none]
find #[none]
apply #[none]
]
set 'send-error send-error: func [msg][
%>[error <%=mold msg%>]<%
response/end
]
set 'send-ok send-ok: func [data /ticket][
%>[ok <%=mold/all data%>]<%
]
send-xml-ok: func [data /ticket][
%><xml...><%
]
;-- add utf-8 encoding
send-json-ok: func [data /ticket][
%>{'status':0, 'list':[<% foreach row data [%>
{'id':'<%=row/1%>','type':'<%=row/3%>','severity':'<%=row/4%>','status':'<%=row/5%>','date':'<%=form row/6/date%> <%=form-time row/6/time%>','summary':'<%=js-encode row/7%>'},
<%]
remove find/reverse tail response/buffer #","
%>]
}<%
export-json
]
send-json-error: func [data][
%><xml...><%
]
]
]
either all [
none? validate [format word!]
request/content/format
][
switch request/content/format [
JSON [
set 'send-ok get in api-ctx 'send-json-ok
set 'send-error get in api-ctx 'send-json-error
]
XML [
;-- not supported yet
]
]
][
set 'send-ok get in api-ctx 'send-ok
set 'send-error get in api-ctx 'send-error
]
if validate/full [type word! *][send-error "TYPE argument not found or invalid"]
switch request/content/type [
ticket [
if any [
validate/full [id integer! *]
not positive? request/content/id
][
send-error "ID argument not found or invalid"
]
validate [show word!]
params: request/content
data: get-ticket params/id
remove/part at data 16 4
ticket: make api-ctx/ticket! []
set next bind first ticket ticket data
if ticket/fixed-in == 0 [ticket/fixed-in: none]
if find [comments all] params/show [ticket/comments: list-comments/no-flat params/id]
if find [history all] params/show [
out: make block! 8
foreach [date user action field opt-id old new msg] list-logs params/id [
append/only out new-line/all reduce [
date
user
reform [
history/decode field
any [all [opt-id join ": " zeropad opt-id 7] ""]
]
history/decode action
either all [old new] [
reform [
any [all [old history/decode/value field old] ""]
"=>" history/decode/value field new
]
][
any [msg "-"]
]
] off
]
ticket/history: new-line/all out on
]
send-ok new-line/all/skip third ticket on 2
]
list [
if any [
validate/full [filter integer! *]
request/content/filter < 2
request/content/filter > 9
][
send-error "FILTER argument not found or invalid"
]
invalid: validate [
range pair!
mode word!
]
params: request/content
if params/range [params/range/1: params/range/1 - 1]
if any [
all [invalid find invalid 'range]
all [params/range params/range/1 > params/range/2]
][
send-error "RANGE argument is invalid"
]
if any [
all [invalid find invalid 'range]
all [params/mode not find [count brief] params/mode]
][
send-error "MODE argument is invalid"
]
qry: api-ctx/query!
qry/filter: params/filter
qry/results: either params/range [
params/range/2 - params/range/1
][
99999
]
send-ok switch/default params/mode [
count [
to-integer first first select-tickets/count qry
]
brief [
either params/range [
select-tickets/range qry params/range/1 params/range/2
][
select-tickets qry
]
]
][
either params/range [
select-tickets/range/id-only qry params/range/1 params/range/2
][
select-tickets/id-only qry
]
]
]
]
%>