Skip to content

Commit

Permalink
Merge pull request #321 from 4dn-dcic/dmichaels-minor-portal-utils-up…
Browse files Browse the repository at this point in the history
…dates-20241108

minor updates to portal_utilss for internal use
  • Loading branch information
dmichaels-harvard authored Nov 12, 2024
2 parents c15d7dd + c27ba0a commit ebd157a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ dcicutils
Change Log
----------

8.16.3
======
* dmichaels / 2024-11-08
* Minor updates to portal_utils for internal command-line utility development.


8.16.2
======
* dmichaels / 2024-10-30
* Added license exception for autocommand: GNU Lesser General Public License v3 (LGPLv3);
see license_policies/"c4-python-infrastructure.jsonc. Ran into this with latest building of submitr.


8.16.1
======
* dmichaels / 2024-10-11
Expand Down
61 changes: 58 additions & 3 deletions dcicutils/portal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from requests.models import Response
from threading import Thread
from typing import Callable, Dict, List, Optional, Tuple, Type, Union
# from urllib.parse import parse_qs as parse_url_query_string
from uuid import uuid4 as uuid
from webtest.app import TestApp, TestResponse
from wsgiref.simple_server import make_server as wsgi_make_server
Expand Down Expand Up @@ -209,8 +210,31 @@ def vapp(self) -> Optional[TestApp]:
return self._vapp

def get(self, url: str, follow: bool = True,
raw: bool = False, database: bool = False, raise_for_status: bool = False, **kwargs) -> OptionalResponse:
raw: bool = False, database: bool = False,
limit: Optional[int] = None, offset: Optional[int] = None,
field: Optional[str] = None, deleted: bool = False,
raise_for_status: bool = False, **kwargs) -> OptionalResponse:
url = self.url(url, raw, database)
if isinstance(limit, int) and (limit >= 0):
if "?" in url:
url += f"&limit={limit}"
else:
url += f"?limit={limit}"
if isinstance(offset, int) and (offset >= 0):
if "?" in url:
url += f"&from={offset}"
else:
url += f"?from={offset}"
if isinstance(field, str) and field:
if "?" in url:
url += f"&field={field}"
else:
url += f"?field={field}"
if deleted is True:
if "?" in url:
url += "&status=deleted"
else:
url += "?status=deleted"
if not self.vapp:
response = requests.get(url, allow_redirects=follow, **self._kwargs(**kwargs))
else:
Expand Down Expand Up @@ -252,14 +276,45 @@ def post(self, url: str, data: Optional[dict] = None, json: Optional[dict] = Non
response.raise_for_status()
return response

def get_metadata(self, object_id: str, raw: bool = False,
database: bool = False, raise_exception: bool = True) -> Optional[dict]:
def get_metadata(self, object_id: str, raw: bool = False, database: bool = False,
limit: Optional[int] = None, offset: Optional[int] = None,
field: Optional[str] = None, deleted: bool = False,
raise_exception: bool = True) -> Optional[dict]:
if not isinstance(object_id, str):
return None
if isinstance(raw, bool) and raw:
add_on = "frame=raw" + ("&datastore=database" if isinstance(database, bool) and database else "")
elif database:
add_on = "datastore=database"
else:
add_on = ""
if isinstance(limit, int) and (limit >= 0):
if add_on:
add_on += f"&limit={limit}"
else:
add_on += f"limit={limit}"
if isinstance(offset, int) and (offset >= 0):
if add_on:
add_on += f"&from={offset}"
else:
add_on += f"from={offset}"
if isinstance(field, int) and field:
if add_on:
add_on += f"&field={field}"
else:
add_on += f"field={field}"
if deleted is True:
if add_on:
add_on += "&status=deleted"
else:
add_on += "status=deleted"
if (question_mark := object_id.find("?")) > 0:
query_string = object_id[question_mark + 1:]
object_id = object_id[:question_mark]
if add_on:
add_on += f"&{query_string}"
else:
add_on += query_string
if raise_exception:
return get_metadata(obj_id=object_id, vapp=self.vapp, key=self.key, add_on=add_on)
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.16.2"
version = "8.16.3"
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit ebd157a

Please sign in to comment.