Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for passing directly the python dict string as response body #59

Merged
merged 18 commits into from
Jan 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/CDSAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ directory as `filename`.
The client periodically requests the status of the retrieve request.
`wait` is the maximum time (in seconds) between status updates.
"""
function retrieve(name, params, filename; wait=1.0)
function retrieve(name, params, filename; wait=1.0) end

function retrieve(name, params::AbstractDict, filename; wait=1.0)
creds = Dict()
open(joinpath(homedir(), ".cdsapirc")) do f
for line in readlines(f)
Expand All @@ -27,12 +29,12 @@ function retrieve(name, params, filename; wait=1.0)
["PRIVATE-TOKEN" => creds["key"]],
body=JSON.json(Dict("inputs" => params))
)
body = JSON.parse(String(response.body))
data = Dict("status" => "queued")

data = JSON.parse(String(response.body))
job_endpoint = Dict(response.headers)["location"]
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved

while data["status"] != "successful"
data = HTTP.request("GET",
creds["url"] * "/retrieve/v1/jobs/" * string(body["jobID"]),
data = HTTP.request("GET", job_endpoint,
["PRIVATE-TOKEN" => creds["key"]]
)
data = JSON.parse(String(data.body))
Expand All @@ -53,7 +55,7 @@ function retrieve(name, params, filename; wait=1.0)
end

response = HTTP.request("GET",
creds["url"] * "/retrieve/v1/jobs/" * string(body["jobID"]) * "/results",
job_endpoint * "/results",
["PRIVATE-TOKEN" => creds["key"]]
)
body = JSON.parse(String(response.body))
Expand All @@ -62,6 +64,9 @@ function retrieve(name, params, filename; wait=1.0)
return data
end

retrieve(name, params::String, filename; wait=1.0) =
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved
retrieve(name, JSON.parse(params), filename; wait)

"""
py2ju(dictstr)

Expand Down