-
Notifications
You must be signed in to change notification settings - Fork 6
/
pip_get.ado
83 lines (67 loc) · 2.61 KB
/
pip_get.ado
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
/*==================================================
project: Get data from API of from cache
Author: R.Andres Castaneda
----------------------------------------------------
Creation Date: 12 May 2023 - 11:32:30
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pip_get, rclass
version 16.1
syntax , [ ///
clear ///
cacheforce ///
gname(string) ///
cachedir(passthru) ///
]
if (c(changed) != 1 & "`clear'" == "") error 4
if ("`gname'" == "") local gname pip_last_queries
tempname tempframe
frame create `tempframe'
qui frame `tempframe' {
tempfile fpip_get
save `fpip_get', empty
foreach query of global `gname' {
local queryfull "${pip_host}/`query'"
pip_cache load, query("`queryfull'") `cacheforce' `clear' `cachedir'
local pc_exists = "`r(pc_exists)'"
local piphash = "`r(piphash)'"
if ("`pc_exists'" == "0" | "${pip_cachedir}" == "0") {
cap import delimited "`queryfull'", `clear' varn(1) asdouble
if (_rc) noi pip_download_err_msg "`queryfull'"
pip_cache save, piphash("`piphash'") query("`queryfull'") /*
*/ `cacheforce' `cachedir'
}
append using `fpip_get'
save `fpip_get', replace
}
}
frame copy `tempframe' `c(frame)', replace
end
//========================================================
// auxiliary Programs
//========================================================
//------------ Error messages when downloading
program define pip_download_err_msg
args query
local json: subinstr local query "&format=csv" ""
local trouble "https://github.com/worldbank/pip#troubleshooting"
noi {
dis "{pstd}{err}It was not possible to download data from the PIP API.{p_end}"
dis "{pstd}{err}Please, troubleshoot in the following order:{p_end}"
dis "{phang2}{res}(1) {txt}Check your Internet connection by " /*
*/ `"{browse "${pip_host}/health-check" :clicking here}"' "{p_end}"
dis "{phang2}{res}(2) {txt}Check that you can see the data in " /*
*/ `"{browse "`json'" :JSON }"' "format in your browser and " /*
*/ "that you can download the data in " `"{browse "`query'" :csv }"' "{p_end}"
dis "{phang2}{res}(3) {txt}Consider adjusting your Stata timeout parameters. For more details see {help netio}{p_end}"
dis "{phang2}{res}(4) {txt}Follow the instructions " /*
*/ `"{browse "`trouble'" :here}"' ".{p_end}"
di ""
}
error 673
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><