Skip to content

Commit a3e26e5

Browse files
authored
feat: default database and collection in MetadataDbClient (#152)
* feat: default database and collection for MetadataDbClient * docs: update MetadataDbClient examples * docs: how to override defaults
1 parent afaa7f2 commit a3e26e5

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

docs/source/ExamplesDocDBRestApi.rst

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ Count Example 1: Get # of records with a certain subject_id
1818
from aind_data_access_api.document_db import MetadataDbClient
1919
2020
API_GATEWAY_HOST = "api.allenneuraldynamics.org"
21-
DATABASE = "metadata_index"
22-
COLLECTION = "data_assets"
21+
# Default database and collection names are set in the client
22+
# To override the defaults, provide the database and collection
23+
# parameters in the constructor
2324
2425
docdb_api_client = MetadataDbClient(
2526
host=API_GATEWAY_HOST,
26-
database=DATABASE,
27-
collection=COLLECTION,
2827
)
2928
3029
filter = {"subject.subject_id": "731015"}
@@ -136,8 +135,6 @@ It's possible to attach a custom Session to retry certain requests errors:
136135
from aind_data_access_api.document_db import MetadataDbClient
137136
138137
API_GATEWAY_HOST = "api.allenneuraldynamics.org"
139-
DATABASE = "metadata_index"
140-
COLLECTION = "data_assets"
141138
142139
retry = Retry(
143140
total=5,
@@ -151,8 +148,6 @@ It's possible to attach a custom Session to retry certain requests errors:
151148
152149
with MetadataDbClient(
153150
host=API_GATEWAY_HOST,
154-
database=DATABASE,
155-
collection=COLLECTION,
156151
session=session,
157152
) as docdb_api_client:
158153
records = docdb_api_client.retrieve_docdb_records(limit=10)

docs/source/UserGuide.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ REST API
5454
from aind_data_access_api.document_db import MetadataDbClient
5555
5656
API_GATEWAY_HOST = "api.allenneuraldynamics.org"
57-
DATABASE = "metadata_index"
58-
COLLECTION = "data_assets"
57+
# Default database and collection names are set in the client
58+
# To override the defaults, provide the database and collection
59+
# parameters in the constructor
5960
6061
docdb_api_client = MetadataDbClient(
6162
host=API_GATEWAY_HOST,
62-
database=DATABASE,
63-
collection=COLLECTION,
6463
)
6564
6665
filter = {"subject.subject_id": "731015"}

src/aind_data_access_api/document_db.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,36 @@ def __exit__(self, exc_type, exc_val, exc_tb):
362362
class MetadataDbClient(Client):
363363
"""Class to manage reading and writing to metadata db"""
364364

365+
def __init__(
366+
self,
367+
host: str,
368+
database: str = "metadata_index",
369+
collection: str = "data_assets",
370+
version: str = "v1",
371+
boto: Optional[BotoSession] = None,
372+
session: Optional[Session] = None,
373+
):
374+
"""
375+
Instantiate a MetadataDbClient.
376+
377+
Parameters
378+
----------
379+
host : str
380+
database : str
381+
collection : str
382+
version : str
383+
boto : Optional[BotoSession]
384+
session : Optional[Session]
385+
"""
386+
super().__init__(
387+
host=host,
388+
database=database,
389+
collection=collection,
390+
version=version,
391+
boto=boto,
392+
session=session,
393+
)
394+
365395
def retrieve_docdb_records(
366396
self,
367397
filter_query: Optional[dict] = None,

tests/test_document_db.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@ class TestMetadataDbClient(unittest.TestCase):
437437

438438
example_client_args = {
439439
"host": "example.com/",
440-
"database": "metadata_db",
441-
"collection": "data_assets",
442440
}
443441

444442
example_record_list = [
@@ -452,6 +450,26 @@ class TestMetadataDbClient(unittest.TestCase):
452450
for id_num in range(0, 10)
453451
]
454452

453+
def test_metadatadbclient_constructor(self):
454+
"""Tests class constructor"""
455+
client = MetadataDbClient(**self.example_client_args)
456+
457+
self.assertEqual("example.com", client.host)
458+
self.assertEqual("metadata_index", client.database)
459+
self.assertEqual("data_assets", client.collection)
460+
self.assertEqual("v1", client.version)
461+
self.assertEqual(
462+
"https://example.com/v1/metadata_index/data_assets",
463+
client._base_url,
464+
)
465+
466+
client = MetadataDbClient(**self.example_client_args, version="v2")
467+
self.assertEqual("v2", client.version)
468+
self.assertEqual(
469+
"https://example.com/v2/metadata_index/data_assets",
470+
client._base_url,
471+
)
472+
455473
@patch("aind_data_access_api.document_db.Client._find_records")
456474
def test_retrieve_docdb_records(
457475
self,

0 commit comments

Comments
 (0)