Skip to content

Commit 59f0f51

Browse files
committed
Update CDS retreival to new API
1 parent 4aafbdb commit 59f0f51

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
name = "CDSAPI"
22
uuid = "8a7b9de3-9c00-473e-88b4-7eccd7ef2fea"
33
authors = ["Micky Yun Chan <michan@redhat.com> and contributors"]
4-
version = "1.0.1"
4+
version = "2.0.0"
55

66
[deps]
7-
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
87
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
98
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
109

src/CDSAPI.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module CDSAPI
22

33
using HTTP
44
using JSON
5-
using Base64
65

76
"""
87
retrieve(name, params, filename; max_sleep = 120.)
@@ -14,45 +13,46 @@ directory as `filename`.
1413
The 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
5757
end
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

Comments
 (0)