-
Notifications
You must be signed in to change notification settings - Fork 4
/
12_File_Export_Import.do
80 lines (67 loc) · 1.89 KB
/
12_File_Export_Import.do
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
/*******************************************************************************
Example of API File Export and Import
Luke Stevens
22-Jan-2021
*******************************************************************************/
version 16
clear
set more off
import delimited tokens.txt
local apisource=url in 1
local apidest=url in 2
local tokensource=token in 1
local tokendest=token in 2
local fieldsource=field_name in 1
local fielddest=field_name in 2
clear
di "'`apisource''"
di "'`apidest''"
di "'`tokensource''"
di "'`tokendest''"
tempname tmpname
local tempexportfile="`tmpname'.csv"
* download records with files from source project
shell curl ///
--output `tempexportfile' ///
--form token=`tokensource' ///
--form content=record ///
--form format=csv ///
--form type=flat ///
--form filterLogic="[`fieldsource']<>''" ///
`apisource'
import delimited `tempexportfile', clear
rm `tempexportfile'
* loop through obs and upload file to destination
local N = _N
local thisrec
local thisfile
forvalues i = 1/`N' {
local thisrec=record_id[`i']
local thisfile=`fieldsource'[`i']
local destrec=`thisrec'
if "`thisfile'"!="" {
* download file from source record
di "Record `thisrec': downloading file `thisfile'"
shell curl ///
--output "`thisfile'" ///
--form token=`tokensource' ///
--form content=file ///
--form action=export ///
--form record="`thisrec'" ///
--form field=`fieldsource' ///
--form returnFomat=json ///
`apisource'
* upload file to destination record
di "Uploading file to destination record `destrec'"
shell curl ///
--form token=`tokendest' ///
--form content=file ///
--form action=import ///
--form record="`destrec'" ///
--form field=`fielddest' ///
--form filename="`thisfile'" ///
--form file=@"`thisfile'" ///
`apidest'
rm `thisfile'
}
}