Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,15 @@ Find
.. autofunction:: find_collections
.. autofunction:: find_by_image_uuid
.. autofunction:: find_by_contributor
.. autofunction:: find_by_darwin_core_dataset_id
.. autofunction:: find_by_uuid

Update & delete
^^^^^^^^^^^^^^^

.. autofunction:: update
.. autofunction:: delete_by_uuid

List & count
^^^^^^^^^^^^

Expand All @@ -160,6 +167,32 @@ Compute stats

----

Licenses
--------

.. module:: fathomnet.api.licenses

The ``fathomnet.api.licenses`` module supports license lookup operations.

.. automodule:: fathomnet.api.licenses
:members:
:noindex:

----

Notifications
-------------

.. module:: fathomnet.api.notifications

The ``fathomnet.api.notifications`` module supports user notification operations. All operations require authentication.

.. automodule:: fathomnet.api.notifications
:members:
:noindex:

----

Regions
-------

Expand Down Expand Up @@ -199,6 +232,7 @@ Create, update, & delete
^^^^^^^^^^^^^^^^^^^^^^^^

.. autofunction:: create_with_dto
.. autofunction:: create_for_image
.. autofunction:: update
.. autofunction:: delete

Expand All @@ -208,6 +242,14 @@ Find
.. autofunction:: find_by_uuid
.. autofunction:: find_by_image_uuid_and_key

List & count
^^^^^^^^^^^^

.. autofunction:: find_distinct
.. autofunction:: find_distinct_keys
.. autofunction:: count_distinct
.. autofunction:: count_distinct_keys

----

Taxa
Expand Down
18 changes: 15 additions & 3 deletions src/fathomnet/api/boundingboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,22 @@ def find_all_user_defined_keys() -> List[str]:


def upload_csv(
csv_fp: BinaryIO, auth_header: Optional[dto.AuthHeader] = None
csv_fp: BinaryIO,
auth_header: Optional[dto.AuthHeader] = None,
use_contributors_email: Optional[bool] = None,
) -> dto.Message:
"""Upload a CSV of bounding boxes."""
res_json = BoundingBoxes.post("upload/csv", files={"csv": csv_fp}, auth=auth_header)
"""Upload a CSV of bounding boxes.

:param use_contributors_email: Optional value for the server's ``usecontributorsemail`` query parameter
"""
params = (
{"usecontributorsemail": use_contributors_email}
if use_contributors_email is not None
else None
)
res_json = BoundingBoxes.post(
"upload/csv", files={"csv": csv_fp}, params=params, auth=auth_header
)
return dto.Message.from_dict(res_json)


Expand Down
7 changes: 7 additions & 0 deletions src/fathomnet/api/darwincore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warnings
from typing import List

from fathomnet import dto
from fathomnet.api import EndpointManager


Expand Down Expand Up @@ -33,3 +34,9 @@ def find_owner_institutions_by_image_uuid(image_uuid: str) -> List[str]:
"""Get a list of owner institutions by image UUID."""
res_json = DarwinCore.get(f"query/ownerinstitutions/{image_uuid}")
return res_json


def find_by_dataset_id(dataset_id: str) -> dto.BDarwinCore:
"""Get a darwin core record by its dataset ID (UUID)."""
res_json = DarwinCore.get("query/datasetid/{}".format(dataset_id))
return dto.BDarwinCore.from_dict(res_json)
17 changes: 17 additions & 0 deletions src/fathomnet/api/geoimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,20 @@ def find_by_image_set_upload_uuid(
params={"limit": limit, "offset": offset},
)
return list(map(dto.GeoImage.from_dict, res_json))


def count_valid(ok: bool = True) -> dto.Count:
"""Get a count of geo images whose URL is valid (``ok=True``) or invalid (``ok=False``)."""
res_json = GeoImages.get("count/valid", params={"ok": ok})
return dto.Count.from_dict(res_json)


def find_valid(
ok: bool = True, pageable: Optional[dto.Pageable] = None
) -> List[dto.GeoImage]:
"""Get a paged list of geo images whose URL is valid (``ok=True``) or invalid (``ok=False``)."""
params = [("ok", ok)]
if pageable:
params.extend(pageable.to_params())
res_json = GeoImages.get("query/valid", params=params)
return list(map(dto.GeoImage.from_dict, res_json.get("content", [])))
18 changes: 18 additions & 0 deletions src/fathomnet/api/imagesetuploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def find_by_contributor(contributors_email: str) -> List[dto.BImageSetUploadDTO]
return list(map(dto.BImageSetUploadDTO.from_dict, res_json))


