@@ -2,7 +2,6 @@ module CDSAPI
22
33using HTTP
44using JSON
5- using Base64
65
76"""
87 retrieve(name, params, filename; max_sleep = 120.)
@@ -14,45 +13,46 @@ directory as `filename`.
1413The client periodically requests the status of the retrieve request.
1514`max_sleep` is the maximum time (in seconds) between the status updates.
1615"""
17- function retrieve (name, params, filename; max_sleep = 120. )
16+ function retrieve (name, params, filename; max_sleep= 120.0 )
1817 creds = Dict ()
19- open (joinpath (homedir ()," .cdsapirc" )) do f
18+ open (joinpath (homedir (), " .cdsapirc" )) do f
2019 for line in readlines (f)
21- key, val = strip .(split (line,' :' , limit= 2 ))
20+ key, val = strip .(split (line, ' :' , limit= 2 ))
2221 creds[key] = val
2322 end
2423 end
2524
26- apikey = string (" Basic " , base64encode (creds[" key" ]))
2725 response = HTTP. request (
2826 " POST" ,
29- creds[" url" ] * " /resources/ $name " ,
30- [" Authorization " => apikey ],
31- body= JSON. json (params),
27+ creds[" url" ] * " /retrieve/v1/processes/ $name /execute/ " ,
28+ [" PRIVATE-TOKEN " => creds[ " key " ] ],
29+ body= JSON. json (Dict ( " inputs " => params) ),
3230 verbose= 1 )
3331
3432 resp_dict = JSON. parse (String (response. body))
35- data = Dict (" state " => " queued" )
36- sleep_seconds = 1.
33+ data = Dict (" status " => " queued" )
34+ sleep_seconds = 1.0
3735
38- while data[" state " ] != " completed "
39- data = HTTP. request (" GET" , creds[" url" ] * " /tasks/ " * string (resp_dict[" request_id " ]), [ " Authorization " => apikey ])
36+ while data[" status " ] != " successful "
37+ data = HTTP. request (" GET" , creds[" url" ] * " /retrieve/v1/jobs/ " * string (resp_dict[" jobID " ]), [ " PRIVATE-TOKEN " => creds[ " key " ] ])
4038 data = JSON. parse (String (data. body))
41- println (" request queue status " , data[" state " ])
39+ println (" request queue status " , data[" status " ])
4240
43- if data[" state " ] == " failed"
41+ if data[" status " ] == " failed"
4442 error (" Request to dataset $name failed. Check " *
4543 " https://cds.climate.copernicus.eu/cdsapp#!/yourrequests " *
4644 " for more information (after login)." )
4745 end
4846
49- sleep_seconds = min (1.5 * sleep_seconds,max_sleep)
50- if data[" state " ] != " completed "
47+ sleep_seconds = min (1.5 * sleep_seconds, max_sleep)
48+ if data[" status " ] != " successful "
5149 sleep (sleep_seconds)
5250 end
5351 end
5452
55- HTTP. download (data[" location" ], filename)
53+ response = HTTP. request (" GET" , creds[" url" ] * " /retrieve/v1/jobs/" * string (resp_dict[" jobID" ]) * " /results/" , [" PRIVATE-TOKEN" => creds[" key" ]])
54+ body = JSON. parse (String (response. body))
55+ HTTP. download (body[" asset" ][" value" ][" href" ], filename)
5656 return data
5757end
5858
@@ -88,7 +88,7 @@ function py2ju(dictstr)
8888 # if there's no pair after the last comma
8989 if findnext (" :" , dictstr_cpy, lastcomma_pos) == nothing
9090 # remove the comma
91- dictstr_cpy = dictstr_cpy[firstindex (dictstr_cpy): (lastcomma_pos - 1 )] * dictstr_cpy[(lastcomma_pos + 1 ): lastindex (dictstr_cpy)]
91+ dictstr_cpy = dictstr_cpy[firstindex (dictstr_cpy): (lastcomma_pos- 1 )] * dictstr_cpy[(lastcomma_pos+ 1 ): lastindex (dictstr_cpy)]
9292 end
9393
9494 # removes trailing comma from a list
0 commit comments