Skip to content

Commit 0ed846d

Browse files
committed
clarified the readme and removed references to credentialsfromfile
renamed the scoped values to the uppercase variant
1 parent f2bb4f4 commit 0ed846d

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Please install the package with Julia's package manager:
1919
## Basic usage
2020
`CDSAPI.jl` will attempt to use CDS credentials using three different methods with the following priority:
2121

22-
1. direct credentials provided via a specific file
22+
1. direct credentials provided through the scoped values `KEY` and `URL`
2323
2. environmental variables `CDSAPI_URL` and `CDSAPI_KEY`
2424
3. default credential file in home directory `~/.cdsapirc`
2525

@@ -94,28 +94,31 @@ Dict{String,Any} with 6 entries:
9494
```
9595
# Multiple credentials
9696
In case you want to use multiple api-tokens for different requests, you can specify the token to use with each different request.
97-
To do so you can have multiple versions of a `.cdsapirc` file stored somewhere. These files must be in the same format as the classic `.cdsapirc` file.
9897

99-
To use them, parse them and pass the result to the scoped value `CDSAPI.auth`:
98+
Pass the desired values to the corresponding scoped values `CDSAPI.URL` and `CDSAPI.KEY`:
99+
If the `URL` or the `KEY` are not specified, the default values obtained from the default methods are used.
100100
```julia
101101
using CDSAPI
102102

103103
dataset = "reanalysis-era5-single-levels"
104104
request = """ #= some request =# """
105105

106-
_url1, _key1 = CDSAPI.credentialsfromfile("path/to/credential/file1")
107-
_url2, _key2 = CDSAPI.credentialsfromfile("path/to/credential/file2")
106+
customkey = "an-example-of-key"
107+
customurl = "http://my-custom-endpoint"
108108

109-
with( CDSAPI.url => _url1, CDSAPI.key => _key1 ) do
109+
# In this case the `URL` argument will be taken from the default locations
110+
# and only the KEY is overwritten.
111+
CDSAPI.with( CDSAPI.KEY => customkey ) do
110112
CDSAPI.retrieve(dataset, request, "download.nc")
111113
end
112114

113-
with( CDSAPI.url => _url2, CDSAPI.key => _key2 ) do
115+
# In this case is the other way around, so all requests will be made with the
116+
# credentials (customurl, defaultkey)
117+
CDSAPI.with( CDSAPI.URL => customurl) do
114118
CDSAPI.retrieve(dataset, request, "download.nc")
115119
end
116120
```
117121

118-
If the `url` or the `key` are not specified the default values obtained from the usual methods are used.
119122

120123
[build-img]: https://img.shields.io/github/actions/workflow/status/JuliaClimate/CDSAPI.jl/CI.yml?branch=master&style=flat-square
121124
[build-url]: https://github.com/JuliaClimate/CDSAPI.jl/actions

src/CDSAPI.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ using JSON
55

66
using ScopedValues
77

8-
9-
const key = ScopedValue("")
10-
const url = ScopedValue("")
8+
const KEY = ScopedValue("")
9+
const URL = ScopedValue("")
1110

1211
"""
1312
credentials()
@@ -27,29 +26,29 @@ function credentials()
2726

2827
dotrc = joinpath(homedir(), ".cdsapirc")
2928
if isfile(dotrc)
30-
_url, _key = credentialsfromfile(dotrc)
29+
url, key = credentialsfromfile(dotrc)
3130
else
32-
_url = _key = ""
31+
url = key = ""
3332
end
3433

3534
# overwrite with environmental variables
36-
_url = get(ENV, "CDSAPI_URL", _url)
37-
_key = get(ENV, "CDSAPI_KEY", _key)
35+
url = get(ENV, "CDSAPI_URL", url)
36+
key = get(ENV, "CDSAPI_KEY", key)
3837

3938
# overwrite with ScopedValues provided by user
4039

41-
_url = isempty(url[]) ? _url : url[]
42-
_key = isempty(key[]) ? _key : key[]
40+
url = isempty(URL[]) ? url : URL[]
41+
key = isempty(KEY[]) ? key : KEY[]
4342

4443

45-
if isempty(_url) || isempty(_key)
44+
if isempty(url) || isempty(key)
4645
error("""
4746
Missing credentials. Either add the CDSAPI_URL and CDSAPI_KEY env variables
4847
or create a .cdsapirc file (default location: '$(homedir())').
4948
""")
5049
end
5150

52-
return _url, _key
51+
return url, key
5352
end
5453

5554
"""

0 commit comments

Comments
 (0)