Skip to content

Commit dff4038

Browse files
authored
feat: adds query params to find and find_one (#130)
1 parent 22d7373 commit dff4038

File tree

8 files changed

+193
-125
lines changed

8 files changed

+193
-125
lines changed

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ coverage
33
mypy
44
pandas
55
pre-commit
6+
pyjson5
67
pytest
78
responses
89
ruff
9-
setuptools-scm
1010
setuptools
11+
setuptools-scm

src/posit/connect/paginator.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ class Paginator:
3737
url (str): The URL of the paginated API endpoint.
3838
"""
3939

40-
def __init__(self, session: requests.Session, url: str) -> None:
40+
def __init__(self, session: requests.Session, url: str, params: dict = {}) -> None:
4141
self.session = session
4242
self.url = url
43+
self.params = params
4344

4445
def fetch_results(self) -> List[dict]:
4546
"""
@@ -90,6 +91,10 @@ def fetch_page(self, page_number: int) -> Page:
9091
Page: The fetched page object.
9192
9293
"""
93-
params = {"page_number": page_number, "page_size": _MAX_PAGE_SIZE}
94+
params = {
95+
**self.params,
96+
"page_number": page_number,
97+
"page_size": _MAX_PAGE_SIZE,
98+
}
9499
response = self.session.get(self.url, params=params)
95100
return Page(**response.json())

src/posit/connect/users.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,17 @@ def __init__(self, config: Config, session: requests.Session) -> None:
152152
self.config = config
153153
self.session = session
154154

155-
def find(self) -> List[User]:
156-
paginator = Paginator(self.session, self.url)
155+
@overload
156+
def find(
157+
self, prefix: str = ..., user_role: str = ..., account_status: str = ...
158+
) -> List[User]: ...
159+
160+
@overload
161+
def find(self, *args, **kwargs) -> List[User]: ...
162+
163+
def find(self, *args, **kwargs):
164+
params = dict(*args, **kwargs)
165+
paginator = Paginator(self.session, self.url, params=params)
157166
results = paginator.fetch_results()
158167
return [
159168
User(
@@ -164,8 +173,17 @@ def find(self) -> List[User]:
164173
for user in results
165174
]
166175

167-
def find_one(self) -> User | None:
168-
paginator = Paginator(self.session, self.url)
176+
@overload
177+
def find_one(
178+
self, prefix: str = ..., user_role: str = ..., account_status: str = ...
179+
) -> User | None: ...
180+
181+
@overload
182+
def find_one(self, *args, **kwargs) -> User | None: ...
183+
184+
def find_one(self, *args, **kwargs) -> User | None:
185+
params = dict(*args, **kwargs)
186+
paginator = Paginator(self.session, self.url, params=params)
169187
pages = paginator.fetch_pages()
170188
results = (result for page in pages for result in page.results)
171189
users = (

tests/posit/connect/__api__/v1/users.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/posit/connect/__api__/v1/users?page_number=1&page_size=500.json renamed to tests/posit/connect/__api__/v1/users?page_number=1&page_size=500.jsonc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// A single page response from the '/v1/users' endpoint.
2+
//
3+
// This file is typically used in conjunction with v1/users?page_number=2&page_size=500.jsonc
4+
15
{
26
"results": [
37
{
@@ -29,4 +33,4 @@
2933
],
3034
"current_page": 1,
3135
"total": 3
32-
}
36+
}

tests/posit/connect/__api__/v1/users?page_number=2&page_size=500.json renamed to tests/posit/connect/__api__/v1/users?page_number=2&page_size=500.jsonc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// A subsequent single page response from the '/v1/users' endpoint.
2+
//
3+
// This file is typically used in conjunction with v1/users?page_number=1&page_size=500.jsonc
4+
15
{
26
"results": [
37
{
@@ -16,4 +20,4 @@
1620
],
1721
"current_page": 2,
1822
"total": 3
19-
}
23+
}

tests/posit/connect/api.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
import json
1+
import pyjson5 as json
22

33
from pathlib import Path
44

55

66
def load_mock(path: str) -> dict:
77
"""
8-
Read a JSON object from `path`
8+
Load mock data from a file.
9+
10+
Reads a JSON or JSONC (JSON with Comments) file and returns the parsed data.
11+
12+
It's primarily used for loading mock data for tests.
13+
14+
The file names for mock data should match the query path that they represent.
15+
16+
Parameters
17+
----------
18+
path : str
19+
The relative path to the JSONC file.
20+
21+
Returns
22+
-------
23+
dict
24+
The parsed data from the JSONC file.
25+
26+
Examples
27+
--------
28+
>>> data = load_mock("v1/example.json")
29+
>>> data = load_mock("v1/example.jsonc")
930
"""
1031
return json.loads((Path(__file__).parent / "__api__" / path).read_text())

0 commit comments

Comments
 (0)