Skip to content

Commit 02be543

Browse files
committed
Consolidate methods into a single call
from earlier conversations with @tdstein , we should only have a single method with a trailiing kwargs so that a single mistype doesn't ruin the typed experience
1 parent 22b62ba commit 02be543

File tree

1 file changed

+130
-94
lines changed

1 file changed

+130
-94
lines changed

src/posit/connect/content.py

Lines changed: 130 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import TYPE_CHECKING, Any, List, Literal, Optional, overload
99

1010
from . import tasks
11+
from ._utils import drop_none
1112
from .bundles import Bundles
1213
from .context import Context
1314
from .env import EnvVars
@@ -33,14 +34,14 @@ def delete(self) -> None:
3334
url = self.params.url + path
3435
self.params.session.delete(url)
3536

36-
@overload
3737
def update(
38-
self,
39-
*,
40-
repository: Optional[str] = None,
41-
branch: Optional[str] = "main",
42-
directory: Optional[str] = ".",
43-
polling: Optional[bool] = False,
38+
self,
39+
*,
40+
repository: Optional[str] = None,
41+
branch: Optional[str] = "main",
42+
directory: Optional[str] = ".",
43+
polling: Optional[bool] = False,
44+
**attributes: Any,
4445
) -> None:
4546
"""Update the content's repository.
4647
@@ -54,25 +55,25 @@ def update(
5455
Directory containing the content. Default is '.'
5556
polling: bool, optional
5657
Indicates that the Git repository is regularly polled. Default is False.
57-
58+
attributes: Any
59+
Additional attributes.
5860
5961
Returns
6062
-------
6163
None
6264
"""
63-
...
64-
65-
@overload
66-
def update(self, **attributes: Any) -> None:
67-
"""Update the content's repository."""
68-
...
69-
70-
def update(self, **attributes: Any) -> None:
71-
"""Update the content's repository."""
65+
args = {
66+
"repository": repository,
67+
"branch": branch,
68+
"directory": directory,
69+
"polling": polling,
70+
**attributes,
71+
}
7272
url = self.params.url + f"v1/content/{self.content_guid}/repository"
73-
response = self.params.session.patch(url, json=attributes)
73+
response = self.params.session.patch(url, json=drop_none(args))
7474
super().update(**response.json())
7575

76+
7677
class ContentItemOAuth(Resource):
7778
def __init__(self, params: ResourceParameters, content_guid: str) -> None:
7879
super().__init__(params)
@@ -108,20 +109,20 @@ def oauth(self) -> ContentItemOAuth:
108109
def repository(self) -> Optional[ContentItemRepository]:
109110
path = f"v1/content/{self.guid}/repository"
110111
url = self.params.url + path
111-
try:
112+
try:
112113
response = self.params.session.get(url)
113114
return ContentItemRepository(self.params, self["guid"], **response.json())
114-
except:
115+
except Exception:
115116
return None
116117

117-
@overload
118118
def create_repository(
119119
self,
120120
*,
121121
repository: str,
122122
branch: Optional[str] = "main",
123123
directory: Optional[str] = ".",
124124
polling: Optional[bool] = False,
125+
**attributes: Any,
125126
) -> ContentItemRepository:
126127
"""Create repository.
127128
@@ -135,33 +136,23 @@ def create_repository(
135136
Directory containing the content. Default is '.'.
136137
polling : bool, optional
137138
Indicates that the Git repository is regularly polled. Default is False.
138-
139-
Returns
140-
-------
141-
ContentItemRepository
142-
"""
143-
...
144-
145-
@overload
146-
def create_repository(self, **attributes) -> ContentItemRepository:
147-
"""Create a repository.
148-
149-
Returns
150-
-------
151-
ContentItemRepository
152-
"""
153-
...
139+
**attributes : Any
140+
Additional attributes.
154141
155-
def create_repository(self, **attributes):
156-
"""Create a repository.
157-
158142
Returns
159143
-------
160144
ContentItemRepository
161145
"""
146+
args = {
147+
"repository": repository,
148+
"branch": branch,
149+
"directory": directory,
150+
"polling": polling,
151+
**attributes,
152+
}
162153
path = f"v1/content/{self.guid}/repository"
163154
url = self.params.url + path
164-
response = self.params.session.put(url, json=attributes)
155+
response = self.params.session.put(url, json=drop_none(args))
165156
return ContentItemRepository(self.params, **response.json())
166157

