@sergiy ,
See the full code below:
1. Define API
apiGenerate <- sprintf("%s/api/v2/export/", sserver)
2. Generate file in the server
response_generate = POST(apiGenerate, authenticate(ssuser, sspassword), body = list(ExportType= ex_format, QuestionnaireId = quid, InterviewStatus = interview_status ), encode = "json" )
Wait 5 seconds
Sys.sleep(5)
3. Get the links and JobIDs generated in the system
#get the links of all files generated in the system
Myjson <- tempfile(fileext = ".json")
response_link = GET(apiGenerate,
authenticate(ssuser, sspassword),
query = list(exportType= ex_format,
questionnaireIdentity = quid,
interviewStatus = interview_status
),
write_disk(Myjson, overwrite = T)
)
3.1 arrange link by date so the latest file generated is exported (I am still working on this part to improve the syntax but this is the part where I verify that I am getting the latest file. I know that I can work with the jobID but I am still working on the code)
#arrange the links by date
response_json_link <- jsonlite::fromJSON(Myjson) %>%
mutate(CompleteDate = str_replace(CompleteDate,"T", " "),
CompleteDate = str_remove(CompleteDate, "\\.[^.]*$"),
CompleteDate = lubridate::ymd_hms(CompleteDate)) %>%
arrange(desc(CompleteDate))
#get the latest link
apiExport = response_json_link$Links$Download[1]
4. Export the data
response_export <- GET(apiExport, authenticate(ssuser, sspassword), user_agent(“andres.arau@outlook.com”))
REMEMBER THAT THIS IS WORK ON PROGRESS AND THAS SOME WORK IS TO BE DONE TO IMPROVE THE APPROACH TO TEST THAT THE JOB HAS BEEN COMPLETED. ALSO IMPROVABLE IS THE STRATEGY TO VERIFY THAT I AM GETTING THE EXPECTED JOBID (THIS CAN BE DONE MUCH MORE EFFECTIVELY… I AM WORKING ON IT)
The inputs of other R users would be great to learn how to make this process more efficient.