Skip to content

Commit 2e81439

Browse files
committed
add filtering for get_jgi_samples
1 parent 317bd85 commit 2e81439

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

nmdc_api_utilities/data_staging.py

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ class JGISequencingProjectAPI(NMDCSearch):
1414
Class to interact with the NMDC API to get JGI samples.
1515
"""
1616

17-
def __init__(self, env="prod", auth: NMDCAuth = None):
17+
def __init__(self, env="prod", auth: NMDCAuth = None, client_id: str = None, client_secret: str = None):
1818
self.env = env
1919
self.auth = auth or NMDCAuth()
20+
if client_id and client_secret:
21+
self.auth = NMDCAuth(
22+
client_id=client_id, client_secret=client_secret, env=self.env
23+
)
2024
super().__init__(env=env)
2125

2226
@requires_auth
2327
def create_jgi_sequencing_project(
2428
self,
2529
jgi_sequencing_project: dict,
26-
client_id: str = None,
27-
client_secret: str = None,
2830
) -> dict:
2931
"""
3032
Create a new JGI sequencing project in the NMDC database.
@@ -44,10 +46,7 @@ def create_jgi_sequencing_project(
4446
Exception
4547
If the creation fails.
4648
"""
47-
if client_id and client_secret:
48-
self.auth = NMDCAuth(
49-
client_id=client_id, client_secret=client_secret, env=self.env
50-
)
49+
5150
url = f"{self.base_url}/wf_file_staging/jgi_sequencing_projects"
5251
headers = {
5352
"accept": "application/json",
@@ -67,6 +66,7 @@ def create_jgi_sequencing_project(
6766

6867
return response.json()
6968

69+
@requires_auth
7070
def list_jgi_sequencing_projects(self, params: dict = None) -> dict:
7171
"""
7272
List JGI sequencing projects from the NMDC database.
@@ -84,6 +84,8 @@ def list_jgi_sequencing_projects(self, params: dict = None) -> dict:
8484
url = f"{self.base_url}/wf_file_staging/jgi_sequencing_projects"
8585
headers = {
8686
"accept": "application/json",
87+
"Content-Type": "application/json",
88+
"Authorization": f"Bearer {self.auth.get_token()}",
8789
}
8890
try:
8991
response = requests.get(url, headers=headers, params=params)
@@ -98,6 +100,7 @@ def list_jgi_sequencing_projects(self, params: dict = None) -> dict:
98100

99101
return response.json()['resources']
100102

103+
@requires_auth
101104
def get_jgi_sequencing_project(self, sequencing_project_name: str) -> dict:
102105
"""
103106
Get a specific JGI sequencing project by name.
@@ -115,6 +118,7 @@ def get_jgi_sequencing_project(self, sequencing_project_name: str) -> dict:
115118
url = f"{self.base_url}/wf_file_staging/jgi_sequencing_projects/{sequencing_project_name}"
116119
headers = {
117120
"accept": "application/json",
121+
"Authorization": f"Bearer {self.auth.get_token()}",
118122
}
119123
try:
120124
response = requests.get(url, headers=headers)
@@ -127,19 +131,25 @@ def get_jgi_sequencing_project(self, sequencing_project_name: str) -> dict:
127131
f"API request response: {response.json()}\n API Status Code: {response.status_code}"
128132
)
129133

130-
return response.json()['resources'][0]
134+
return response.json()
131135

132136

133137
class JGISampleSearchAPI(NMDCSearch):
134138
"""
135139
Class to interact with the NMDC API to get JGI samples.
136140
"""
137141

138-
def __init__(self, env="prod"):
142+
def __init__(self, auth: NMDCAuth = None, env="prod", client_id: str = None, client_secret: str = None):
139143
self.env = env
140-
super().__init__(collection_name="jgi_samples", env=env)
144+
self.auth = auth or NMDCAuth()
145+
if client_id and client_secret:
146+
self.auth = NMDCAuth(
147+
client_id=client_id, client_secret=client_secret, env=self.env
148+
)
149+
super().__init__(env=env)
141150

142-
def get_jgi_samples(self, sequencing_project_name: str) -> dict:
151+
@requires_auth
152+
def get_jgi_samples(self, query: dict=None) -> dict:
143153
"""
144154
Get a specific JGI sample by name.
145155
@@ -153,12 +163,15 @@ def get_jgi_samples(self, sequencing_project_name: str) -> dict:
153163
dict
154164
The JGI sample record.
155165
"""
156-
url = f"{self.base_url}/wf_file_staging/jgi_samples/{sequencing_project_name}"
157-
headers = {
158-
"accept": "application/json",
159-
}
166+
160167
try:
161-
response = requests.get(url, headers=headers)
168+
query = query if query else {}
169+
query_params = {"filter": f'{json.dumps(query)}', "max_page_size": 20}
170+
response = requests.get(
171+
self.base_url + '/wf_file_staging/jgi_samples',
172+
headers={"Authorization": f"Bearer {self.auth.get_token()}"},
173+
params=query_params
174+
)
162175
response.raise_for_status()
163176
except requests.RequestException as e:
164177
logger.error(f"Request failed: {e}")
@@ -171,17 +184,15 @@ def get_jgi_samples(self, sequencing_project_name: str) -> dict:
171184
return response.json()['resources']
172185

173186
@requires_auth
174-
def insert_jgi_samples(self,
175-
jgi_samples: list,
176-
client_id: str = None,
177-
client_secret: str = None,) -> dict:
187+
def insert_jgi_sample(self,
188+
jgi_sample: dict,) -> dict:
178189
"""
179190
Insert JGI samples into the NMDC database.
180191
181192
Parameters
182193
----------
183194
jgi_samples : list
184-
The list of JGI sample data to be inserted.
195+
The JGI sample data to be inserted.
185196
186197
Returns
187198
-------
@@ -193,18 +204,15 @@ def insert_jgi_samples(self,
193204
Exception
194205
If the insertion fails.
195206
"""
196-
if client_id and client_secret:
197-
self.auth = NMDCAuth(
198-
client_id=client_id, client_secret=client_secret, env=self.env
199-
)
200207
url = f"{self.base_url}/wf_file_staging/jgi_samples"
201208
headers = {
202209
"accept": "application/json",
203210
"Content-Type": "application/json",
204211
"Authorization": f"Bearer {self.auth.get_token()}",
205212
}
213+
print(headers)
206214
try:
207-
response = requests.post(url, headers=headers, json=jgi_samples)
215+
response = requests.post(url, headers=headers, json=jgi_sample)
208216
response.raise_for_status()
209217
except requests.RequestException as e:
210218
logger.error(f"Request failed: {e}")
@@ -219,9 +227,7 @@ def insert_jgi_samples(self,
219227
@requires_auth
220228
def update_jgi_sample(self,
221229
jgi_file_id: str,
222-
jgi_sample: dict,
223-
client_id: str = None,
224-
client_secret: str = None,) -> dict:
230+
jgi_sample: dict,) -> dict:
225231
"""
226232
Update JGI samples in the NMDC database.
227233
@@ -246,18 +252,14 @@ def update_jgi_sample(self,
246252
Exception
247253
If the update fails.
248254
"""
249-
if client_id and client_secret:
250-
self.auth = NMDCAuth(
251-
client_id=client_id, client_secret=client_secret, env=self.env
252-
)
253255
url = f"{self.base_url}/wf_file_staging/jgi_samples/{jgi_file_id}"
254256
headers = {
255257
"accept": "application/json",
256258
"Content-Type": "application/json",
257259
"Authorization": f"Bearer {self.auth.get_token()}",
258260
}
259261
try:
260-
response = requests.put(url, headers=headers, json=jgi_sample)
262+
response = requests.patch(url, headers=headers, json=jgi_sample)
261263
response.raise_for_status()
262264
except requests.RequestException as e:
263265
logger.error(f"Request failed: {e}")
@@ -274,11 +276,16 @@ class GlobusTaskAPI(NMDCSearch):
274276
Class to interact with the NMDC API for Globus tasks.
275277
"""
276278

277-
def __init__(self, env="prod"):
279+
def __init__(self, env="prod", auth: NMDCAuth = None, client_id: str = None, client_secret: str = None):
278280
self.env = env
279-
super().__init__(collection_name="globus_tasks", env=env)
281+
if client_id and client_secret:
282+
self.auth = NMDCAuth(
283+
client_id=client_id, client_secret=client_secret, env=self.env
284+
)
285+
super().__init__(env=env)
280286

281-
def get_globus_tasks(self, task_id: str = None) -> dict:
287+
@requires_auth
288+
def get_globus_tasks(self, query: dict = None) -> dict:
282289
"""
283290
Get Globus tasks, optionally filtered by task_id.
284291
@@ -295,12 +302,13 @@ def get_globus_tasks(self, task_id: str = None) -> dict:
295302
url = f"{self.base_url}/wf_file_staging/globus_tasks"
296303
headers = {
297304
"accept": "application/json",
305+
"Content-Type": "application/json",
306+
"Authorization": f"Bearer {self.auth.get_token()}",
298307
}
299-
params = {}
300-
if task_id:
301-
params['task_id'] = task_id
308+
query = query if query else {}
309+
query_params = {"filter": f'{json.dumps(query)}', "max_page_size": 20}
302310
try:
303-
response = requests.get(url, headers=headers, params=params)
311+
response = requests.get(url, headers=headers, params=query_params)
304312
response.raise_for_status()
305313
except requests.RequestException as e:
306314
logger.error(f"Request failed: {e}")
@@ -314,9 +322,7 @@ def get_globus_tasks(self, task_id: str = None) -> dict:
314322

315323
@requires_auth
316324
def create_globus_task(self,
317-
globus_task: dict,
318-
client_id: str = None,
319-
client_secret: str = None,) -> dict:
325+
globus_task: dict,) -> dict:
320326
"""
321327
Create a new Globus task in the NMDC database.
322328
@@ -335,10 +341,7 @@ def create_globus_task(self,
335341
Exception
336342
If the creation fails.
337343
"""
338-
if client_id and client_secret:
339-
self.auth = NMDCAuth(
340-
client_id=client_id, client_secret=client_secret, env=self.env
341-
)
344+
342345
url = f"{self.base_url}/wf_file_staging/globus_tasks"
343346
headers = {
344347
"accept": "application/json",
@@ -360,9 +363,8 @@ def create_globus_task(self,
360363

361364
@requires_auth
362365
def update_globus_task(self,
363-
globus_task: dict,
364-
client_id: str = None,
365-
client_secret: str = None,) -> dict:
366+
globus_task_id: str,
367+
globus_task: dict,) -> dict:
366368
"""
367369
Update a Globus task in the NMDC database.
368370
@@ -381,18 +383,14 @@ def update_globus_task(self,
381383
Exception
382384
If the update fails.
383385
"""
384-
if client_id and client_secret:
385-
self.auth = NMDCAuth(
386-
client_id=client_id, client_secret=client_secret, env=self.env
387-
)
388-
url = f"{self.base_url}/wf_file_staging/globus_tasks"
386+
url = f"{self.base_url}/wf_file_staging/globus_tasks/{globus_task_id}"
389387
headers = {
390388
"accept": "application/json",
391389
"Content-Type": "application/json",
392390
"Authorization": f"Bearer {self.auth.get_token()}",
393391
}
394392
try:
395-
response = requests.put(url, headers=headers, json=globus_task)
393+
response = requests.patch(url, headers=headers, json=globus_task)
396394
response.raise_for_status()
397395
except requests.RequestException as e:
398396
logger.error(f"Request failed: {e}")

0 commit comments

Comments
 (0)