Skip to content

Commit

Permalink
Optionally read page size limit from the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
soapy1 committed Jan 20, 2025
1 parent 93a2904 commit d517a1d
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


# conda-store default page size
PAGE_SIZE_LIMIT = 100
DEFAULT_PAGE_SIZE_LIMIT = 100

@gen.coroutine
def get_username_hook(spawner):
Expand All @@ -26,16 +26,24 @@ def get_username_hook(spawner):
)


# TODO: this should get unit tests. Currently, since this is not a python module,
# adding tests in a traditional sense is not possible. See /~https://github.com/soapy1/nebari/tree/try-unit-test-spawner
# for a demo on one approach to adding test.
def get_conda_store_environments(user_info: dict):
import urllib3
import yarl
import math
import os

# Check for the environment variable `CONDA_STORE_API_PAGE_SIZE_LIMIT`. Fall
# back to using the default page size limit if not set.
page_size = os.environ.get("CONDA_STORE_API_PAGE_SIZE_LIMIT", DEFAULT_PAGE_SIZE_LIMIT)

external_url = z2jh.get_config("custom.conda-store-service-name")
token = z2jh.get_config("custom.conda-store-jhub-apps-token")
endpoint = "conda-store/api/v1/environment"

url = yarl.URL(f"http://{external_url}/{endpoint}/?size={PAGE_SIZE_LIMIT}")
url = yarl.URL(f"http://{external_url}/{endpoint}/?size={page_size}")
http = urllib3.PoolManager()
response = http.request(
"GET", str(url), headers={"Authorization": f"Bearer {token}"}
Expand All @@ -48,11 +56,11 @@ def get_conda_store_environments(user_info: dict):

# If there are more records than the specified size limit, then
# will need to page through to get all the available envs
if total_records > PAGE_SIZE_LIMIT:
if total_records > page_size:
# Already pulled the first page of results, start looping through
# the envs starting on the 2nd page
for page in range(2, math.ceil(total_records/PAGE_SIZE_LIMIT)+1):
url = yarl.URL(f"http://{external_url}/{endpoint}/?size={PAGE_SIZE_LIMIT}&page={page}")
for page in range(2, math.ceil(total_records/page_size)+1):
url = yarl.URL(f"http://{external_url}/{endpoint}/?size={page_size}&page={page}")
response = http.request(
"GET", str(url), headers={"Authorization": f"Bearer {token}"}
)
Expand Down

0 comments on commit d517a1d

Please sign in to comment.