forked from shabiel/fileman-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_how_to.txt
369 lines (307 loc) · 14.1 KB
/
doc_how_to.txt
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
Input to what File: PACKAGE// WEB SERVICE URL HANDLER
(13 entries)
EDIT WHICH FIELD: ALL//
Select WEB SERVICE URL HANDLER HTTP VERB: ??
Choose from:
GET fileman/dd/{fields} DD^DDWC001
GET fileman/{file}/{iens} F^%W0
GET fileman/{file}/{iens}/{field} FV^%W0
GET filesystem/* FILESYS^%W0
GET mocha/{type} MOCHA^%W0
GET r/{routine?.1"%25".32AN} R^%W0
GET xml XML^VPRJRSP
OPTIONS rpc/{rpc} RPCO^%W0
POST MOCHA/ordercheck MOCHAP^%W0
POST fileman/vals VALS^DDWC001
POST mocha/{type} MOCHAP^%W0
POST rpc/{rpc} RPC^%W0
POST xmlpost POSTTEST^%W0
PUT r/{routine?.1"%25".32AN} PR^%W0
You may enter a new WEB SERVICE URL HANDLER, if you wish
Choose from:
POST POST
PUT PUT
GET GET
DELETE DELETE
OPTIONS OPTIONS
HEAD HEAD
TRACE TRACE
CONNECT CONNECT
Select WEB SERVICE URL HANDLER HTTP VERB: POST
URI: fileman/fda
EXECUTION ENDPOINT: FDAPOST^DDWC001
Are you adding 'POST' as a new WEB SERVICE URL HANDLER (the 14TH)? No// Y
(Yes)
URI: fileman/fda//
EXECUTION ENDPOINT: FDAPOST^DDWC001//
AUTHENTICATION REQUIRED?: Y YES
KEY:
REVERSE KEY:
OPTION:
FDAPOST(ARGS,BODY,RESULT) ; Post an FDA directly to update Fileman Data
K ^KBANFDA ZSHOW "*":^KBANFDA
QUIT ""
;
;
sakura@icarus:/var2/local/foia201303-2$ curl -k -XPOST https://thebes.smh101.com/fileman/fda -d@-
FDA(1001,"+1,",.01)="ABC"
FDA(1001,"+1,",1)="DEF"
<CTRL-D>
Looking at the result, we see this, and can happily confirm that the server is working.
^KBANFDA("S",1)="FDAPOST+1^DDWC001"
^KBANFDA("S",2)=" Indirection"
^KBANFDA("S",3)="RESPOND+14^VPRJRSP"
^KBANFDA("S",4)="RESPOND+11^VPRJRSP"
^KBANFDA("S",5)="WAIT+36^VPRJREQ"
^KBANFDA("V",1)="%=""71.37.7.7"""
^KBANFDA("V",2)="%WOS=""GT.M"""
^KBANFDA("V",3)="%WTCP=0"
^KBANFDA("V",4)="BODY(1)=""FDA(1001,""""+1,"""",.01)=""""ABC""""FDA(1001,""""+1,"""",1)=""""DEF"""""""
^KBANFDA("V",5)="*HTTPARGS=ARGS"
^KBANFDA("V",6)="HTTPERR=0"
^KBANFDA("V",7)="HTTPLOG=0"
^KBANFDA("V",8)="HTTPLOG(""DT"")=63221"
^KBANFDA("V",9)="HTTPLOG(""ID"")=30"
^KBANFDA("V",10)="HTTPREQ(""header"")=""content-type"""
^KBANFDA("V",11)="HTTPREQ(""header"",""accept"")=""*/*"""
^KBANFDA("V",12)="HTTPREQ(""header"",""content-length"")=48"
^KBANFDA("V",13)="HTTPREQ(""header"",""content-type"")=""application/x-www-form-urlencoded"""
^KBANFDA("V",14)="HTTPREQ(""header"",""host"")=""thebes.smh101.com"""
^KBANFDA("V",15)="HTTPREQ(""header"",""user-agent"")=""curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 lib
idn/1.23 librtmp/2.3"""
^KBANFDA("V",16)="HTTPREQ(""method"")=""POST"""
^KBANFDA("V",17)="HTTPREQ(""paging"")=""0:999999"""
^KBANFDA("V",18)="HTTPREQ(""path"")=""/fileman/fda"""
^KBANFDA("V",19)="HTTPREQ(""query"")="""""
^KBANFDA("V",20)="HTTPREQ(""store"")=""data"""
^KBANFDA("V",21)="IO(""IP"")=""71.37.7.7"""
^KBANFDA("V",22)="*RESULT=HTTPRSP"
^KBANFDA("V",23)="ROUTINE=""FDAPOST^DDWC001"""
^KBANFDA("V",24)="TCPX="""""
The variable we are most interested in is `BODY`. That contains the posted/putted data. In this case, our two lines seem to have been jumbled together. I actually know from previous experience that this happens with curl when using --data option. Instead, we need to use the --data-binary option so that new lines won't get stripped away.
sakura@icarus:/var2/local/foia201303-2$ curl -k -XPOST https://thebes.smh101.com/fileman/fda --data-binary @-
FDA(1001,"+1,",.01)="ABC"
FDA(1001,"+1,",1)="DEF"
Now, we see that the data printed out as follows, which is much more to our liking.
^KBANFDA("V",4)="BODY(1)=""FDA(1001,""""+1,"""",.01)=""""ABC""""""_$C(10)_""FDA(1001,""""+1,"""",1)=""""DEF""""""_$C(10)"
Now, let's edit the routine to parse the FDA:
FDAPOST(ARGS,BODY,RESULT) ; Post an FDA directly to update Fileman Data
N PARSED ; Parsed array which stores each line on a separate node.
D PARSE10^VPRJRUT(.BODY,.PARSED) ; Parser
K ^KBANFDA M ^KBANFDA=PARSED
QUIT ""
;
Now, here's the result:
GTM>zwrite ^KBANFDA
^KBANFDA(1)="FDA(1001,""+1,"",.01)=""ABC"""
^KBANFDA(2)="FDA(1001,""+1,"",1)=""DEF"""
^KBANFDA(3)=""
This is actually good enough to help us write the rest of the code.
Let's do it, assuming we are using FDA as the variable:
FDAPOST(ARGS,BODY,RESULT) ; Post an FDA directly to update Fileman Data
N PARSED ; Parsed array which stores each line on a separate node.
D PARSE10^VPRJRUT(.BODY,.PARSED) ; Parser
N I S I=""
F S I=$O(PARSED(I)) Q:I="" D
. I PARSED(I)'="" S @PARSED(I)
D UPDATE^DIE("",$NA(FDA))
QUIT "" <-- Must quit with a value for any PUT or POST requests. See later for why.
At the same time, on this instance, we need to make sure we are making a valid FDA. Here's the condensed DD...
CONDENSED DATA DICTIONARY---BACH WERKE VERZEICHNIS FILE (#1001)
STORED IN: ^BWV( FEB 3,2014 PAGE 1
--------------------------------------------------------------------------------
FILE SECURITY
DD SECURITY : DELETE SECURITY:
READ SECURITY : LAYGO SECURITY :
WRITE SECURITY :
CROSS REFERENCED BY:
BWV NUMBER(B)
FILE STRUCTURE
FIELD FIELD
NUMBER NAME
.01 BWV NUMBER (RFJ7), [0;1]
.02 WORK TITLE (FJ200), [0;2]
.03 YEAR COMPOSED (NJ4,0), [0;3]
1 INSTRUMENT (Multiple-1001.01), [1;0]
.01 INSTRUMENT (MFJ100), [0;1]
.02 NUMBER (NJ2,0), [0;2]
2.1 KEY (P1002), [2;1]
Now, let's go ahead and file the data with curl.
curl -k -XPOST https://thebes.smh101.com/fileman/fda --data-binary @-
FDA(1001,"+1,",.01)=565
FDA(1001,"+1,",.02)="Toccata and Fugue"
FDA(1001,"+1,",.03)=1780
<CTRL-D><CTRL-D>
Now, let's check our handy work:
Output from what File: BACH WERKE VERZEICHNIS// (6 entries)
Select BACH WERKE VERZEICHNIS BWV NUMBER: 565 Toccata and Fugue
Another one:
Standard Captioned Output? Yes// (Yes)
Include COMPUTED fields: (N/Y/R/B): NO// - No record number (IEN), no Computed Fields
BWV NUMBER: 565 WORK TITLE: Toccata and Fugue
YEAR COMPOSED: 1780
## Passing flags
We need to support passing flags to the updater call. We need to decide a format--Since we are not passing in JSON using this call, we cannot pass it as part of the JSON. What we can do is to obtain it from the URL as a query variable.
So, a url like https://thebes.smh101.com/fileman/fda?flags=E may do the trick. Supporting that in our code is easy.
Notice that in our code, the FLAGS variable is obtained from ARGS("flags"), which is where MWS puts the query parameter from the URL.
I made a consious decision for future compatibility not to restrict which flags can go into the updater. If there is an error, we will leave it to Fileman to handle that. But we will deal with that in a moment.
FDAPOST(ARGS,BODY,RESULT) ; Post an FDA directly to update Fileman Data
N PARSED ; Parsed array which stores each line on a separate node.
D PARSE10^VPRJRUT(.BODY,.PARSED) ; Parse by CR/LF
;
; Process flags
N FLAGS S FLAGS=""
I $D(ARGS("flags")) S FLAGS=$$UP^VPRJRUT(ARGS("flags"))
;
; Set FDA
N I S I=""
F S I=$O(PARSED(I)) Q:I="" D
. I PARSED(I)'="" S @PARSED(I)
;
D UPDATE^DIE(FLAGS,$NA(FDA))
QUIT ""
Now, to test it using curl again. Before we do that, we have to find an appropriate experiment.
Remember we had in our data dictionary a field named KEY that was a pointer? Normally, we will only be able to file it ONLY IF we supply it with a number, unless we supply the external form, which is the key name. Let's file our Toccata and Fugue in D minor BWV 565.
GTM>ZWRITE ^MusicKey
^MusicKey(0)="MUSICAL KEY^1002^5^5"
^MusicKey(1,0)="F MAJOR"
^MusicKey(2,0)="G MAJOR"
^MusicKey(3,0)="D MAJOR"
^MusicKey(4,0)="B FLAT MAJOR"
^MusicKey(5,0)="D MINOR"
^MusicKey("B","B FLAT MAJOR",4)=""
^MusicKey("B","D MAJOR",3)=""
^MusicKey("B","D MINOR",5)=""
^MusicKey("B","F MAJOR",1)=""
^MusicKey("B","G MAJOR",2)=""
sakura@icarus:/var2/local/foia201303-2$ curl -k -XPOST https://thebes.smh101.com/fileman/fda?flags=E --data-binary @-
FDA(1001,"+1,",.01)=565
FDA(1001,"+1,",.02)="Toccata and Fugue"
FDA(1001,"+1,",.03)=1680
FDA(1001,"+1,",2.1)="D MINOR"
And now let's check:
Select BACH WERKE VERZEICHNIS BWV NUMBER: 565
1 565 Toccata and Fugue
2 565 Toccata and Fugue D MINOR
CHOOSE 1-2: 2 565 Toccata and Fugue D MINOR
Another one:
Standard Captioned Output? Yes// (Yes)
Include COMPUTED fields: (N/Y/R/B): NO// - No record number (IEN), no Computed Fields
BWV NUMBER: 565 WORK TITLE: Toccata and Fugue
YEAR COMPOSED: 1680 KEY: D MINOR
That's great. The easy part of the coding is done.
A required part of a RESTful interface is to return the URL where the created data can be accessed. This can be easily done. That's the purpose of the extrinsic return which we left as an empty string up till now.
Run the curl command again, but this time add a `-i` to tell it to print out the return headers. Here they are:
sakura@icarus:/var2/local/foia201303-2$ curl -k -i -XPOST https://thebes.smh101.com/fileman/fda?flags=E --data-binary @-
FDA(1001,"+1,",.01)=565
FDA(1001,"+1,",.02)="Toccata and Fugue"
FDA(1001,"+1,",.03)=1680
FDA(1001,"+1,",2.1)="D MINOR"
HTTP/1.1 200 OK
Date: Wed, 05 Feb 2014 22:02:34 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 0
Let's make the change to return a url.
FDAPOST(ARGS,BODY,RESULT) ; Post an FDA directly to update Fileman Data
N PARSED ; Parsed array which stores each line on a separate node.
D PARSE10^VPRJRUT(.BODY,.PARSED) ; Parse by CR/LF
;
; Process flags
N FLAGS S FLAGS=""
I $D(ARGS("flags")) S FLAGS=$$UP^VPRJRUT(ARGS("flags"))
;
; Set FDA
N I S I=""
F S I=$O(PARSED(I)) Q:I="" D
. I PARSED(I)'="" S @PARSED(I)
;
N IEN,FILE
S FILE=$O(FDA(""))
D UPDATE^DIE(FLAGS,$NA(FDA),$NA(IEN))
QUIT "/fileman/"_FILE_"/"_IEN(1)
---
Let's try it now.
sakura@icarus:/var2/local/foia201303-2$ curl -k -i -XPOST https://thebes.smh101.com/fileman/fda?flags=E --data-binary @-
FDA(1001,"+1,",.01)=565
FDA(1001,"+1,",.02)="Toccata and Fugue"
FDA(1001,"+1,",.03)=1680
FDA(1001,"+1,",2.1)="D MINOR"
HTTP/1.1 201 Created
Date: Wed, 05 Feb 2014 22:22:52 GMT
Location: https://thebes.smh101.com/fileman/1001/10
Content-Type: application/json; charset=utf-8
Content-Length: 0
We got the location header back telling us where the posted data is located. And let's try that URL:
sakura@icarus:/var2/local/foia201303-2$ curl -k https://thebes.smh101.com/fileman/1001/10
{"BWV NUMBER":565,"KEY":"D MINOR","WORK TITLE":"Toccata and Fugue","YEAR COMPOSED":1680}
Perfect!
---
## Handling errors
Right now we return with an HTTP success code whether the updater succeeded or not.
We actually need to check whether fileman actually succeeded. We need to check the variable DIERR and return what's in TMP("DIERR",$J) as the error message.
In my testing, it turned out that that format returned by Fileman is difficult to represent in JSON:
^KBANERR("DIERR",16482,1)=701
^KBANERR("DIERR",16482,1,"PARAM",0)=3
^KBANERR("DIERR",16482,1,"PARAM",3)=1880
^KBANERR("DIERR",16482,1,"PARAM","FIELD")=.03
^KBANERR("DIERR",16482,1,"PARAM","FILE")=1001
^KBANERR("DIERR",16482,1,"TEXT",1)="The value '1880' for field YEAR COMPOSED in file BACH WERKE VERZEICHNIS is not valid."
^KBANERR("DIERR",16482,"E",701,1)=""
So after some fiddling and addition of an expanded error format to MWS, this is the new code.
FDAPOST(ARGS,BODY,RESULT) ; Post an FDA directly to update Fileman Data
N PARSED ; Parsed array which stores each line on a separate node.
D PARSE10^VPRJRUT(.BODY,.PARSED) ; Parse by CR/LF
;
; Process flags
N FLAGS S FLAGS=""
I $D(ARGS("flags")) S FLAGS=$$UP^VPRJRUT(ARGS("flags"))
;
; Set FDA
N I S I=""
F S I=$O(PARSED(I)) Q:I="" D
. I PARSED(I)'="" S @PARSED(I)
;
N DIERR
N IEN,FILE
S FILE=$O(FDA(""))
D UPDATE^DIE(FLAGS,$NA(FDA),$NA(IEN))
;
I $D(DIERR) DO QUIT ""
. N ERROR
. N I S I=0 F S I=$O(^TMP("DIERR",$J,I)) Q:'I D
. . M ERROR("PARAM")=^TMP("DIERR",$J,I,"PARAM")
. . S ERROR("PARAM","CODE")=^TMP("DIERR",$J,I)
. . M ERROR("TEXT")=^TMP("DIERR",$J,I,"TEXT")
. . D SETERROR^VPRJRUT(400,,.ERROR)
. D CLEAN^DILF ; Remove Fileman temp vars
;
QUIT "/fileman/"_FILE_"/"_IEN(1)
To test it, try this:
sakura@icarus:/tmp$ curl -ks -XPOST https://thebes.smh101.com/fileman/fda?flags=E --data-binary @- | python -mjson.tool
FDA(1001,"+1,",.01)=565
FDA(1001,"+1,",.02)="Toccata and Fugue"
FDA(1001,"+1,",.03)=1880
FDA(1001,"+1,",2.1)="D MINOR"
<CTRL-D><CTRL-D>
{
"apiVersion": "1.0",
"error": {
"code": 400,
"errors": [
{
"PARAM": {
"0": 3,
"3": 1880,
"CODE": 701,
"FIELD": ".03",
"FILE": 1001
},
"TEXT": [
"The value '1880' for field YEAR COMPOSED in file BACH WERKE VERZEICHNIS is not valid."
]
}
],
"message": "Bad Request",
"request": "POST /fileman/fda flags=E"
}
}