1- from typing import List , Literal , Optional , Sequence , TypedDict , overload
1+ from typing import Literal , Optional , TypedDict , overload
22
33from typing_extensions import NotRequired , Required , Unpack
44
5- from .errors import ClientError
6- from .resources import FinderMethods , Resource , ResourceParameters , Resources
5+ from .resources import Active , ActiveFinderMethods , Resource
76
87JobTag = Literal [
98 "unknown" ,
3231]
3332
3433
35- class Job (Resource ):
34+ class Job (Active ):
3635 class _Job (TypedDict ):
3736 # Identifiers
3837 id : Required [str ]
@@ -100,10 +99,12 @@ class _Job(TypedDict):
10099 tag : Required [JobTag ]
101100 """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."""
102101
103- def __init__ (self , / , params , endpoint , ** kwargs : Unpack [_Job ]):
102+ def __init__ (self , / , params , ** kwargs : Unpack [_Job ]):
104103 super ().__init__ (params , ** kwargs )
105- key = kwargs ["key" ]
106- self ._endpoint = endpoint + key
104+
105+ @property
106+ def _endpoint (self ) -> str :
107+ return self ._ctx .url + f"v1/content/{ self ['app_id' ]} /jobs/{ self ['key' ]} "
107108
108109 def destroy (self ) -> None :
109110 """Destroy the job.
@@ -118,48 +119,21 @@ def destroy(self) -> None:
118119 ----
119120 This action requires administrator, owner, or collaborator privileges.
120121 """
121- self .params .session .delete (self ._endpoint )
122+ self ._ctx .session .delete (self ._endpoint )
122123
123124
124- class Jobs (FinderMethods [Job ], Sequence [ Job ], Resources ):
125+ class Jobs (ActiveFinderMethods [Job ]):
125126 """A collection of jobs."""
126127
127- def __init__ (self , params , endpoint ):
128- super ().__init__ (Job , params , endpoint )
129- self ._endpoint = endpoint + "jobs"
130- self ._cache = None
131-
132- @property
133- def _data (self ) -> List [Job ]:
134- if self ._cache :
135- return self ._cache
136-
137- response = self .params .session .get (self ._endpoint )
138- results = response .json ()
139- self ._cache = [Job (self .params , self ._endpoint , ** result ) for result in results ]
140- return self ._cache
141-
142- def __getitem__ (self , index ):
143- """Retrieve an item or slice from the sequence."""
144- return self ._data [index ]
145-
146- def __len__ (self ):
147- """Return the length of the sequence."""
148- return len (self ._data )
128+ _uid = "key"
149129
150- def __repr__ (self ):
151- """Return the string representation of the sequence."""
152- return f"Jobs( { ', ' . join ( map ( str , self ._data )) } )"
130+ def __init__ (self , cls , ctx , parent : Active ):
131+ super (). __init__ ( cls , ctx )
132+ self ._parent = parent
153133
154- def count (self , value ):
155- """Return the number of occurrences of a value in the sequence."""
156- return self ._data .count (value )
157-
158- def index (self , value , start = 0 , stop = None ):
159- """Return the index of the first occurrence of a value in the sequence."""
160- if stop is None :
161- stop = len (self ._data )
162- return self ._data .index (value , start , stop )
134+ @property
135+ def _endpoint (self ) -> str :
136+ return self ._ctx .url + f"v1/content/{ self ._parent ['guid' ]} /jobs"
163137
164138 class _FindByRequest (TypedDict , total = False ):
165139 # Identifiers
@@ -286,37 +260,18 @@ def find_by(self, **conditions: Unpack[_FindByRequest]) -> Optional[Job]:
286260 @overload
287261 def find_by (self , ** conditions ): ...
288262
289- def find_by (self , ** conditions ):
290- if "key" in conditions and self ._cache is None :
291- key = conditions ["key" ]
292- try :
293- return self .find (key )
294- except ClientError as e :
295- if e .http_status == 404 :
296- return None
297- raise e
298-
263+ def find_by (self , ** conditions ) -> Optional [Job ]:
299264 return super ().find_by (** conditions )
300265
301- def reload (self ) -> "Jobs" :
302- """Unload the cached jobs.
303266
304- Forces the next access, if any, to query the jobs from the Connect server.
305- """
306- self ._cache = None
307- return self
308-
309-
310- class JobsMixin (Resource ):
267+ class JobsMixin (Active , Resource ):
311268 """Mixin class to add a jobs attribute to a resource."""
312269
313270 class HasGuid (TypedDict ):
314271 """Has a guid."""
315272
316273 guid : Required [str ]
317274
318- def __init__ (self , params : ResourceParameters , ** kwargs : Unpack [HasGuid ]):
319- super ().__init__ (params , ** kwargs )
320- uid = kwargs ["guid" ]
321- endpoint = self .params .url + f"v1/content/{ uid } "
322- self .jobs = Jobs (self .params , endpoint )
275+ def __init__ (self , ctx , ** kwargs ):
276+ super ().__init__ (ctx , ** kwargs )
277+ self .jobs = Jobs (Job , ctx , self )
0 commit comments