Skip to content

Commit c174a45

Browse files
committed
Generate a list of urls to query
1 parent d517a1d commit c174a45

File tree

1 file changed

+34
-15
lines changed
  • src/_nebari/stages/kubernetes_services/template/modules/kubernetes/services/jupyterhub/files/jupyterhub

1 file changed

+34
-15
lines changed

src/_nebari/stages/kubernetes_services/template/modules/kubernetes/services/jupyterhub/files/jupyterhub/02-spawner.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,33 @@ def get_username_hook(spawner):
2626
)
2727

2828

29+
def get_total_records(url: str, token: str) -> int:
30+
import urllib3
31+
32+
http = urllib3.PoolManager()
33+
response = http.request(
34+
"GET", url, headers={"Authorization": f"Bearer {token}"}
35+
)
36+
decoded_response = json.loads(response.data.decode("UTF-8"))
37+
return decoded_response.get("count", 0)
38+
39+
40+
def generate_paged_urls(base_url: str, total_records: int, page_size: int) -> list[str]:
41+
import math
42+
43+
urls = []
44+
# pages starts at 1
45+
for page in range(1, math.ceil(total_records/page_size)+1):
46+
urls.append(f"{base_url}?size={page_size}&page={page}")
47+
48+
return urls
49+
50+
2951
# TODO: this should get unit tests. Currently, since this is not a python module,
3052
# adding tests in a traditional sense is not possible. See https://github.com/soapy1/nebari/tree/try-unit-test-spawner
3153
# for a demo on one approach to adding test.
3254
def get_conda_store_environments(user_info: dict):
3355
import urllib3
34-
import yarl
35-
import math
3656
import os
3757

3858
# Check for the environment variable `CONDA_STORE_API_PAGE_SIZE_LIMIT`. Fall
@@ -43,26 +63,25 @@ def get_conda_store_environments(user_info: dict):
4363
token = z2jh.get_config("custom.conda-store-jhub-apps-token")
4464
endpoint = "conda-store/api/v1/environment"
4565

46-
url = yarl.URL(f"http://{external_url}/{endpoint}/?size={page_size}")
66+
base_url = f"http://{external_url}/{endpoint}/"
4767
http = urllib3.PoolManager()
48-
response = http.request(
49-
"GET", str(url), headers={"Authorization": f"Bearer {token}"}
50-
)
5168

52-
# parse response
53-
decoded_response = json.loads(response.data.decode("UTF-8"))
54-
env_data = decoded_response.get("data", [])
55-
total_records = decoded_response.get("count", 0)
69+
# get total number of records from the endpoint
70+
total_records = get_total_records(base_url, token)
71+
72+
# will contain all the environment info returned from the api
73+
env_data = []
5674

5775
# If there are more records than the specified size limit, then
5876
# will need to page through to get all the available envs
5977
if total_records > page_size:
60-
# Already pulled the first page of results, start looping through
61-
# the envs starting on the 2nd page
62-
for page in range(2, math.ceil(total_records/page_size)+1):
63-
url = yarl.URL(f"http://{external_url}/{endpoint}/?size={page_size}&page={page}")
78+
# generate a list of urls to hit to build the response
79+
urls = generate_paged_urls(base_url, total_records, page_size)
80+
81+
# get content from urls
82+
for url in urls:
6483
response = http.request(
65-
"GET", str(url), headers={"Authorization": f"Bearer {token}"}
84+
"GET", url, headers={"Authorization": f"Bearer {token}"}
6685
)
6786
decoded_response = json.loads(response.data.decode("UTF-8"))
6887
env_data += decoded_response.get("data", [])

0 commit comments

Comments
 (0)