def find_by_darwin_core_dataset_id(dataset_id: str) -> dto.BImageSetUploadDTO:
"""Get the image set upload for a darwin core dataset ID (UUID)."""
res_json = ImageSetUploads.get("query/darwincore/{}".format(dataset_id))
return dto.BImageSetUploadDTO.from_dict(res_json)


def find_by_image_uuid(image_uuid: str) -> List[dto.BImageSetUploadDTO]:
"""Get an image set upload by UUID."""
res_json = ImageSetUploads.get("query/image/{}".format(image_uuid))
Expand All @@ -62,6 +68,18 @@ def find_by_uuid(uuid: str) -> dto.BImageSetUploadDTO:
return dto.BImageSetUploadDTO.from_dict(res_json)


def update(
uuid: str,
image_set_upload_update: dto.ImageSetUploadUpdate,
auth_header: Optional[dto.AuthHeader] = None,
) -> Optional[dto.BImageSetUploadDTO]:
"""(Admin) Update an image set upload's contributor email and/or contributor notification timestamp."""
res_json = ImageSetUploads.put(
uuid, json=image_set_upload_update.to_dict(), auth=auth_header
)
return dto.BImageSetUploadDTO.from_dict(res_json) if res_json else None


def delete_by_uuid(uuid: str, auth_header: Optional[dto.AuthHeader] = None) -> None:
"""Delete an image set upload by UUID."""
ImageSetUploads.delete(uuid, parse_json=False, auth=auth_header)
22 changes: 22 additions & 0 deletions src/fathomnet/api/licenses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# licenses.py (fathomnet-py)
from typing import List
from urllib.parse import quote

from fathomnet import dto
from fathomnet.api import EndpointManager


class Licenses(EndpointManager):
PATH = "licenses"


def find_all() -> List[dto.FullLicense]:
"""Get a list of all supported licenses."""
res_json = Licenses.get("list/all")
return list(map(dto.FullLicense.from_dict, res_json))


def find_by_spdx_id_or_url(spdx_id_or_url: str) -> dto.FullLicense:
"""Get a license by its SPDX identifier (e.g. ``CC-BY-4.0``) or URL."""
res_json = Licenses.get("search/{}".format(quote(spdx_id_or_url, safe="")))
return dto.FullLicense.from_dict(res_json)
110 changes: 110 additions & 0 deletions src/fathomnet/api/notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# notifications.py (fathomnet-py)
from typing import List, Optional
from urllib.parse import quote

from fathomnet import dto
from fathomnet.api import EndpointManager


class Notifications(EndpointManager):
PATH = "notifications"


def _params(pageable: Optional[dto.Pageable], **extra) -> list:
params = pageable.to_params() if pageable else []
params.extend((k, v) for k, v in extra.items() if v is not None)
return params


def create(
notification_content: dto.NotificationContent,
auth_header: Optional[dto.AuthHeader] = None,
) -> dto.Notification:
"""(Admin) Create a notification."""
res_json = Notifications.post(
"", json=notification_content.to_dict(), auth=auth_header
)
return dto.Notification.from_dict(res_json)


def find_mine(
pageable: Optional[dto.Pageable] = None,
auth_header: Optional[dto.AuthHeader] = None,
) -> List[dto.Notification]:
"""Get a paged list of notifications for the authenticated user."""
res_json = Notifications.get(
"query/email", params=_params(pageable), auth=auth_header
)
return list(map(dto.Notification.from_dict, res_json.get("content", [])))


def find_by_email(
email: str,
pageable: Optional[dto.Pageable] = None,
auth_header: Optional[dto.AuthHeader] = None,
) -> List[dto.Notification]:
"""(Admin) Get a paged list of notifications for an email."""
res_json = Notifications.get(
"query/email/{}".format(quote(email)),
params=_params(pageable),
auth=auth_header,
)
return list(map(dto.Notification.from_dict, res_json.get("content", [])))


def find_by_email_since(
email: str,
since: str,
pageable: Optional[dto.Pageable] = None,
auth_header: Optional[dto.AuthHeader] = None,
) -> List[dto.Notification]:
"""(Admin) Get a paged list of notifications for an email created since a timestamp (ISO 8601 / RFC 3339)."""
res_json = Notifications.get(
"query/email/{}/since".format(quote(email)),
params=_params(pageable, since=since),
auth=auth_header,
)
return list(map(dto.Notification.from_dict, res_json.get("content", [])))


def find_by_email_and_topic(
email: str,
topic: str,
pageable: Optional[dto.Pageable] = None,
auth_header: Optional[dto.AuthHeader] = None,
) -> List[dto.Notification]:
"""(Admin) Get a paged list of notifications for an email and topic."""
res_json = Notifications.get(
"query/email/{}/topic/{}".format(quote(email), quote(topic, safe="")),
params=_params(pageable),
auth=auth_header,
)
return list(map(dto.Notification.from_dict, res_json.get("content", [])))


