-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathredcap.scm
961 lines (917 loc) · 42 KB
/
redcap.scm
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#|
LambdaNative - a cross-platform Scheme framework
Copyright (c) 2009-2020, University of British Columbia
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the University of British Columbia nor
the names of its contributors may be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|#
(include "redcap-unittest.scm")
;; REDCap module - Allows data export-/import to an Research Electronic Data Capture server
;; intermediate data handling
(define redcap:datachunk 10000)
(define redcap:data (u8vector))
(define redcap:datalen 0)
(define (redcap:data-clear!)
(set! redcap:datalen 0)
(u8vector-shrink! redcap:data 0))
(define (redcap:data->string)
(let ((str (u8vector->utf8string (subu8vector redcap:data 0 redcap:datalen))))
(redcap:data-clear!)
str))
(define (redcap:data->u8vector)
(let ((v (subu8vector redcap:data 0 redcap:datalen)))
(redcap:data-clear!)
v))
(define (redcap:data-append! v)
(let ((lv (u8vector-length v))
(la (u8vector-length redcap:data)))
(if (> (+ redcap:datalen lv) la)
(set! redcap:data (u8vector-append redcap:data (make-u8vector (max lv redcap:datachunk)))))
(subu8vector-move! v 0 lv redcap:data redcap:datalen)
(set! redcap:datalen (+ redcap:datalen lv))))
;; Local variables specifying REDCap server settings
(define redcap:url "/redcap/api/")
(define (redcap-url-set! url) (set! redcap:url url))
(define redcap:port 443)
(define (redcap-port-set! port) (set! redcap:port port))
(define redcap:user-agent "lambdanative/1.0")
(define redcap:content-type "application/x-www-form-urlencoded")
(define redcap:content-type-file "multipart/form-data")
(define redcap:boundary (string-append "------------------------------"
(number->string (inexact->exact (floor (current-time-seconds))))))
(define redcap:buf (##still-copy (make-u8vector 1024)))
;; Helper function to remove quotes from strings.
(define (redcap:string-remove-quotes str)
(let ((strlength (string-length str)))
(if (and (fx>= (string-length str) 2)
(string=? (substring str 0 1) "\"")
(string=? (substring str (- strlength 1) strlength) "\""))
(substring str 1 (- strlength 1))
str)
))
;; Helper function to parse REDCAP JSON format [much easier than XML parsing]
(define (redcap:jsonstr->list str)
(cond ((and (string? str) (string-contains str "[{") (string-contains str "}]"))
(let* ((index (string-contains str "[{"))
;; Remove anything outside brackets first
(output (json-decode (substring str index (string-length str)))))
(if (json-error? output) #f (vector->list output))))
((and (string? str) (string-prefix? "{" str) (string-suffix? "}" str))
;; Try adding square brackets around it
(redcap:jsonstr->list (string-append "[" str "]")))
((or (list? str) (and (string? str) (string-contains str "[]"))) '())
(else #f)))
;; Helper function to split return string into header and body
(define (redcap:split-headerbody str)
(let ((pos (string-contains str "\r\n\r\n")))
(if pos (list (substring str 0 pos) (substring str (+ pos 4) (string-length str))) (list str (list)))
)
)
;; Helper function that returns a returned vector split into header and body
;; and changes the header into a string
(define (redcap:split-headerbody-vector vctr)
(let loop ((lst (u8vector->list vctr)) (hdrlst '()))
(if (fx> (length lst) 4)
;; Look for the vector equivalent of "\r\n\r\n"
(if (and (fx= (car lst) 13) (fx= (cadr lst) 10) (fx= (caddr lst) 13) (fx= (cadddr lst) 10))
;; Turn the header into a string, keep everything after the header as a vector
(list (u8vector->string (list->u8vector hdrlst)) (list->u8vector (list-tail lst 4)))
;; Otherwise go to the next spot in the list
(loop (cdr lst) (append hdrlst (list (car lst)))))
;; If through the list and no divider found, just return as string - just header
(list (u8vector->string vctr) #f)))
)
;; Helper function which makes REDCap EAV XML given a record identifier and list of values
(define (redcap:list->xmlstr record event data . xargs)
(let* ((instance (redcap:arg 'instance xargs #f))
(instrument (redcap:arg 'instrument xargs #f))
(instance-string (if instance
(string-append "<redcap_repeat_instance>" instance "</redcap_repeat_instance>")
""))
(instrument-string (if (and instrument instance)
(string-append "<redcap_repeat_instrument>" instrument "</redcap_repeat_instrument>")
""))
(repeat (string-append instrument-string instance-string)))
(string-append
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" "\r\n"
"<records>" "\r\n"
(let loop ((i 0) (str ""))
(if (= i (length data)) str
(loop (+ i 1)
(string-append str
(let* ((lpair (list-ref data i))
(field (car lpair))
(fieldname (if (string? field) field (symbol->string field)))
(values (cdr lpair))
(itemize (lambda (v)
(let ((value (if (number? v) (number->string v) (if (string? v) v "NA"))))
(string-append
"<item>"
"<record>" record "</record>"
repeat
;; Only add event if it is non-blank
(if (string=? event "")
""
(string-append "<redcap_event_name>" event "</redcap_event_name>"))
"<field_name>" fieldname "</field_name>"
"<value>" value "</value>"
"</item>" "\r\n")))))
(if (list? values)
(string-mapconcat values "" itemize)
(itemize values)))))))
"</records>"))
)
;; Helper function to build the http request string
(define (redcap:make-request-str host request . bd)
(string-append "POST " redcap:url " HTTP/1.0" "\r\n"
"Host: " host "\r\n"
"User-Agent: " redcap:user-agent "\r\n"
"Content-Length: " (number->string (string-length request)) "\r\n"
"Content-Type: " (if (null? bd) redcap:content-type (string-append redcap:content-type-file "; boundary=" (car bd))) "\r\n"
"\r\n" request "\n"))
;; Checks for an error message in header, logs the message and returns false. If there is no error (ie. code is 200 or 201), just return true
(define (redcap:error-check msg)
(if (and (string? (car msg)) (fx> (string-length (car msg)) 12) (or (string=? (substring (car msg) 9 12) "201")
(string=? (substring (car msg) 9 12) "200")))
#t
(let ((ret (cadr msg)))
(log-error "REDCap:" (if (and (list? ret) (fx= (length ret) 0)) " Nothing returned" (cadr msg))) #f))
)
(define (redcap-export-metadata host token . xargs)
;; See if format was specified in xargs, use json by default
(let* ((format (redcap:arg 'format xargs "json"))
(forms (redcap:arg 'forms xargs #f))
(fields (redcap:arg 'fields xargs #f))
(request (string-append "format=" format "&content=metadata&token=" token (if forms (string-append "&forms[0]=" forms ) "") (if fields (string-append "&fields[0]=" fields ) "") )) ;;TODO expand to multiple fields and forms
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
;;(display request-str)
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((output (cadr (redcap:split-headerbody (redcap:data->string)))))
(if (string=? format "json")
;; If format is json, turn into a list, otherwise just return output
(let ((datalist (redcap:jsonstr->list output)))
(cond
((not (list? datalist))
;; If no list returned, json not properly formatted
(log-error "REDCap error: Incomplete json " datalist)
#f)
((and (fx> (length datalist) 0) (string=? (caaar datalist) "error\""))
;; If the first entry is an error, then log it and return false
(log-error "REDCap error: " (cdaar datalist))
#f)
(else datalist)))
output))
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
#f ;; Denote difference between no data and no connection
)
)
)
)
(define (redcap-export-instrument host token . xargs)
;; See if format was specified in xargs, use json by default
(let* ((format (redcap:arg 'format xargs "json"))
(request (string-append "format=" format "&content=instrument&token=" token))
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((output (cadr (redcap:split-headerbody (redcap:data->string)))))
(if (string=? format "json")
;; If format is json, turn into a list, otherwise just return output
(let ((datalist (redcap:jsonstr->list output)))
(cond
((not (list? datalist))
;; If no list returned, json not properly formatted
(log-error "REDCap error: Incomplete json " output)
#f)
((and (fx> (length datalist) 0) (string=? (caaar datalist) "error\""))
;; If the first entry is an error, then log it and return false
(log-error "REDCap error: " (cdaar datalist))
#f)
(else datalist)))
output))
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
#f ;; Denote difference between no data and no connection
)
)
)
)
(define (redcap-export-report host token report . xargs)
;; See if format was specified in xargs, use json by default
(let* ((format (redcap:arg 'format xargs "json"))
;; Similarly, use lists (not tables) by default
(use-tables? (redcap:arg 'tables xargs #f))
(request (string-append
"format=" format
"&content=report&token=" token
"&report_id=" report
"&csvDelimiter="
"&rawOrLabel=raw"
"&rawOrLabelHeaders=raw"
"&exportCheckboxLabel=false"
"&returnFormat=json"))
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((output (cadr (redcap:split-headerbody (redcap:data->string)))))
(if (string=? format "json")
;; If format is json, turn into a list, otherwise just return output
(let ((datalist (redcap:jsonstr->list output)))
(cond
((not (list? datalist))
;; If no list returned, json not properly formatted
(log-error "REDCap error: Incomplete json " output)
#f)
((and (not use-tables?)
(fx> (length datalist) 0)
(string=? (caaar datalist) "error"))
;; If the first entry is an error, then log it and return false
(log-error "REDCap error: " (cdaar datalist))
#f)
((and use-tables?
(fx> (length datalist) 0)
(table-ref (car datalist) "error" #f))
(log-error "REDCap error: " (table-ref (car datalist) "error" ""))
#f)
(else datalist)))
output))
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
#f ;; Denote difference between no data and no connection
)
)
)
)
(define (redcap-import-record host token record data . xargs)
(let* ((event (redcap:arg 'event xargs ""))
(instance (redcap:arg 'instance xargs #f))
(instrument (redcap:arg 'instrument xargs #f))
(type (redcap:arg 'type xargs "eav"))
(over (redcap:arg 'overwrite xargs "overwrite"))
(message (string-append "token=" token "&content=record&format=xml&type=" type "&overwriteBehavior=" over "&data=" (redcap:list->xmlstr record event data 'instance instance 'instrument instrument) " &returnContent=count&returnFormat=json"))
(request-str (redcap:make-request-str host message)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(redcap:error-check msg)
)
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot import to REDCap, no valid connection")
(httpsclient-close)
#f
)
)
)
)
(define (redcap-import-record-csv host token str . xargs)
(let* ((bd redcap:boundary)
(ct (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"content\"" "\r\n" "\r\n"
"record" "\r\n"))
(ft (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"format\"" "\r\n" "\r\n"
"csv" "\r\n"))
(tp (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"type\"" "\r\n" "\r\n"
"flat" "\r\n"))
;; Default overwriteBehavior is "overwrite", but can be specified in xargs as "normal"
(over (redcap:arg 'overwrite xargs "overwrite"))
(ob (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"overwriteBehavior\"" "\r\n" "\r\n"
over "\r\n"))
(tk (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"token\"" "\r\n" "\r\n"
token "\r\n"))
(dt (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"data\"" "\r\n"
"Content-Type: text/csv" "\r\n" "\r\n"
str "\r\n"))
(cl (string-append "--" bd "--"))
(request (string-append ct ft tp ob tk dt cl))
(request-str (redcap:make-request-str host request bd)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(redcap:error-check msg)
)
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv-reentrant redcap:buf))
))
)
)
(begin
(log-warning "Cannot import to REDCap, no valid connection")
(httpsclient-close)
#f
)
)
)
)
(define (redcap-export-records host token . xargs)
(let* ((format (redcap:arg 'format xargs "json"));; See if format was specified in xargs, use json by default
(type (redcap:arg 'type xargs "flat"))
(request (string-append "token=" token "&content=record&format=" format "&type=" type))
(records (redcap:arg 'records xargs #f))
(recordstr (if (pair? records)
(let loop ((i 0) (str ""))
(if (= i (length records)) str
(loop (+ i 1) (string-append str "&records%5B" (number->string i) "%5D=" (list-ref records i)))
))
""))
(events (redcap:arg 'events xargs #f))
(eventstr (if (pair? events)
(let loop ((i 0) (str ""))
(if (= i (length events)) str
(loop (+ i 1) (string-append str "&events%5B" (number->string i) "%5D=" (list-ref events i)))
))
""))
(forms (redcap:arg 'forms xargs #f))
(formstr (if (pair? forms)
(let loop ((i 0) (str ""))
(if (= i (length forms)) str
(loop (+ i 1) (string-append str "&forms%5B" (number->string i) "%5D=" (list-ref forms i)))
))
""))
(fields (redcap:arg 'fields xargs #f))
(fieldstr (if (pair? fields)
(let loop ((i 0) (str ""))
(if (= i (length fields)) str
(loop (+ i 1) (string-append str "&fields%5B" (number->string i) "%5D=" (list-ref fields i)))
))
""))
(filter (redcap:arg 'filter xargs #f))
(filterstr (if (string? filter)
(string-append "&filterLogic=" filter)
""))
(request-all (string-append request eventstr formstr fieldstr recordstr filterstr))
(request-str (redcap:make-request-str host request-all)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(if (redcap:error-check msg)
(let ((output (cadr msg)))
(if (string=? format "json")
;; If format is json, turn into a list, otherwise just return output
(if (string? output)
;; If a string turn into a list, otherwise just return false
(let ((datalist (redcap:jsonstr->list output)))
(cond
((not (list? datalist))
;; If no list returned, json not properly formatted
(log-error "REDCap error: Incomplete json " msg)
#f)
((and (fx> (length datalist) 0) (string=? (caaar datalist) "error\""))
;; If the first entry is an error, then log it and return false
(log-error "REDCap error: " (cdaar datalist))
#f)
(else datalist)))
#f)
output))
#f))
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv-reentrant redcap:buf))
))
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
#f ;; Return false to denote difference between no data and no connection
)
)
)
)
;; calls the export project to get fields such as project_id, project_title, creation_time, production_time, in_production, project_language, purpose, purpose_other, project_notes, custom_record_label, secondary_unique_field, is_longitudinal, has_repeating_instruments_or_events, surveys_enabled, scheduling_enabled, record_autonumbering_enabled, randomization_enabled, ddp_enabled, project_irb_number, project_grant_number, project_pi_firstname, project_pi_lastname, display_today_now_button, missing_data_codes, external_modules
;; requires redcap v >10.0 to function
(define (redcap-export-project-info host token . xargs)
;; See if format was specified in xargs, use json by default
(let* ((format (redcap:arg 'format xargs "json"))
(request (string-append "format=" format "&returnFormat=json&content=project&token=" token))
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((output (cadr (redcap:split-headerbody (redcap:data->string)))))
(if (string=? format "json")
;; If format is json, turn into a list, otherwise just return output
(let ((datalist (redcap:jsonstr->list output)))
(cond
((not (list? datalist))
;; If no list returned, json not properly formatted
(log-error "REDCap error: Incomplete json " output)
#f)
((and (fx> (length datalist) 0) (string=? (caaar datalist) "error\""))
;; If the first entry is an error, then log it and return false
(log-error "REDCap error: " (cdaar datalist))
#f)
(else datalist)))
output))
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
#f ;; Denote difference between no data and no connection
)
)
)
)
;; Get the next instance index given a repeatable event or instrument
;; For events, supplying only the event name will suffice
;; For instruments, if the project is longitudinal,
;; both the instrument name and the event should be supplied,
;; otherwise the next index over all events will be returned.
;; If neither are given, the next index over all repeated events and instruments
;; will be returned; this is not likely what is desired.
(define (redcap-get-next-instance-index host token record . xargs)
(let* ((form (redcap:arg 'form xargs #f))
(event (redcap:arg 'event xargs #f))
(records (if record (if (pair? record) record (list record)) #f))
(forms (if form (if (pair? form) form (list form)) #f))
(events (if event (if (pair? event) event (list event)) #f))
(response (redcap-export-records host token 'forms forms 'records records 'events events 'fields "redcap_repeat_instance" 'type "eav")))
(cond ((not response) #f)
((= (length response) 0)
(begin (log-warning "No REDCap entry currently exists; instance will begin at 1.") 1))
((not (alist-ref (car response) "redcap_repeat_instance"))
(begin (log-error "This REDCap entry is not repeatable.") #f))
(else (+ 1 (foldr
(lambda (record maxinstance)
(let* ((instance (alist-ref record "redcap_repeat_instance"))
(i (if (string? instance) (string->number instance) instance)))
(if (and i (> i maxinstance)) i maxinstance)))
0 response))))))
;returns the index for instance in
(define (redcap-get-next-instance host token record form)
(let* ((forms (if (pair? form) form (list form)))
(records (if (pair? record) record (list record)))
(maxinstance 0)
(response (redcap-export-records host token 'forms forms 'records records 'type "eav")) ;;currently redcap8.0 flat does not return instances
(len (if response (length response) 0))
)
(if response
(if (fx> len 0)
(begin (let loop ((entries response))
(if (fx> (length entries) 0)
(let* ((row (car entries))
(val (alist-ref row "redcap_repeat_instance" 0))
(instrument (alist-ref row "redcap_repeat_instrument" #f))
(instance (if (string? val) (string->number (string-remove-quotes val)) val)))
(if (not instrument) (log-warning "Exported REDcap instrument " (car forms) " is not repeatable"))
(if (and instance (fx> instance maxinstance)) (set! maxinstance instance))
(loop (cdr entries))))) (fx+ maxinstance 1))
(begin (log-warning "Exported REDcap record has no repeated entry") 1))
(begin (log-error "Cannot retrieve instance number from REDCap.") #f)))
)
(define (redcap-export-fieldnames host token)
(let* ((request (string-append "token=" token "&content=exportFieldNames&format=json"))
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
;; Get data as a list, make sure first entry isn't an error
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(if (redcap:error-check msg)
(let ((datalist (redcap:jsonstr->list (cadr msg))))
(cond
((not (list? datalist))
;; If no list returned, json not properly formatted
(log-error "REDCap error: Incomplete json")
#f)
((and (fx> (length datalist) 0) (string=? (caaar datalist) "error\""))
;; If the first entry is an error, then log it and return false
(log-error "REDCap error: " (cdaar datalist))
#f)
(else
(map cdr (map caddr datalist)))))
#f)))
(begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
#f ;; Denote difference between no data and no connection
)
)
)
)
(define (redcap-export-ids host token . xargs)
(let ((fieldnames (redcap-export-fieldnames host token)))
(if fieldnames
(let* ((event (redcap:arg 'event xargs #f))
(id (if (list? fieldnames) (car fieldnames) #f))
(request (string-append "token=" token "&content=record&" (if event (string-append "events=" event "&") "")
"format=json&fields=" id "&type=flat"))
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (and (fx= (httpsclient-open host redcap:port) 1) id)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
;; Get data as a list, make sure first entry isn't an error
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(if (redcap:error-check msg)
(let ((datalist (redcap:jsonstr->list (cadr msg))))
(cond
((not (list? datalist))
;; If no list returned, json not properly formatted
(log-error "REDCap error: Incomplete json")
#f)
((and (fx> (length datalist) 0) (string=? (caaar datalist) "error\""))
;; If the first entry is an error, then log it and return false
(log-error "REDCap error: " (cdaar datalist))
#f)
(else
(maps
(lambda (entry)
(let ((id (cdar entry)))
;; May be a number if downloaded from newer REDCap - convert back to string
(if (number? id) (number->string id) id)))
datalist))))
#f)))
(begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
#f ;; Denote difference between no data and no connection
)
)
)
;; No fieldnames returned, so cannot get IDs
#f
)
)
)
(define (redcap-delete-records host token records)
(let* ((request (string-append "token=" token "&content=record&action=delete&returnFormat=json"))
(recordstr (if (pair? records)
(let loop ((i 0) (str ""))
(if (= i (length records)) str
(loop (+ i 1) (string-append str "&records%5B" (number->string i) "%5D=" (list-ref records i)))
))
""))
(request-str (redcap:make-request-str host (string-append request recordstr))))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(redcap:error-check msg))
)
(begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
)
)
)
)
(begin
(log-warning "Cannot delete record on REDCap, no valid connection")
(httpsclient-close)
#f
)
)
)
)
(define (redcap-import-file host token record field filename . xargs)
(let* ((filesize (if (file-exists? filename) (file-info-size (file-info filename)) #f))
(bd redcap:boundary)
(ct (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"content\"" "\r\n" "\r\n"
"file" "\r\n"))
(at (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"action\"" "\r\n" "\r\n"
"import" "\r\n"))
(rd (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"record\"" "\r\n" "\r\n"
record "\r\n"))
(fd (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"field\"" "\r\n" "\r\n"
field "\r\n"))
(event (redcap:arg 'event xargs #f))
(ev (if event
(string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"event\"" "\r\n" "\r\n"
event "\r\n")
""))
(repeat (redcap:arg 'repeat xargs #f))
(re (if repeat
(string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"repeat_instance\"" "\r\n" "\r\n"
repeat "\r\n")
""))
(tk (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"token\"" "\r\n" "\r\n"
token "\r\n"))
(dt (string-append "--" bd "\r\n"
"Content-Disposition: form-data; name=\"file\"; filename=\"" filename "\"" "\r\n"
"Content-Type: application/octet-stream" "\r\n" "\r\n"))
(cl (string-append "--" bd "--"))
(close-vector (u8vector-append (string->u8vector (string-append "\r\n" cl "\r\n"))))
(request-vector (string->u8vector (string-append "POST " redcap:url " HTTP/1.0" "\r\n"
"Host: " host "\r\n"
"User-Agent: " redcap:user-agent "\r\n"
"Content-Length: " (number->string (+ (string-length ct) (string-length at) (string-length rd)
(string-length fd) (string-length tk) (string-length ev) (string-length re) (string-length dt)
(if filesize filesize 0) (u8vector-length close-vector))) "\r\n"
"Content-Type: " redcap:content-type-file "; boundary=" bd "\r\n" "\r\n"
ct at rd ev re fd tk dt))))
;; Check if we have a valid connection before proceeding
(if (and filesize (fx= (httpsclient-open host redcap:port) 1))
(let* ((fh (open-input-file filename))
(buflen (if (or (string=? (system-platform) "android") (string=? (system-platform) "ios")) 50000 (if (string=? (system-platform) "linux") 100000 100000 )))
(buf (make-u8vector buflen)))
(httpsclient-send request-vector)
(let loop ((start 0) (end (if (fx< filesize buflen) filesize buflen)))
(if (fx>= start filesize)
(if (port? fh) (close-input-port fh))
(let ((len (fx- end start)))
(input-port-byte-position fh start)
(read-subu8vector buf 0 len fh)
(if (fx< len buflen)
(httpsclient-send (subu8vector buf 0 len))
(httpsclient-send buf)
)
(thread-sleep! 0.0001) ;;allow GUI to refresh
(loop (fx+ start buflen) (if (fx> (fx+ start (fx* 2 buflen)) filesize) filesize (fx+ end buflen)))
)
)
)
(httpsclient-send close-vector)
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
;; If the buffer isn't full, then there will be no more data to get
(if (and n (fx< n (u8vector-length redcap:buf)))
(begin
(httpsclient-close)
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(redcap:error-check msg)))
(loop (httpsclient-recv redcap:buf))))
)
(begin
(if (not filesize)
(let ((str (string-append "File " filename " not found."))) (log-error str))
(begin
(log-warning "Cannot import to REDCap, no valid connection")
(httpsclient-close)))
#f
)
)
)
)
(define (redcap-export-file host token record field . xargs)
(let* ((event (redcap:arg 'event xargs ""))
(repeat (redcap:arg 'repeat xargs ""))
(request (string-append "content=file&action=export&token=" token
"&record=" record
(if event
(string-append "&event=" event)
""
)
(if repeat
(string-append "&repeat_instance=" repeat)
""
)
"&field=" field))
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n 1))
(if (fx<= n 0)
(begin
(httpsclient-close)
;; Get a list consisting of the header as a string followed by the body as a vector
(let ((fileout (redcap:split-headerbody-vector (redcap:data->u8vector))))
;; If the returned code is 200 or 201, just return the header and file
(if (and (string? (car fileout)) (fx> (string-length (car fileout)) 12)
(or (string=? (substring (car fileout) 9 12) "201")
(string=? (substring (car fileout) 9 12) "200")))
fileout
;; Display and log the REDCap error message, but return the error message as a string
(let ((message (u8vector->utf8string (cadr fileout))))
(log-error "REDCap:" message)
message)
))
)
(let ((count (httpsclient-recv redcap:buf)))
(if (> count 0) (redcap:data-append! (subu8vector redcap:buf 0 count)))
(loop count))
)
)
)
(begin
(log-warning "Cannot export from REDCap, no valid connection")
(httpsclient-close)
"" ;; Could also return #f
)
)
)
)
(define (redcap-delete-file host token record field . xargs)
(let* ((event (redcap:arg 'event xargs ""))
(repeat (redcap:arg 'repeat xargs ""))
(request (string-append "content=file&action=delete&token=" token
"&record=" record
(if event
(string-append "&event=" event)
""
)
(if repeat
(string-append "&repeat_instance=" repeat)
""
)
"&field=" field))
(request-str (redcap:make-request-str host request)))
;; Check if we have a valid connection before proceeding
(if (fx= (httpsclient-open host redcap:port) 1)
(begin
(httpsclient-send (string->u8vector request-str))
(redcap:data-clear!)
(let loop ((n #f))
(if (and n (fx<= n 0))
(begin
(httpsclient-close)
(let ((msg (redcap:split-headerbody (redcap:data->string))))
(redcap:error-check msg)
)
) (begin
(if (and n (> n 0))
(redcap:data-append! (subu8vector redcap:buf 0 n)))
(loop (httpsclient-recv redcap:buf))
))
)
)
(begin
(log-warning "Cannot delete file on REDCap, no valid connection")
(httpsclient-close)
)
)
)
)
(define (redcap-get-filename header)
;; Get index of name=" which occurs before file name in the header
(let* ((nameindex1 (string-contains header "name=\""))
;; Get part of header string after the above string
(subheader (substring header (+ nameindex1 6) (string-length header)))
;; Get index of next quotation marks
(nameindex2 (if nameindex1 (string-contains subheader "\"") #f)))
;; Return the name of the file
(if (and nameindex1 nameindex2)
(substring subheader 0 nameindex2)
#f))
)
(define (redcap:arg k args def)
(let loop ((as args))
(if (fx= (length as) 0)
def
(if (equal? k (car as)) (cadr as) (loop (cdr as)))
)
))
;; eof