Skip to content

Commit

Permalink
updating all instances of page_size to max_page_size
Browse files Browse the repository at this point in the history
  • Loading branch information
hesspnnl committed Feb 4, 2025
1 parent 6800c6b commit 86a75b8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
25 changes: 15 additions & 10 deletions nmdc_notebook_tools/collection_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_records(
The fields to return. Default is all fields.
"""
filter = urllib.parse.quote_plus(filter)
url = f"{self.base_url}/nmdcschema/{self.collection_name}?filter={filter}&page_size={max_page_size}&projection={fields}"
url = f"{self.base_url}/nmdcschema/{self.collection_name}?filter={filter}&max_page_size={max_page_size}&projection={fields}"
try:
response = requests.get(url)
response.raise_for_status()
Expand Down Expand Up @@ -70,7 +70,7 @@ def _get_all_pages(
next_page_token = response.json()["next_page_token"]
else:
break
url = f"{self.base_url}/nmdcschema/{self.collection_name}?filter={filter}&page_size={max_page_size}&projection={fields}&page_token={next_page_token}"
url = f"{self.base_url}/nmdcschema/{self.collection_name}?filter={filter}&max_page_size={max_page_size}&projection={fields}&page_token={next_page_token}"
try:
response = requests.get(url)
response.raise_for_status()
Expand All @@ -85,7 +85,7 @@ def _get_all_pages(
return results

def get_record_by_filter(
self, filter: str, page_size=25, fields="", all_pages=False
self, filter: str, max_page_size=25, fields="", all_pages=False
):
"""
Get a record from the NMDC API by its id.
Expand All @@ -94,19 +94,24 @@ def get_record_by_filter(
The filter to use to query the collection. Must be in MonogDB query format.
Resources found here - https://www.mongodb.com/docs/manual/reference/method/db.collection.find/#std-label-method-find-query
Example: {"name":{"my record name"}}
page_size: int
max_page_size: int
The number of results to return per page. Default is 25.
fields: str
The fields to return. Default is all fields.
Example: "id,name,description,alternative_identifiers,file_size_bytes,md5_checksum,data_object_type,url,type"
all_pages: bool
True to return all pages. False to return the first page. Default is False.
"""
results = self.get_records(filter, page_size, fields, all_pages)
results = self.get_records(filter, max_page_size, fields, all_pages)
return results

def get_record_by_attribute(
self, attribute_name, attribute_value, page_size=25, fields="", all_pages=False
self,
attribute_name,
attribute_value,
max_page_size=25,
fields="",
all_pages=False,
):
"""
Get a record from the NMDC API by its name. Records can be filtered based on their attributes found https://microbiomedata.github.io/nmdc-schema/.
Expand All @@ -115,14 +120,14 @@ def get_record_by_attribute(
The name of the attribute to filter by.
attribute_value: str
The value of the attribute to filter by.
page_size: int
max_page_size: int
The number of results to return per page. Default is 25.
fields: str
The fields to return. Default is all fields.
all_pages: bool
"""
filter = f'{{"{attribute_name}":{{"$regex":"{attribute_value}"}}}}'
results = self.get_records(filter, page_size, fields, all_pages)
results = self.get_records(filter, max_page_size, fields, all_pages)
return results

def get_record_by_id(
Expand All @@ -141,7 +146,7 @@ def get_record_by_id(
fields: str
The fields to return. Default is all fields.
"""
url = f"{self.base_url}/nmdcschema/{self.collection_name}/{collection_id}?page_size={max_page_size}&projection={fields}"
url = f"{self.base_url}/nmdcschema/{self.collection_name}/{collection_id}?max_page_size={max_page_size}&projection={fields}"
# get the reponse
try:
response = requests.get(url)
Expand Down Expand Up @@ -185,7 +190,7 @@ def get_record_data_object_by_type(
# if fields is empty, return all fields
if not fields:
fields = "id,name,description,alternative_identifiers,file_size_bytes,md5_checksum,data_object_type,url,type"
url = f"{self.base_url}/nmdcschema/data_object_set?filter={filter}&page_size={max_page_size}&projection={fields}"
url = f"{self.base_url}/nmdcschema/data_object_set?filter={filter}&max_page_size={max_page_size}&projection={fields}"
# get the reponse
try:
response = requests.get(url)
Expand Down
9 changes: 8 additions & 1 deletion nmdc_notebook_tools/test/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ def test_find_study_by_filter():
)


def test_get_studies():
def test_get_studies_all_pages():
st = StudySearch()
studies = st.get_records(max_page_size=20, all_pages=True)
print(studies)
assert len(studies) > 32


def test_get_studies():
st = StudySearch()
studies = st.get_records(max_page_size=100)
print(studies)
assert len(studies) == 32
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "nmdc_notebook_tools"
version = "0.2.1"
version = "0.2.2"
description = "A Python library for general research functions using NMDC APIs"
authors = [
{ name = "Olivia Hess", email = "[email protected]" },
Expand Down

0 comments on commit 86a75b8

Please sign in to comment.