167158
def delete(self) -> None:
@@ -206,7 +197,7 @@ def render(self) -> Task:
206197
--------
207198
>>> render()
208199
"""
209-
self.update()
200+
self.update() # pyright: ignore[reportCallIssue]
210201

211202
if self.is_rendered:
212203
variants = self._variants.find()
@@ -222,7 +213,6 @@ def render(self) -> Task:
222213
f"Render not supported for this application mode: {self.app_mode}. Did you need to use the 'restart()' method instead? Note that some application modes do not support 'render()' or 'restart()'.",
223214
)
224215

225-
226216
def restart(self) -> None:
227217
"""Mark for restart.
228218
@@ -236,7 +226,7 @@ def restart(self) -> None:
236226
--------
237227
>>> restart()
238228
"""
239-
self.update()
229+
self.update() # pyright: ignore[reportCallIssue]
240230

241231
if self.is_interactive:
242232
unix_epoch_in_seconds = str(int(time.time()))
@@ -252,7 +242,6 @@ def restart(self) -> None:
252242
f"Restart not supported for this application mode: {self.app_mode}. Did you need to use the 'render()' method instead? Note that some application modes do not support 'render()' or 'restart()'.",
253243
)
254244

255-
@overload
256245
def update(
257246
self,
258247
*,
@@ -286,6 +275,7 @@ def update(
286275
default_r_environment_management: Optional[bool] = None,
287276
default_py_environment_management: Optional[bool] = None,
288277
service_account_name: Optional[str] = None,
278+
**attributes: Any,
289279
) -> None:
290280
"""Update the content item.
291281
@@ -346,15 +336,36 @@ def update(
346336
-------
347337
None
348338
"""
349-
350-
@overload
351-
def update(self, **attributes: Any) -> None:
352-
"""Update the content."""
353-
354-
def update(self, **attributes: Any) -> None:
355-
"""Update the content."""
339+
args = {
340+
"name": name,
341+
"title": title,
342+
"description": description,
343+
"access_type": access_type,
344+
"owner_guid": owner_guid,
345+
"connection_timeout": connection_timeout,
346+
"read_timeout": read_timeout,
347+
"init_timeout": init_timeout,
348+
"idle_timeout": idle_timeout,
349+
"max_processes": max_processes,
350+
"min_processes": min_processes,
351+
"max_conns_per_process": max_conns_per_process,
352+
"load_factor": load_factor,
353+
"cpu_request": cpu_request,
354+
"cpu_limit": cpu_limit,
355+
"memory_request": memory_request,
356+
"memory_limit": memory_limit,
357+
"amd_gpu_limit": amd_gpu_limit,
358+
"nvidia_gpu_limit": nvidia_gpu_limit,
359+
"run_as": run_as,
360+
"run_as_current_user": run_as_current_user,
361+
"default_image_name": default_image_name,
362+
"default_r_environment_management": default_r_environment_management,
363+
"default_py_environment_management": default_py_environment_management,
364+
"service_account_name": service_account_name,
365+
**attributes,
366+
}
356367
url = self.params.url + f"v1/content/{self['guid']}"
357-
response = self.params.session.patch(url, json=attributes)
368+
response = self.params.session.patch(url, json=drop_none(args))
358369
super().update(**response.json())
359370

360371
# Relationships
@@ -443,7 +454,6 @@ def count(self) -> int:
443454
"""
444455
return len(self.find())
445456

446-
@overload
447457
def create(
448458
self,
449459
*,
@@ -476,6 +486,7 @@ def create(
476486
default_r_environment_management: Optional[bool] = None,
477487
default_py_environment_management: Optional[bool] = None,
478488
service_account_name: Optional[str] = None,
489+
**attributes: Any,
479490
) -> ContentItem:
480491
"""Create content.
481492
@@ -529,31 +540,43 @@ def create(
529540
Manage Python environment for the content. Default is None.
530541
service_account_name : str, optional
531542
Kubernetes service account name for running content. Default is None.
543+
**attributes : Any
544+
Additional attributes.
532545
533546
Returns
534547
-------
535548
ContentItem
536549
"""
537-
538-
@overload
539-
def create(self, **attributes) -> ContentItem:
540-
"""Create a content item.
541-
542-
Returns
543-
-------
544-
ContentItem
545-
"""
546-
547-
def create(self, **attributes) -> ContentItem:
548-
"""Create a content item.
549-
550-
Returns
551-
-------
552-
ContentItem
553-
"""
550+
args = {
551+
"name": name,
552+
"title": title,
553+
"description": description,
554+
"access_type": access_type,
555+
"connection_timeout": connection_timeout,
556+
"read_timeout": read_timeout,
557+
"init_timeout": init_timeout,
558+
"idle_timeout": idle_timeout,
559+
"max_processes": max_processes,
560+
"min_processes": min_processes,
561+
"max_conns_per_process": max_conns_per_process,
562+
"load_factor": load_factor,
563+
"cpu_request": cpu_request,
564+
"cpu_limit": cpu_limit,
565+
"memory_request": memory_request,
566+
"memory_limit": memory_limit,
567+
"amd_gpu_limit": amd_gpu_limit,
568+
"nvidia_gpu_limit": nvidia_gpu_limit,
569+
"run_as": run_as,
570+
"run_as_current_user": run_as_current_user,
571+
"default_image_name": default_image_name,
572+
"default_r_environment_management": default_r_environment_management,
573+
"default_py_environment_management": default_py_environment_management,
574+
"service_account_name": service_account_name,
575+
**attributes,
576+
}
554577
path = "v1/content"
555578
url = self.params.url + path
556-
response = self.params.session.post(url, json=attributes)
579+
response = self.params.session.post(url, json=drop_none(args))
557580
return ContentItem(self.params, **response.json())
558581

559582
@overload
@@ -650,7 +673,6 @@ def find(self, include: Optional[str | list[Any]] = None, **conditions) -> List[
650673
for result in response.json()
651674
]
652675

653-
@overload
654676
def find_by(
655677
self,
656678
*,
@@ -684,8 +706,11 @@ def find_by(
684706
default_r_environment_management: Optional[bool] = None,
685707
default_py_environment_management: Optional[bool] = None,
686708
service_account_name: Optional[str] = None,
709+
**attributes: Any,
687710
) -> Optional[ContentItem]:
688-
"""Find the first content record matching the specified attributes. There is no implied ordering so if order matters, you should find it yourself.
711+
"""Find the first content record matching the specified attributes.
712+
713+
There is no implied ordering so if order matters, you should find it yourself.
689714
690715
Parameters
691716
----------
@@ -739,23 +764,8 @@ def find_by(
739764
Manage Python environment for the content.
740765
service_account_name : str, optional
741766
Kubernetes service account name for running content.
742-
743-
Returns
744-
-------
745-
Optional[ContentItem]
746-
"""
747-
748-
@overload
749-
def find_by(self, **attributes) -> Optional[ContentItem]:
750-
"""Find the first content record matching the specified attributes. There is no implied ordering so if order matters, you should find it yourself.
751-
752-
Returns
753-
-------
754-
Optional[ContentItem]
755-
"""
756-
757-
def find_by(self, **attributes) -> Optional[ContentItem]:
758-
"""Find the first content record matching the specified attributes. There is no implied ordering so if order matters, you should find it yourself.
767+
**attributes : Any
768+
Additional attributes.
759769
760770
Returns
761771
-------
@@ -765,11 +775,37 @@ def find_by(self, **attributes) -> Optional[ContentItem]:
765775
-------
766776
>>> find_by(name="example-content-name")
767777
"""
778+
args = {
779+
"name": name,
780+
"title": title,
781+
"description": description,
782+
"access_type": access_type,
783+
"owner_guid": owner_guid,
784+
"connection_timeout": connection_timeout,
785+
"read_timeout": read_timeout,
786+
"init_timeout": init_timeout,
787+
"idle_timeout": idle_timeout,
788+
"max_processes": max_processes,
789+
"min_processes": min_processes,
790+
"max_conns_per_process": max_conns_per_process,
791+
"load_factor": load_factor,
792+
"cpu_request": cpu_request,
793+
"cpu_limit": cpu_limit,
794+
"memory_request": memory_request,
795+
"memory_limit": memory_limit,
796+
"amd_gpu_limit": amd_gpu_limit,
797+
"nvidia_gpu_limit": nvidia_gpu_limit,
798+
"run_as": run_as,
799+
"run_as_current_user": run_as_current_user,
800+
"default_image_name": default_image_name,
801+
"default_r_environment_management": default_r_environment_management,
802+
"default_py_environment_management": default_py_environment_management,
803+
"service_account_name": service_account_name,
804+
**attributes,
805+
}
768806
results = self.find()
769807
results = (
770-
result
771-
for result in results
772-
if all(item in result.items() for item in attributes.items())
808+
result for result in results if all(item in result.items() for item in args.items())
773809
)
774810
return next(results, None)
775811

0 commit comments

Comments
 (0)