Skip to content

Commit df1180c

Browse files
committed
Job to inherit from ActiveDict
1 parent 81ebea9 commit df1180c

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

src/posit/connect/jobs.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from __future__ import annotations
2+
13
import posixpath
2-
from typing import Any, Literal, Optional, overload
4+
from typing import Any, Literal, Optional
35

6+
from ._active import ActiveDict, ActiveFinderMethods, ActiveSequence
7+
from ._types_content_item import ContentItemContext, ContentItemP
48
from ._typing_extensions import NotRequired, Required, TypedDict, Unpack
5-
from .context import Context
6-
from .resources import Active, ActiveFinderMethods, ActiveSequence, Resource
79

810
JobTag = Literal[
911
"unknown",
@@ -32,7 +34,7 @@
3234
]
3335

3436

35-
class Job(Active):
37+
class Job(ActiveDict):
3638
class _Job(TypedDict):
3739
# Identifiers
3840
id: Required[str]
@@ -100,7 +102,7 @@ class _Job(TypedDict):
100102
tag: Required[JobTag]
101103
"""A tag categorizing the job type. Options are build_jupyter, build_report, build_site, configure_report, git, packrat_restore, python_restore, render_shiny, run_api, run_app, run_bokeh_app, run_dash_app, run_fastapi_app, run_pyshiny_app, run_python_api, run_streamlit, run_tensorflow, run_voila_app, testing, unknown, val_py_ext_pkg, val_r_ext_pkg, and val_r_install."""
102104

103-
def __init__(self, ctx: Context, path: str, /, **attributes: Unpack[_Job]):
105+
def __init__(self, ctx: ContentItemContext, path: str, /, **attributes: Unpack[_Job]):
104106
super().__init__(ctx, path, **attributes)
105107

106108
def destroy(self) -> None:
@@ -116,21 +118,19 @@ def destroy(self) -> None:
116118
----
117119
This action requires administrator, owner, or collaborator privileges.
118120
"""
119-
endpoint = self._ctx.url + self._path
120-
self._ctx.session.delete(endpoint)
121+
self._delete_api()
121122

122123

123-
class Jobs(ActiveFinderMethods[Job], ActiveSequence[Job]):
124-
def __init__(self, ctx: Context, path: str):
124+
class Jobs(ActiveFinderMethods[Job, ContentItemContext], ActiveSequence[Job, ContentItemContext]):
125+
def __init__(self, ctx: ContentItemContext) -> None:
125126
"""A collection of jobs.
126127
127128
Parameters
128129
----------
129-
ctx : Context
130+
ctx : ContentItemContext
130131
The context object containing the session and URL for API interactions
131-
path : str
132-
The HTTP path component for the jobs endpoint (e.g., 'v1/content/544509fc-e4f0-41de-acb4-1fe3a2c1d797/jobs')
133132
"""
133+
path = posixpath.join(ctx.content_path, "jobs")
134134
super().__init__(ctx, path, "key")
135135

136136
def _create_instance(self, path: str, /, **attributes: Any) -> Job:
@@ -149,7 +149,7 @@ def _create_instance(self, path: str, /, **attributes: Any) -> Job:
149149

150150
class _FindByRequest(TypedDict, total=False):
151151
# Identifiers
152-
id: Required[str]
152+
id: NotRequired[str] # TODO-barret-q: Is this change correct?
153153
"""A unique identifier for the job."""
154154

155155
ppid: NotRequired[Optional[str]]
@@ -214,8 +214,10 @@ class _FindByRequest(TypedDict, total=False):
214214
tag: NotRequired[JobTag]
215215
"""A tag categorizing the job type. Options are build_jupyter, build_report, build_site, configure_report, git, packrat_restore, python_restore, render_shiny, run_api, run_app, run_bokeh_app, run_dash_app, run_fastapi_app, run_pyshiny_app, run_python_api, run_streamlit, run_tensorflow, run_voila_app, testing, unknown, val_py_ext_pkg, val_r_ext_pkg, and val_r_install."""
216216

217-
@overload
218-
def find_by(self, **conditions: Unpack[_FindByRequest]) -> Optional[Job]:
217+
def find_by( # pyright: ignore[reportIncompatibleMethodOverride]
218+
self,
219+
**conditions: Unpack[_FindByRequest],
220+
) -> Job | None:
219221
"""Finds the first record matching the specified conditions.
220222
221223
There is no implied ordering so if order matters, you should specify it yourself.
@@ -267,30 +269,33 @@ def find_by(self, **conditions: Unpack[_FindByRequest]) -> Optional[Job]:
267269
-------
268270
Optional[Job]
269271
"""
270-
271-
@overload
272-
def find_by(self, **conditions): ...
273-
274-
def find_by(self, **conditions) -> Optional[Job]:
275272
return super().find_by(**conditions)
276273

277274

278-
class JobsMixin(Active, Resource):
275+
class JobsMixin:
279276
"""Mixin class to add a jobs attribute to a resource."""
280277

281-
def __init__(self, ctx, path, /, **attributes):
282-
"""Mixin class which adds a `jobs` attribute to the Active Resource.
278+
# def __init__(self: ContentItemP, /, **kwargs: Any) -> None:
279+
# super().__init__(**kwargs)
283280

284-
Parameters
285-
----------
286-
ctx : Context
287-
The context object containing the session and URL for API interactions
288-
path : str
289-
The HTTP path component for the resource endpoint
290-
**attributes : dict
291-
Resource attributes passed
281+
@property
282+
def jobs(self: ContentItemP) -> Jobs:
283+
"""Get the jobs.
284+
285+
Returns
286+
-------
287+
Jobs
292288
"""
293-
super().__init__(ctx, path, **attributes)
289+
# Do not cache result. `content.jobs` should always return the latest jobs.
290+
291+
# if self.__dict__.get("_jobs") is not None:
292+
# # Early return
293+
# return self._jobs
294+
295+
# # Populate Jobs info
296+
# class ContentItemJobsP(ContentItemP, Protocol):
297+
# _jobs: Jobs
298+
# self._jobs = Jobs(self._ctx, posixpath.join(self._path, "jobs"))
299+
# return self._jobs
294300

295-
path = posixpath.join(path, "jobs")
296-
self.jobs = Jobs(ctx, path)
301+
return Jobs(self._ctx)

0 commit comments

Comments
 (0)