def find_by_uuid(
uuid: str, auth_header: Optional[dto.AuthHeader] = None
) -> dto.Notification:
"""Get a notification by UUID."""
res_json = Notifications.get(uuid, auth=auth_header)
return dto.Notification.from_dict(res_json)


def mark_read(
uuid: str, auth_header: Optional[dto.AuthHeader] = None
) -> dto.Notification:
"""Mark a notification as read."""
res_json = Notifications.put("{}/read".format(uuid), auth=auth_header)
return dto.Notification.from_dict(res_json)


def delete(uuid: str, auth_header: Optional[dto.AuthHeader] = None) -> None:
"""Delete a notification by UUID."""
Notifications.delete(uuid, parse_json=False, auth=auth_header)


def delete_by_email(email: str, auth_header: Optional[dto.AuthHeader] = None) -> None:
"""(Admin) Delete all notifications for an email."""
Notifications.delete(
"query/email/{}".format(quote(email)), parse_json=False, auth=auth_header
)
36 changes: 36 additions & 0 deletions src/fathomnet/api/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,42 @@ def create_with_dto(
return dto.TagDTO.from_dict(res_json)


def create_for_image(
image_uuid: str, tag: dto.TagDTO, auth_header: Optional[dto.AuthHeader] = None
) -> dto.TagDTO:
"""Create a tag on the image with the given UUID."""
res_json = Tags.post(image_uuid, json=tag.to_dict(), auth=auth_header)
return dto.TagDTO.from_dict(res_json)


def count_distinct() -> dto.Count:
"""Get a count of distinct tags (key, value, media type)."""
res_json = Tags.get("count/distinct")
return dto.Count.from_dict(res_json)


def count_distinct_keys() -> dto.Count:
"""Get a count of distinct tag keys."""
res_json = Tags.get("count/distinct/keys")
return dto.Count.from_dict(res_json)


def find_distinct(pageable: Optional[dto.Pageable] = None) -> List[dto.SimpleTag]:
"""Get a paged list of distinct tags (key, value, media type)."""
res_json = Tags.get(
"list/distinct", params=pageable.to_params() if pageable else None
)
return list(map(dto.SimpleTag.from_dict, res_json.get("content", [])))


def find_distinct_keys(pageable: Optional[dto.Pageable] = None) -> List[str]:
"""Get a paged list of distinct tag keys."""
res_json = Tags.get(
"list/distinct/keys", params=pageable.to_params() if pageable else None
)
return res_json.get("content", [])


def find_by_uuid(uuid: str) -> dto.TagDTO:
"""Get a tag by UUID."""
res_json = Tags.get(uuid)
Expand Down
41 changes: 41 additions & 0 deletions src/fathomnet/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,44 @@ class ContributionStats(DTO):
ownerInstitutionCode: Optional[str] = None
totalUploads: Optional[int] = None
totalImages: Optional[int] = None


class FullLicense(DTO):
spdxId: Optional[str] = None
name: Optional[str] = None
url: Optional[str] = None
selectableForImages: Optional[bool] = None
selectableForAnnotations: Optional[bool] = None


class NotificationType(str, Enum):
ACCOUNT_CREATED = "ACCOUNT_CREATED"
IMAGE_SET = "IMAGE_SET"


class NotificationContent(DTO):
notificationType: Optional[NotificationType] = None
topic: Optional[str] = None
message: Optional[str] = None
email: Optional[str] = None


class Notification(DTO):
uuid: Optional[str] = None
notificationType: Optional[NotificationType] = None
topic: Optional[str] = None
message: Optional[str] = None
email: Optional[str] = None
createdTimestamp: Optional[str] = None
readTimestamp: Optional[str] = None


class ImageSetUploadUpdate(DTO):
contributorsEmail: Optional[str] = None
contributorNotificationTimestamp: Optional[str] = None


class SimpleTag(DTO):
key: Optional[str] = None
value: Optional[str] = None
mediaType: Optional[str] = None
5 changes: 5 additions & 0 deletions test/test_darwincore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ def test_find_owner_institutions_by_image_uuid(self):
)
self.assertIsNotNone(owner_institutions)
self.assertIn("MBARI", owner_institutions)

def test_find_by_dataset_id(self):
dataset_id = "9596e315-82e5-407f-853d-979bd5700d43"
darwin_core = darwincore.find_by_dataset_id(dataset_id)
self.assertEqual(darwin_core.datasetID, dataset_id)
Loading
Loading