gdex-api-client 2.0.8__tar.gz → 2.0.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gdex-api-client
3
- Version: 2.0.8
3
+ Version: 2.0.9
4
4
  Summary: CLI and library for calling GDEX API
5
5
  Author-email: GDEX data Help <datahelp@ucar.edu>
6
6
  License-Expression: MIT
7
- Project-URL: Homepage, https://gdex.ucar/edu
7
+ Project-URL: Homepage, https://gdex.ucar.edu
8
8
  Keywords: API,Geoscience,GDEX
9
9
  Description-Content-Type: text/markdown
10
10
  License-File: LICENSE
@@ -15,7 +15,7 @@ Dynamic: license-file
15
15
 
16
16
  This repository contains the Geoscience Data Exchange (GDEX) API client for python.
17
17
 
18
- At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x), see [src/python](src/python) for more details.
18
+ At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x), see [src/gdex_api_client](../src/gdex_api_client) for more details.
19
19
 
20
20
 
21
21
  # GDEX External Application API Description
@@ -45,10 +45,10 @@ At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x
45
45
  + The client can be used as a command-line tool or as an imported python module to work with python objects directly.
46
46
  + In most cases, the client will print the JSON response after a command is entered.
47
47
  + A Jupyter Notebook was created to show the basic functionality of gdex_client.py.
48
- - [The Jupyter Notebook can be found here](../src/python/gdex_client_example.ipynb)
49
- + [gdex_client.py can be found here](../src/python/gdex_client.py)
48
+ - [The Jupyter Notebook can be found here](../src/gdex_api_client/gdex_client_example.ipynb)
49
+ + [gdex_client.py can be found here](../src/gdex_api_client/gdex_client.py)
50
50
  - All HTTPS requests to to the GDEX API must include a bearer token to access the API.
51
- + Token information can be found on [you user profile](https://gdex.ucar.edu/accounts/profile)
51
+ + Token information can be found on [your user profile](https://gdex.ucar.edu/accounts/profile)
52
52
  - Responses are JSON formatted.
53
53
  + Typically, error responses will give an explaination of what went wrong in the value of the `messages` key.
54
54
  + If `status` is `ok`, then all relevant data will be in the value of the `result` key.
@@ -70,7 +70,7 @@ Additionally, you can programatically use the module via
70
70
  ### Authentication
71
71
 
72
72
  `POST`, `DELETE`, and some user-specific 'GET' HTTP requests require that you include a bearer token in your URL.
73
- To do this you would need to first find your token in [you user profile](https://gdex.ucar.edu/accounts/profile).
73
+ To do this you would need to first find your token in [your user profile](https://gdex.ucar.edu/accounts/profile).
74
74
 
75
75
  Next, you would append this token to the end of any url using `?token=[bearer token]. For example,
76
76
  ```
@@ -89,7 +89,7 @@ The list below is how to perform HTTPS `GET`, `POST`, and `DELETE` commands usin
89
89
 
90
90
  curl -X DELETE [URL]
91
91
 
92
- URL in these examples could be for example `https://gdex.ucar.edu/api/summary/ds083.2` or `https://gdex.ucar.edu/api/status` or `https://gdex.ucar.edu/api/get_req_files/123456`
92
+ URL in these examples could be for example `https://gdex.ucar.edu/api/summary/d083002` or `https://gdex.ucar.edu/api/status` or `https://gdex.ucar.edu/api/get_req_files/123456`
93
93
 
94
94
  ------
95
95
 
@@ -103,7 +103,7 @@ The list below is how to perform HTTPS `GET`, `POST`, and `DELETE` commands usin
103
103
 
104
104
  wget --method=delete [URL]
105
105
 
106
- URL in these case could be for example `https://gdex.ucar.edu/api/summary/ds083.2` or `https://gdex.ucar.edu/api/request` or `https://gdex.ucar.edu/api/request/123456`
106
+ URL in these case could be for example `https://gdex.ucar.edu/api/summary/d083002` or `https://gdex.ucar.edu/api/request` or `https://gdex.ucar.edu/api/request/123456`
107
107
 
108
108
  ------
109
109
 
@@ -118,7 +118,7 @@ Returns a summary of datasets and dataset groups that have subsetting available.
118
118
  #### URL
119
119
 
120
120
  ```
121
- GET https://gdex.ucar.edu/api/summary/[dsnnn.n]
121
+ GET https://gdex.ucar.edu/api/summary/[dnnnnnn]
122
122
  ```
123
123
 
124
124
  #### Example Response
@@ -162,7 +162,7 @@ Returns a summary of only the Parameters in a dataset for subsetting.
162
162
  #### URL
163
163
 
164
164
  ```
165
- GET https://gdex.ucar.edu/api/paramsummary/[dsnnn.n]
165
+ GET https://gdex.ucar.edu/api/paramsummary/[dnnnnnn]
166
166
  ```
167
167
 
168
168
  #### Example Response
@@ -173,7 +173,7 @@ GET https://gdex.ucar.edu/api/paramsummary/[dsnnn.n]
173
173
  "https_response": 200,
174
174
  "messages": [],
175
175
  "result": {
176
- "dsid": "083.2",
176
+ "dsid": "d083002",
177
177
  "subsetting_available": true,
178
178
  "data": [
179
179
  {
@@ -243,7 +243,7 @@ Returns full metadata of a dataset available for subsetting.
243
243
  #### URL
244
244
 
245
245
  ```
246
- GET https://gdex.ucar.edu/api/metadata/[dsnnn.n]
246
+ GET https://gdex.ucar.edu/api/metadata/[dnnnnnn]
247
247
  ```
248
248
 
249
249
  #### Example Response
@@ -449,7 +449,7 @@ GET https://gdex.ucar.edu/api/metadata/[dsnnn.n]
449
449
  #### Description
450
450
 
451
451
  Returns that status of a given request index.
452
- A request index is a six digit integer.
452
+ A request index is a seven digit integer.
453
453
 
454
454
  An authentication token is needed for this for this request. See [Authentication Section](#authentication) for more details.
455
455
 
@@ -482,10 +482,10 @@ If `[RequestIndex]` is given
482
482
  {
483
483
  "status": "Completed",
484
484
  "date_ready": "2020-03-01",
485
- "request_index": 410935,
485
+ "request_index": 4109350,
486
486
  "NCAR_contact": "rpconroy@ucar.edu",
487
- "rqstid": "LASTNAME410935",
488
- "request_id": "LASTNAME410935",
487
+ "rqstid": "LASTNAME4109350",
488
+ "request_id": "LASTNAME4109350",
489
489
  "date_purge": "2020-03-06",
490
490
  "date_rqst": "2020-03-01",
491
491
  "subset_info": {
@@ -509,10 +509,10 @@ Or if `[RequestIndex]` is not specified, get all requests
509
509
  {
510
510
  "status": "Completed",
511
511
  "date_ready": "2020-03-01",
512
- "request_index": 410935,
512
+ "request_index": 4109350,
513
513
  "NCAR_contact": "rpconroy@ucar.edu",
514
- "rqstid": "LASTNAME410935",
515
- "request_id": "LASTNAME410935",
514
+ "rqstid": "LASTNAME4109350",
515
+ "request_id": "LASTNAME4109350",
516
516
  "date_purge": "2020-03-06",
517
517
  "date_rqst": "2020-03-01",
518
518
  "subset_info": {
@@ -522,10 +522,10 @@ Or if `[RequestIndex]` is not specified, get all requests
522
522
  {
523
523
  "status": "Completed",
524
524
  "date_ready": "2020-03-01",
525
- "request_index": 410936,
525
+ "request_index": 4109360,
526
526
  "NCAR_contact": "rpconroy@ucar.edu",
527
- "rqstid": "LASTNAME410936",
528
- "request_id": "LASTNAME410936",
527
+ "rqstid": "LASTNAME4109360",
528
+ "request_id": "LASTNAME4109360",
529
529
  "date_purge": "2020-03-06",
530
530
  "date_rqst": "2020-03-01",
531
531
  "subset_info": {
@@ -565,7 +565,7 @@ GET https://gdex.ucar.edu/api/get_req_files/[RequestIndex]
565
565
  "total_size": 156,
566
566
  "web_files": [
567
567
  {
568
- "web_path": "https://gdex.ucar.edu/dsrqst/LASTNAME411039/Filename.extension",
568
+ "web_path": "https://gdex.ucar.edu/dsrqst/LASTNAME4110390/Filename.extension",
569
569
  "wfile": "Filename.extension",
570
570
  "size": 156
571
571
  }
@@ -598,7 +598,7 @@ GET https://gdex.ucar.edu/api/control_file_template/[dsxxx.x]
598
598
  "https_response": 200,
599
599
  "messages": [],
600
600
  "result": {
601
- "template": "dataset=ds083.2\ndate=201103020000/to/201103151200\nparam=TMP/R H/ABS V\nlevel=ISBL:850/700/500\noformat=netCDF\nnlat=30\nslat=-25\nwlon=-150\nelon=-30\n#groupindex=2\ntargetdir=/glade/scratch\n"
601
+ "template": "dataset=d083002\ndate=201103020000/to/201103151200\nparam=TMP/R H/ABS V\nlevel=ISBL:850/700/500\noformat=netCDF\nnlat=30\nslat=-25\nwlon=-150\nelon=-30\n#groupindex=2\ntargetdir=/glade/scratch\n"
602
602
  },
603
603
  "request_end": "2020-03-03T11:44:21.842582",
604
604
  "request_start": "2020-03-03T11:44:21.835081"
@@ -631,7 +631,7 @@ POST https://gdex.ucar.edu/api/submit
631
631
  "https_response": 200,
632
632
  "messages": [],
633
633
  "result": {
634
- "request_id": "411298"
634
+ "request_id": "4112980"
635
635
  },
636
636
  "request_end": "2020-03-03T11:45:16.513226",
637
637
  "request_start": "2020-03-03T11:45:15.341967"
@@ -2,7 +2,7 @@
2
2
 
3
3
  This repository contains the Geoscience Data Exchange (GDEX) API client for python.
4
4
 
5
- At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x), see [src/python](src/python) for more details.
5
+ At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x), see [src/gdex_api_client](../src/gdex_api_client) for more details.
6
6
 
7
7
 
8
8
  # GDEX External Application API Description
@@ -32,10 +32,10 @@ At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x
32
32
  + The client can be used as a command-line tool or as an imported python module to work with python objects directly.
33
33
  + In most cases, the client will print the JSON response after a command is entered.
34
34
  + A Jupyter Notebook was created to show the basic functionality of gdex_client.py.
35
- - [The Jupyter Notebook can be found here](../src/python/gdex_client_example.ipynb)
36
- + [gdex_client.py can be found here](../src/python/gdex_client.py)
35
+ - [The Jupyter Notebook can be found here](../src/gdex_api_client/gdex_client_example.ipynb)
36
+ + [gdex_client.py can be found here](../src/gdex_api_client/gdex_client.py)
37
37
  - All HTTPS requests to to the GDEX API must include a bearer token to access the API.
38
- + Token information can be found on [you user profile](https://gdex.ucar.edu/accounts/profile)
38
+ + Token information can be found on [your user profile](https://gdex.ucar.edu/accounts/profile)
39
39
  - Responses are JSON formatted.
40
40
  + Typically, error responses will give an explaination of what went wrong in the value of the `messages` key.
41
41
  + If `status` is `ok`, then all relevant data will be in the value of the `result` key.
@@ -57,7 +57,7 @@ Additionally, you can programatically use the module via
57
57
  ### Authentication
58
58
 
59
59
  `POST`, `DELETE`, and some user-specific 'GET' HTTP requests require that you include a bearer token in your URL.
60
- To do this you would need to first find your token in [you user profile](https://gdex.ucar.edu/accounts/profile).
60
+ To do this you would need to first find your token in [your user profile](https://gdex.ucar.edu/accounts/profile).
61
61
 
62
62
  Next, you would append this token to the end of any url using `?token=[bearer token]. For example,
63
63
  ```
@@ -76,7 +76,7 @@ The list below is how to perform HTTPS `GET`, `POST`, and `DELETE` commands usin
76
76
 
77
77
  curl -X DELETE [URL]
78
78
 
79
- URL in these examples could be for example `https://gdex.ucar.edu/api/summary/ds083.2` or `https://gdex.ucar.edu/api/status` or `https://gdex.ucar.edu/api/get_req_files/123456`
79
+ URL in these examples could be for example `https://gdex.ucar.edu/api/summary/d083002` or `https://gdex.ucar.edu/api/status` or `https://gdex.ucar.edu/api/get_req_files/123456`
80
80
 
81
81
  ------
82
82
 
@@ -90,7 +90,7 @@ The list below is how to perform HTTPS `GET`, `POST`, and `DELETE` commands usin
90
90
 
91
91
  wget --method=delete [URL]
92
92
 
93
- URL in these case could be for example `https://gdex.ucar.edu/api/summary/ds083.2` or `https://gdex.ucar.edu/api/request` or `https://gdex.ucar.edu/api/request/123456`
93
+ URL in these case could be for example `https://gdex.ucar.edu/api/summary/d083002` or `https://gdex.ucar.edu/api/request` or `https://gdex.ucar.edu/api/request/123456`
94
94
 
95
95
  ------
96
96
 
@@ -105,7 +105,7 @@ Returns a summary of datasets and dataset groups that have subsetting available.
105
105
  #### URL
106
106
 
107
107
  ```
108
- GET https://gdex.ucar.edu/api/summary/[dsnnn.n]
108
+ GET https://gdex.ucar.edu/api/summary/[dnnnnnn]
109
109
  ```
110
110
 
111
111
  #### Example Response
@@ -149,7 +149,7 @@ Returns a summary of only the Parameters in a dataset for subsetting.
149
149
  #### URL
150
150
 
151
151
  ```
152
- GET https://gdex.ucar.edu/api/paramsummary/[dsnnn.n]
152
+ GET https://gdex.ucar.edu/api/paramsummary/[dnnnnnn]
153
153
  ```
154
154
 
155
155
  #### Example Response
@@ -160,7 +160,7 @@ GET https://gdex.ucar.edu/api/paramsummary/[dsnnn.n]
160
160
  "https_response": 200,
161
161
  "messages": [],
162
162
  "result": {
163
- "dsid": "083.2",
163
+ "dsid": "d083002",
164
164
  "subsetting_available": true,
165
165
  "data": [
166
166
  {
@@ -230,7 +230,7 @@ Returns full metadata of a dataset available for subsetting.
230
230
  #### URL
231
231
 
232
232
  ```
233
- GET https://gdex.ucar.edu/api/metadata/[dsnnn.n]
233
+ GET https://gdex.ucar.edu/api/metadata/[dnnnnnn]
234
234
  ```
235
235
 
236
236
  #### Example Response
@@ -436,7 +436,7 @@ GET https://gdex.ucar.edu/api/metadata/[dsnnn.n]
436
436
  #### Description
437
437
 
438
438
  Returns that status of a given request index.
439
- A request index is a six digit integer.
439
+ A request index is a seven digit integer.
440
440
 
441
441
  An authentication token is needed for this for this request. See [Authentication Section](#authentication) for more details.
442
442
 
@@ -469,10 +469,10 @@ If `[RequestIndex]` is given
469
469
  {
470
470
  "status": "Completed",
471
471
  "date_ready": "2020-03-01",
472
- "request_index": 410935,
472
+ "request_index": 4109350,
473
473
  "NCAR_contact": "rpconroy@ucar.edu",
474
- "rqstid": "LASTNAME410935",
475
- "request_id": "LASTNAME410935",
474
+ "rqstid": "LASTNAME4109350",
475
+ "request_id": "LASTNAME4109350",
476
476
  "date_purge": "2020-03-06",
477
477
  "date_rqst": "2020-03-01",
478
478
  "subset_info": {
@@ -496,10 +496,10 @@ Or if `[RequestIndex]` is not specified, get all requests
496
496
  {
497
497
  "status": "Completed",
498
498
  "date_ready": "2020-03-01",
499
- "request_index": 410935,
499
+ "request_index": 4109350,
500
500
  "NCAR_contact": "rpconroy@ucar.edu",
501
- "rqstid": "LASTNAME410935",
502
- "request_id": "LASTNAME410935",
501
+ "rqstid": "LASTNAME4109350",
502
+ "request_id": "LASTNAME4109350",
503
503
  "date_purge": "2020-03-06",
504
504
  "date_rqst": "2020-03-01",
505
505
  "subset_info": {
@@ -509,10 +509,10 @@ Or if `[RequestIndex]` is not specified, get all requests
509
509
  {
510
510
  "status": "Completed",
511
511
  "date_ready": "2020-03-01",
512
- "request_index": 410936,
512
+ "request_index": 4109360,
513
513
  "NCAR_contact": "rpconroy@ucar.edu",
514
- "rqstid": "LASTNAME410936",
515
- "request_id": "LASTNAME410936",
514
+ "rqstid": "LASTNAME4109360",
515
+ "request_id": "LASTNAME4109360",
516
516
  "date_purge": "2020-03-06",
517
517
  "date_rqst": "2020-03-01",
518
518
  "subset_info": {
@@ -552,7 +552,7 @@ GET https://gdex.ucar.edu/api/get_req_files/[RequestIndex]
552
552
  "total_size": 156,
553
553
  "web_files": [
554
554
  {
555
- "web_path": "https://gdex.ucar.edu/dsrqst/LASTNAME411039/Filename.extension",
555
+ "web_path": "https://gdex.ucar.edu/dsrqst/LASTNAME4110390/Filename.extension",
556
556
  "wfile": "Filename.extension",
557
557
  "size": 156
558
558
  }
@@ -585,7 +585,7 @@ GET https://gdex.ucar.edu/api/control_file_template/[dsxxx.x]
585
585
  "https_response": 200,
586
586
  "messages": [],
587
587
  "result": {
588
- "template": "dataset=ds083.2\ndate=201103020000/to/201103151200\nparam=TMP/R H/ABS V\nlevel=ISBL:850/700/500\noformat=netCDF\nnlat=30\nslat=-25\nwlon=-150\nelon=-30\n#groupindex=2\ntargetdir=/glade/scratch\n"
588
+ "template": "dataset=d083002\ndate=201103020000/to/201103151200\nparam=TMP/R H/ABS V\nlevel=ISBL:850/700/500\noformat=netCDF\nnlat=30\nslat=-25\nwlon=-150\nelon=-30\n#groupindex=2\ntargetdir=/glade/scratch\n"
589
589
  },
590
590
  "request_end": "2020-03-03T11:44:21.842582",
591
591
  "request_start": "2020-03-03T11:44:21.835081"
@@ -618,7 +618,7 @@ POST https://gdex.ucar.edu/api/submit
618
618
  "https_response": 200,
619
619
  "messages": [],
620
620
  "result": {
621
- "request_id": "411298"
621
+ "request_id": "4112980"
622
622
  },
623
623
  "request_end": "2020-03-03T11:45:16.513226",
624
624
  "request_start": "2020-03-03T11:45:15.341967"
@@ -3,7 +3,7 @@ requires = ["setuptools"]
3
3
  build-backend = "setuptools.build_meta"
4
4
  [project]
5
5
  name = "gdex-api-client"
6
- version = "2.0.8"
6
+ version = "2.0.9"
7
7
  dependencies = [
8
8
  "requests",
9
9
  ]
@@ -15,6 +15,6 @@ readme = "docs/README.md"
15
15
  license = "MIT"
16
16
  keywords = ["API", "Geoscience", "GDEX"]
17
17
  [project.urls]
18
- Homepage = "https://gdex.ucar/edu"
18
+ Homepage = "https://gdex.ucar.edu"
19
19
  [project.scripts]
20
20
  gdex_client = "gdex_api_client.gdex_client:main"
@@ -15,18 +15,16 @@ gdex_client.py -get_control_file_template <dsnnn.n>
15
15
  gdex_client.py -help
16
16
  ```
17
17
  """
18
- __version__ = '2.0.0'
18
+ __version__ = '2.0.9'
19
19
  __author__ = 'Doug Schuster (schuster@ucar.edu), Riley Conroy (rpconroy@ucar.edu)'
20
20
 
21
- import sys
22
- import os
23
- import requests
24
- import getpass
25
- import json
26
21
  import argparse
27
- import codecs
28
- import pdb
22
+ import json
23
+ import os
29
24
  import re
25
+ import sys
26
+
27
+ import requests
30
28
 
31
29
 
32
30
  BASE_URL = 'https://gdex.ucar.edu/api/'
@@ -49,7 +47,7 @@ def query(args=None):
49
47
 
50
48
  Example:
51
49
  ```
52
- >>> query(['-get_status', '123456'])
50
+ >>> query(['-get_status', '1234567'])
53
51
 
54
52
  >>> query(['-get_metadata', 'd083002'])
55
53
  ```
@@ -111,18 +109,18 @@ def read_control_file(control_file):
111
109
  Returns:
112
110
  (dict) python dict representing control file.
113
111
  """
114
- control_params = {}
115
112
  if os.path.exists(control_file):
116
- myfile = open(control_file, 'r')
113
+ with open(control_file, 'r') as fh:
114
+ lines = fh.read().splitlines()
117
115
  else:
118
- myfile = control_file.split('\n')
116
+ lines = control_file.split('\n')
119
117
 
120
- for line in myfile:
118
+ control_params = {}
119
+ for line in lines:
121
120
  line = line.strip()
122
121
  if line.startswith('#') or line == "":
123
122
  continue
124
- li = line.rstrip()
125
- (key, value) = li.split('=', 2)
123
+ key, value = line.split('=', 1)
126
124
  control_params[key] = value
127
125
 
128
126
  # Handle empty params
@@ -130,10 +128,6 @@ def read_control_file(control_file):
130
128
  all_params = get_all_params(control_params['dataset'])
131
129
  control_params['param'] = '/'.join(all_params)
132
130
 
133
- try:
134
- myfile.close()
135
- except:
136
- pass
137
131
  return control_params
138
132
 
139
133
  def get_parser():
@@ -179,7 +173,7 @@ def get_parser():
179
173
  const='ALL',
180
174
  metavar='<Request Index>',
181
175
  required=False,
182
- help="Get a summary of the given dataset.")
176
+ help="Get the status of a request (all open requests if omitted).")
183
177
  group.add_argument('-download', '-d',
184
178
  type=str,
185
179
  required=False,
@@ -221,7 +215,7 @@ def check_status(ret, token_file=DEFAULT_AUTH_FILE):
221
215
  """
222
216
  if ret.status_code == 401: # Not Authorized
223
217
  print(ret.content)
224
- exit(1)
218
+ sys.exit(1)
225
219
 
226
220
  def check_file_status(filepath, filesize):
227
221
  """Prints file download status as percent of file complete.
@@ -240,7 +234,7 @@ def check_file_status(filepath, filesize):
240
234
  sys.stdout.write('%.3f %s' % (percent_complete, '% Completed'))
241
235
  sys.stdout.flush()
242
236
 
243
- def download_files(filelist, out_dir='./', cookie_file=None):
237
+ def download_files(filelist, out_dir='./'):
244
238
  """Download files in a list.
245
239
 
246
240
  Args:
@@ -252,7 +246,7 @@ def download_files(filelist, out_dir='./', cookie_file=None):
252
246
  """
253
247
  for _file in filelist:
254
248
  file_base = os.path.basename(_file)
255
- out_file = out_dir + file_base
249
+ out_file = os.path.join(out_dir, file_base)
256
250
  print('Downloading',file_base)
257
251
  header = requests.head(_file, allow_redirects=True, stream=True)
258
252
  filesize = int(header.headers['Content-Length'])
@@ -283,6 +277,22 @@ def get_authentication(token_file=DEFAULT_AUTH_FILE):
283
277
  else:
284
278
  return get_userinfo()
285
279
 
280
+ def _api_request(endpoint, method='get', **kwargs):
281
+ """Send an authenticated request to the API and return the decoded JSON.
282
+
283
+ Args:
284
+ endpoint (str): Path appended to BASE_URL, e.g. 'summary/d083002'.
285
+ method (str): HTTP method. Defaults to 'get'.
286
+ **kwargs: Passed through to requests.request (e.g. json=...).
287
+
288
+ Returns:
289
+ dict: JSON decoded result of the query.
290
+ """
291
+ url = encode_url(BASE_URL + endpoint, get_authentication())
292
+ ret = requests.request(method, url, **kwargs)
293
+ check_status(ret)
294
+ return ret.json()
295
+
286
296
 
287
297
  def get_summary(ds):
288
298
  """Returns summary of dataset.
@@ -293,14 +303,7 @@ def get_summary(ds):
293
303
  Returns:
294
304
  dict: JSON decoded result of the query.
295
305
  """
296
- url = BASE_URL + 'summary/'
297
- url += ds
298
-
299
- token = get_authentication()
300
- ret = requests.get(encode_url(url,token))
301
-
302
- check_status(ret)
303
- return ret.json()
306
+ return _api_request('summary/' + ds)
304
307
 
305
308
  def get_metadata(ds):
306
309
  """Return metadata of dataset.
@@ -311,14 +314,7 @@ def get_metadata(ds):
311
314
  Returns:
312
315
  dict: JSON decoded result of the query.
313
316
  """
314
- url = BASE_URL + 'metadata/'
315
- url += ds
316
-
317
- token = get_authentication()
318
- ret = requests.get(encode_url(url,token))
319
-
320
- check_status(ret)
321
- return ret.json()
317
+ return _api_request('metadata/' + ds)
322
318
 
323
319
  def get_all_params(ds):
324
320
  """Return set of parameters for a dataset.
@@ -346,14 +342,7 @@ def get_param_summary(ds):
346
342
  Returns:
347
343
  dict: JSON decoded result of the query.
348
344
  """
349
- url = BASE_URL + 'paramsummary/'
350
- url += ds
351
-
352
- token = get_authentication()
353
- ret = requests.get(encode_url(url,token))
354
-
355
- check_status(ret)
356
- return ret.json()
345
+ return _api_request('paramsummary/' + ds)
357
346
 
358
347
 
359
348
  def submit_json(json_file):
@@ -373,13 +362,7 @@ def submit_json(json_file):
373
362
  assert type(json_file) is dict
374
363
  control_dict = json_file
375
364
 
376
- url = BASE_URL + 'submit/'
377
-
378
- token = get_authentication()
379
- ret = requests.post(encode_url(url,token), json=control_dict)
380
-
381
- check_status(ret)
382
- return ret.json()
365
+ return _api_request('submit/', method='post', json=control_dict)
383
366
 
384
367
  def submit(control_file_name):
385
368
  """Submit a GDEX subset or format conversion request.
@@ -400,84 +383,56 @@ def get_status(request_idx=None):
400
383
  If request_ix not provided, get all open requests
401
384
 
402
385
  Args:
403
- request_idx (str, Optional): Request Index, typcally a 6-digit integer.
386
+ request_idx (str, Optional): Request Index, typcally a 7-digit integer.
404
387
 
405
388
  Returns:
406
389
  dict: JSON decoded result of the query.
407
390
  """
408
391
  if request_idx is None:
409
392
  request_idx = 'ALL'
410
- url = BASE_URL + 'status/'
411
- url += str(request_idx)
412
-
413
-
414
- token = get_authentication()
415
- ret = requests.get(encode_url(url,token))
416
-
417
- check_status(ret)
418
- return ret.json()
393
+ return _api_request('status/' + str(request_idx))
419
394
 
420
395
  def get_filelist(request_idx):
421
396
  """Gets filelist for request
422
397
 
423
398
  Args:
424
- request_idx (str): Request Index, typically a 6-digit integer.
399
+ request_idx (str): Request Index, typically a 7-digit integer.
425
400
 
426
401
  Returns:
427
402
  dict: JSON decoded result of the query.
428
403
  """
429
- url = BASE_URL + 'get_req_files/'
430
- url += str(request_idx)
431
-
432
- token = get_authentication()
433
- ret = requests.get(encode_url(url,token))
434
-
435
- check_status(ret)
436
-
437
- return ret.json()
404
+ return _api_request('get_req_files/' + str(request_idx))
438
405
 
439
406
 
440
407
  def download(request_idx, out_dir='./'):
441
408
  """Download files given request Index
442
409
 
443
410
  Args:
444
- request_idx (str): Request Index, typically a 6-digit integer.
411
+ request_idx (str): Request Index, typically a 7-digit integer.
412
+ out_dir (str): Directory to put downloaded files.
445
413
 
446
414
  Returns:
447
- None
415
+ dict: JSON decoded filelist result of the query.
448
416
  """
449
417
  ret = get_filelist(request_idx)
450
418
  if len(ret['data']) == 0:
451
419
  return ret
452
420
 
453
- filelist = ret['data']['web_files']
454
-
455
- token = get_authentication()
456
-
457
- web_files = list(map(lambda x: x['web_path'], filelist))
458
-
459
421
  # Only download unique files.
460
- download_files(set(web_files), out_dir)
422
+ web_files = {f['web_path'] for f in ret['data']['web_files']}
423
+ download_files(web_files, out_dir)
461
424
  return ret
462
425
 
463
426
  def globus_download(request_idx):
464
427
  """Begin a globus transfer.
465
428
 
466
429
  Args:
467
- request_ix (str): Request Index, typically a 6-digit integer.
430
+ request_ix (str): Request Index, typically a 7-digit integer.
468
431
 
469
432
  Returns:
470
433
  dict: JSON decoded result of the query.
471
434
  """
472
- url = BASE_URL + 'request/'
473
- url += request_idx
474
- url += '-globus_download'
475
-
476
- token = get_authentication()
477
- ret = requests.get(encode_url(url,token))
478
-
479
- check_status(ret)
480
- return ret.json()
435
+ return _api_request('request/' + str(request_idx) + '-globus_download')
481
436
 
482
437
  def get_control_file_template(ds):
483
438
  """Write a control file for use in subset requests.
@@ -488,14 +443,7 @@ def get_control_file_template(ds):
488
443
  Returns:
489
444
  dict: JSON decoded result of the query.
490
445
  """
491
- url = BASE_URL + 'control_file_template/'
492
- url += ds
493
-
494
- token = get_authentication()
495
- ret = requests.get(encode_url(url,token))
496
-
497
- check_status(ret)
498
- return ret.json()
446
+ return _api_request('control_file_template/' + ds)
499
447
 
500
448
  def write_control_file_template(ds, write_location='./'):
501
449
  """Write a control file for use in subset requests.
@@ -512,34 +460,25 @@ def write_control_file_template(ds, write_location='./'):
512
460
  _json = get_control_file_template(dsid)
513
461
  control_str = _json['data']['template']
514
462
 
515
- template_filename = write_location + dsid + '_control.ctl'
463
+ template_filename = os.path.join(write_location, dsid + '_control.ctl')
516
464
  if os.path.exists(template_filename):
517
465
  print(template_filename + " already exists.\nExiting")
518
- exit(1)
466
+ sys.exit(1)
519
467
  with open(template_filename, 'w') as fh:
520
468
  fh.write(control_str)
521
469
 
522
470
  return _json
523
471
 
524
472
  def purge_request(request_idx):
525
- """Write a control file for use in subset requests.
473
+ """Purge a request.
526
474
 
527
475
  Args:
528
- ds (str): datset id. e.g. 'd083002'
529
- write_location (str, Optional): Directory in which to write.
530
- Defaults to working directory
476
+ request_idx (str): Request Index, typically a 7-digit integer.
531
477
 
532
478
  Returns:
533
- None
479
+ dict: JSON decoded result of the query.
534
480
  """
535
- url = BASE_URL + 'purge/'
536
- url += request_idx
537
-
538
- token = get_authentication()
539
- ret = requests.delete(encode_url(url,token))
540
-
541
- check_status(ret)
542
- return ret.json()
481
+ return _api_request('purge/' + str(request_idx), method='delete')
543
482
 
544
483
  def get_selected_function(args_dict):
545
484
  """Returns correct function based on options.
@@ -570,5 +509,4 @@ def main():
570
509
  query(sys.argv[1:])
571
510
 
572
511
  if __name__ == "__main__":
573
- """Calls main method"""
574
- query(sys.argv[1:])
512
+ main()
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gdex-api-client
3
- Version: 2.0.8
3
+ Version: 2.0.9
4
4
  Summary: CLI and library for calling GDEX API
5
5
  Author-email: GDEX data Help <datahelp@ucar.edu>
6
6
  License-Expression: MIT
7
- Project-URL: Homepage, https://gdex.ucar/edu
7
+ Project-URL: Homepage, https://gdex.ucar.edu
8
8
  Keywords: API,Geoscience,GDEX
9
9
  Description-Content-Type: text/markdown
10
10
  License-File: LICENSE
@@ -15,7 +15,7 @@ Dynamic: license-file
15
15
 
16
16
  This repository contains the Geoscience Data Exchange (GDEX) API client for python.
17
17
 
18
- At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x), see [src/python](src/python) for more details.
18
+ At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x), see [src/gdex_api_client](../src/gdex_api_client) for more details.
19
19
 
20
20
 
21
21
  # GDEX External Application API Description
@@ -45,10 +45,10 @@ At this stage, the (GDEX) REST API clients are available for Python (2.x and 3.x
45
45
  + The client can be used as a command-line tool or as an imported python module to work with python objects directly.
46
46
  + In most cases, the client will print the JSON response after a command is entered.
47
47
  + A Jupyter Notebook was created to show the basic functionality of gdex_client.py.
48
- - [The Jupyter Notebook can be found here](../src/python/gdex_client_example.ipynb)
49
- + [gdex_client.py can be found here](../src/python/gdex_client.py)
48
+ - [The Jupyter Notebook can be found here](../src/gdex_api_client/gdex_client_example.ipynb)
49
+ + [gdex_client.py can be found here](../src/gdex_api_client/gdex_client.py)
50
50
  - All HTTPS requests to to the GDEX API must include a bearer token to access the API.
51
- + Token information can be found on [you user profile](https://gdex.ucar.edu/accounts/profile)
51
+ + Token information can be found on [your user profile](https://gdex.ucar.edu/accounts/profile)
52
52
  - Responses are JSON formatted.
53
53
  + Typically, error responses will give an explaination of what went wrong in the value of the `messages` key.
54
54
  + If `status` is `ok`, then all relevant data will be in the value of the `result` key.
@@ -70,7 +70,7 @@ Additionally, you can programatically use the module via
70
70
  ### Authentication
71
71
 
72
72
  `POST`, `DELETE`, and some user-specific 'GET' HTTP requests require that you include a bearer token in your URL.
73
- To do this you would need to first find your token in [you user profile](https://gdex.ucar.edu/accounts/profile).
73
+ To do this you would need to first find your token in [your user profile](https://gdex.ucar.edu/accounts/profile).
74
74
 
75
75
  Next, you would append this token to the end of any url using `?token=[bearer token]. For example,
76
76
  ```
@@ -89,7 +89,7 @@ The list below is how to perform HTTPS `GET`, `POST`, and `DELETE` commands usin
89
89
 
90
90
  curl -X DELETE [URL]
91
91
 
92
- URL in these examples could be for example `https://gdex.ucar.edu/api/summary/ds083.2` or `https://gdex.ucar.edu/api/status` or `https://gdex.ucar.edu/api/get_req_files/123456`
92
+ URL in these examples could be for example `https://gdex.ucar.edu/api/summary/d083002` or `https://gdex.ucar.edu/api/status` or `https://gdex.ucar.edu/api/get_req_files/123456`
93
93
 
94
94
  ------
95
95
 
@@ -103,7 +103,7 @@ The list below is how to perform HTTPS `GET`, `POST`, and `DELETE` commands usin
103
103
 
104
104
  wget --method=delete [URL]
105
105
 
106
- URL in these case could be for example `https://gdex.ucar.edu/api/summary/ds083.2` or `https://gdex.ucar.edu/api/request` or `https://gdex.ucar.edu/api/request/123456`
106
+ URL in these case could be for example `https://gdex.ucar.edu/api/summary/d083002` or `https://gdex.ucar.edu/api/request` or `https://gdex.ucar.edu/api/request/123456`
107
107
 
108
108
  ------
109
109
 
@@ -118,7 +118,7 @@ Returns a summary of datasets and dataset groups that have subsetting available.
118
118
  #### URL
119
119
 
120
120
  ```
121
- GET https://gdex.ucar.edu/api/summary/[dsnnn.n]
121
+ GET https://gdex.ucar.edu/api/summary/[dnnnnnn]
122
122
  ```
123
123
 
124
124
  #### Example Response
@@ -162,7 +162,7 @@ Returns a summary of only the Parameters in a dataset for subsetting.
162
162
  #### URL
163
163
 
164
164
  ```
165
- GET https://gdex.ucar.edu/api/paramsummary/[dsnnn.n]
165
+ GET https://gdex.ucar.edu/api/paramsummary/[dnnnnnn]
166
166
  ```
167
167
 
168
168
  #### Example Response
@@ -173,7 +173,7 @@ GET https://gdex.ucar.edu/api/paramsummary/[dsnnn.n]
173
173
  "https_response": 200,
174
174
  "messages": [],
175
175
  "result": {
176
- "dsid": "083.2",
176
+ "dsid": "d083002",
177
177
  "subsetting_available": true,
178
178
  "data": [
179
179
  {
@@ -243,7 +243,7 @@ Returns full metadata of a dataset available for subsetting.
243
243
  #### URL
244
244
 
245
245
  ```
246
- GET https://gdex.ucar.edu/api/metadata/[dsnnn.n]
246
+ GET https://gdex.ucar.edu/api/metadata/[dnnnnnn]
247
247
  ```
248
248
 
249
249
  #### Example Response
@@ -449,7 +449,7 @@ GET https://gdex.ucar.edu/api/metadata/[dsnnn.n]
449
449
  #### Description
450
450
 
451
451
  Returns that status of a given request index.
452
- A request index is a six digit integer.
452
+ A request index is a seven digit integer.
453
453
 
454
454
  An authentication token is needed for this for this request. See [Authentication Section](#authentication) for more details.
455
455
 
@@ -482,10 +482,10 @@ If `[RequestIndex]` is given
482
482
  {
483
483
  "status": "Completed",
484
484
  "date_ready": "2020-03-01",
485
- "request_index": 410935,
485
+ "request_index": 4109350,
486
486
  "NCAR_contact": "rpconroy@ucar.edu",
487
- "rqstid": "LASTNAME410935",
488
- "request_id": "LASTNAME410935",
487
+ "rqstid": "LASTNAME4109350",
488
+ "request_id": "LASTNAME4109350",
489
489
  "date_purge": "2020-03-06",
490
490
  "date_rqst": "2020-03-01",
491
491
  "subset_info": {
@@ -509,10 +509,10 @@ Or if `[RequestIndex]` is not specified, get all requests
509
509
  {
510
510
  "status": "Completed",
511
511
  "date_ready": "2020-03-01",
512
- "request_index": 410935,
512
+ "request_index": 4109350,
513
513
  "NCAR_contact": "rpconroy@ucar.edu",
514
- "rqstid": "LASTNAME410935",
515
- "request_id": "LASTNAME410935",
514
+ "rqstid": "LASTNAME4109350",
515
+ "request_id": "LASTNAME4109350",
516
516
  "date_purge": "2020-03-06",
517
517
  "date_rqst": "2020-03-01",
518
518
  "subset_info": {
@@ -522,10 +522,10 @@ Or if `[RequestIndex]` is not specified, get all requests
522
522
  {
523
523
  "status": "Completed",
524
524
  "date_ready": "2020-03-01",
525
- "request_index": 410936,
525
+ "request_index": 4109360,
526
526
  "NCAR_contact": "rpconroy@ucar.edu",
527
- "rqstid": "LASTNAME410936",
528
- "request_id": "LASTNAME410936",
527
+ "rqstid": "LASTNAME4109360",
528
+ "request_id": "LASTNAME4109360",
529
529
  "date_purge": "2020-03-06",
530
530
  "date_rqst": "2020-03-01",
531
531
  "subset_info": {
@@ -565,7 +565,7 @@ GET https://gdex.ucar.edu/api/get_req_files/[RequestIndex]
565
565
  "total_size": 156,
566
566
  "web_files": [
567
567
  {
568
- "web_path": "https://gdex.ucar.edu/dsrqst/LASTNAME411039/Filename.extension",
568
+ "web_path": "https://gdex.ucar.edu/dsrqst/LASTNAME4110390/Filename.extension",
569
569
  "wfile": "Filename.extension",
570
570
  "size": 156
571
571
  }
@@ -598,7 +598,7 @@ GET https://gdex.ucar.edu/api/control_file_template/[dsxxx.x]
598
598
  "https_response": 200,
599
599
  "messages": [],
600
600
  "result": {
601
- "template": "dataset=ds083.2\ndate=201103020000/to/201103151200\nparam=TMP/R H/ABS V\nlevel=ISBL:850/700/500\noformat=netCDF\nnlat=30\nslat=-25\nwlon=-150\nelon=-30\n#groupindex=2\ntargetdir=/glade/scratch\n"
601
+ "template": "dataset=d083002\ndate=201103020000/to/201103151200\nparam=TMP/R H/ABS V\nlevel=ISBL:850/700/500\noformat=netCDF\nnlat=30\nslat=-25\nwlon=-150\nelon=-30\n#groupindex=2\ntargetdir=/glade/scratch\n"
602
602
  },
603
603
  "request_end": "2020-03-03T11:44:21.842582",
604
604
  "request_start": "2020-03-03T11:44:21.835081"
@@ -631,7 +631,7 @@ POST https://gdex.ucar.edu/api/submit
631
631
  "https_response": 200,
632
632
  "messages": [],
633
633
  "result": {
634
- "request_id": "411298"
634
+ "request_id": "4112980"
635
635
  },
636
636
  "request_end": "2020-03-03T11:45:16.513226",
637
637
  "request_start": "2020-03-03T11:45:15.341967"
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ Offline tests for gdex_client.py (no network access required).
4
+ """
5
+
6
+ import os
7
+ import sys
8
+ from unittest import mock
9
+
10
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
11
+ from gdex_api_client import gdex_client as gc
12
+
13
+
14
+ def test_validate_dsid():
15
+ assert gc.validate_dsid('d083002') == 'd083002'
16
+ for bad in ('083.2', 'ds083.2', 'd08300'):
17
+ try:
18
+ gc.validate_dsid(bad)
19
+ except ValueError:
20
+ continue
21
+ raise AssertionError(bad + ' should be rejected')
22
+
23
+
24
+ def test_read_control_file_string():
25
+ params = gc.read_control_file('# comment\ndataset=d083002\n\nnote=a=b\n')
26
+ assert params == {'dataset': 'd083002', 'note': 'a=b'}
27
+
28
+
29
+ def test_api_request_urls_and_methods():
30
+ cases = [
31
+ (lambda: gc.get_summary('d083002'), 'get', 'summary/d083002'),
32
+ (lambda: gc.get_status('1234567'), 'get', 'status/1234567'),
33
+ (lambda: gc.get_status(), 'get', 'status/ALL'),
34
+ (lambda: gc.get_filelist('1234567'), 'get', 'get_req_files/1234567'),
35
+ (lambda: gc.globus_download('1234567'), 'get', 'request/1234567-globus_download'),
36
+ (lambda: gc.purge_request('1234567'), 'delete', 'purge/1234567'),
37
+ (lambda: gc.submit_json({'dataset': 'd083002'}), 'post', 'submit/'),
38
+ ]
39
+ for call, method, endpoint in cases:
40
+ with mock.patch.object(gc, 'get_authentication', return_value='TOK'), \
41
+ mock.patch.object(gc.requests, 'request') as req:
42
+ req.return_value.status_code = 200
43
+ req.return_value.json.return_value = {'ok': True}
44
+ assert call() == {'ok': True}
45
+ args = req.call_args[0]
46
+ assert args == (method, gc.BASE_URL + endpoint + '?token=TOK'), args
47
+
48
+
49
+ if __name__ == '__main__':
50
+ test_validate_dsid()
51
+ test_read_control_file_string()
52
+ test_api_request_urls_and_methods()
53
+ print('All tests successful')
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env python
2
- """
3
- Tests the functionality of rdams_client.py
4
- """
5
-
6
- import sys
7
- sys.path.append('../src/')
8
- from gdex_api_client import gdex_client as gc
9
-
10
-
11
-
12
-
13
- assert gc.add_ds_str('083.2') == 'ds083.2'
14
- assert gc.add_ds_str('ds083.2') == 'ds083.2'
15
-
16
-
17
- print('All tests succesful')
File without changes