Skip to content

Commit f57340d

Browse files
committed
renames init arguments to path and pathinfo
1 parent b64f3e7 commit f57340d

File tree

3 files changed

+57
-73
lines changed

3 files changed

+57
-73
lines changed

src/posit/connect/content.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ class ContentItemOwner(Resource):
3737
class ContentItem(JobsMixin, VanityMixin, Resource):
3838
def __init__(self, /, params: ResourceParameters, **kwargs):
3939
ctx = Context(params.session, params.url)
40-
base = f"v1/content/{kwargs['guid']}"
41-
super().__init__(ctx, base, **kwargs)
40+
path = f"v1/content"
41+
pathinfo = kwargs["guid"]
42+
super().__init__(ctx, path, pathinfo, **kwargs)
4243

4344
def __getitem__(self, key: Any) -> Any:
4445
v = super().__getitem__(key)

src/posit/connect/jobs.py

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,34 +101,13 @@ class _Job(TypedDict):
101101
"""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."""
102102

103103
@overload
104-
def __init__(self, ctx: Context, base: str, uid: str, /, **attributes: Unpack[_Job]):
105-
...
104+
def __init__(self, ctx: Context, path: str, pathinfo: str, /, **attributes: Unpack[_Job]): ...
106105

107106
@overload
108-
def __init__(self, ctx: Context, base: str, uid: str, /, **attributes: Any): ...
109-
110-
def __init__(self, ctx: Context, base: str, uid: str, /, **attributes: Any):
111-
"""A Job.
112-
113-
A Job represents single execution instance of Content on Connect. Whenever Content runs, whether it's a scheduled report, a script execution, or server processes related to an application, a Job is created to manage and encapsulate that execution.
107+
def __init__(self, ctx: Context, path: str, pathinfo: str, /, **attributes: Any): ...
114108

115-
Parameters
116-
----------
117-
ctx : Context
118-
The context object containing the session and URL for API interactions.
119-
base : str
120-
The base HTTP path for the Job endpoint (e.g., '/jobs')
121-
uid : str
122-
The unique identifier
123-
**attributes
124-
Object items passed to the base resource dictionary.
125-
126-
Notes
127-
-----
128-
A Job is a reference to a server process on Connect, it is not the process itself. Jobs are executed asynchronously.
129-
"""
130-
super().__init__(ctx, **attributes)
131-
self._endpoint = ctx.url + base + uid
109+
def __init__(self, ctx: Context, path: str, pathinfo: str, /, **attributes: Any):
110+
super().__init__(ctx, path, pathinfo, **attributes)
132111

133112
def destroy(self) -> None:
134113
"""Destroy the job.
@@ -143,41 +122,42 @@ def destroy(self) -> None:
143122
----
144123
This action requires administrator, owner, or collaborator privileges.
145124
"""
146-
self._ctx.session.delete(self._endpoint)
125+
endpoint = self._ctx.url + self._path
126+
self._ctx.session.delete(endpoint)
147127

148128

149129
class Jobs(ActiveFinderMethods[Job], ActiveSequence[Job]):
150-
def __init__(self, ctx: Context, base: str, path: str = "jobs", uid="key"):
130+
def __init__(self, ctx: Context, path: str, pathinfo: str = "jobs", uid: str = "key"):
151131
"""A collection of jobs.
152132
153133
Parameters
154134
----------
155135
ctx : Context
156136
The context object containing the session and URL for API interactions
157-
base : str
158-
The base HTTP path for the collection endpoint
159-
name : str
160-
The collection name, by default "jobs"
137+
path : str
138+
The HTTP path component for the collection endpoint
139+
pathinfo : str
140+
The HTTP part of the path directed at a specific resource, by default "jobs"
161141
uid : str, optional
162-
The field name used to uniquely identify records, by default "key"
142+
The field name used to uniquely identify records, by default "guid"
163143
"""
164-
super().__init__(ctx, base, path, uid)
144+
super().__init__(ctx, path, pathinfo, uid)
165145

166-
def _create_instance(self, base: str, uid: str, **kwargs: Any) -> Job:
146+
def _create_instance(self, path: str, pathinfo: str, **kwargs: Any) -> Job:
167147
"""Creates a Job instance.
168148
169149
Parameters
170150
----------
171-
base : str
172-
The base HTTP path for the instance endpoint
173-
uid : str
174-
The unique identifier for the instance.
151+
path : str
152+
The HTTP path component for the collection endpoint
153+
pathinfo : str
154+
The HTTP part of the path directed at a specific resource
175155
176156
Returns
177157
-------
178158
Job
179159
"""
180-
return Job(self._ctx, base, uid, **kwargs)
160+
return Job(self._ctx, path, pathinfo, **kwargs)
181161

182162
class _FindByRequest(TypedDict, total=False):
183163
# Identifiers
@@ -311,15 +291,19 @@ def find_by(self, **conditions) -> Optional[Job]:
311291
class JobsMixin(Active, Resource):
312292
"""Mixin class to add a jobs attribute to a resource."""
313293

314-
def __init__(self, ctx: Context, base: str, /, **kwargs):
294+
def __init__(self, ctx, path, pathinfo="", /, **kwargs):
315295
"""Mixin class which adds a `jobs` attribute to the Active Resource.
316296
317297
Parameters
318298
----------
319299
ctx : Context
320-
The context object containing the session and URL for API interactions
321-
base : str
322-
The base path associated with the instance.
300+
The context object containing the session and URL for API interactions.
301+
path : str
302+
The HTTP path component for the collection endpoint
303+
pathinfo : str
304+
The HTTP part of the path directed at a specific resource
305+
**attributes : dict
306+
Resource attributes passed
323307
"""
324-
super().__init__(ctx, **kwargs)
325-
self.jobs = Jobs(ctx, base)
308+
super().__init__(ctx, path, pathinfo, **kwargs)
309+
self.jobs = Jobs(ctx, self._path)

src/posit/connect/resources.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,61 +51,59 @@ def __init__(self, params: ResourceParameters) -> None:
5151

5252

5353
class Active(ABC, Resource):
54-
def __init__(self, ctx: Context, **kwargs):
55-
"""A base class representing an active resource.
54+
def __init__(self, ctx: Context, path: str, pathinfo: str = "", /, **attributes):
55+
"""A dict abstraction for any HTTP endpoint that returns a singular resource.
5656
5757
Extends the `Resource` class and provides additional functionality for via the session context and an optional parent resource.
5858
5959
Parameters
6060
----------
6161
ctx : Context
6262
The context object containing the session and URL for API interactions.
63-
**kwargs : dict
64-
Additional keyword arguments passed to the parent `Resource` class.
63+
path : str
64+
The HTTP path component for the collection endpoint
65+
pathinfo : str
66+
The HTTP part of the path directed at a specific resource
67+
**attributes : dict
68+
Resource attributes passed
69+
70+
Attributes
71+
----------
72+
_ctx : Context
73+
The context object containing the session and URL for API interactions
74+
_path : str
75+
The HTTP path for the collection endpoint.
6576
"""
6677
params = ResourceParameters(ctx.session, ctx.url)
67-
super().__init__(params, **kwargs)
78+
super().__init__(params, **attributes)
6879
self._ctx = ctx
80+
self._path = posixpath.join(path, pathinfo)
6981

7082

7183
T = TypeVar("T", bound="Active")
7284
"""A type variable that is bound to the `Active` class"""
7385

7486

7587
class ActiveSequence(ABC, Generic[T], Sequence[T]):
76-
def __init__(self, ctx: Context, base: str, name: str, uid="guid"):
88+
def __init__(self, ctx: Context, path: str, pathinfo: str = "", uid: str = "guid"):
7789
"""A sequence abstraction for any HTTP GET endpoint that returns a collection.
7890
7991
It lazily fetches data on demand, caches the results, and allows for standard sequence operations like indexing and slicing.
8092
81-
Parameters
82-
----------
83-
ctx : Context
84-
The context object containing the session and URL for API interactions
85-
base : str
86-
The base HTTP path for the collection endpoint
87-
name : str
88-
The collection name
89-
uid : str, optional
90-
The field name used to uniquely identify records, by default "guid"
91-
9293
Attributes
9394
----------
9495
_ctx : Context
9596
The context object containing the session and URL for API interactions
9697
_path : str
9798
The HTTP path for the collection endpoint.
98-
_endpoint : Url
99-
The HTTP URL for the collection endpoint.
10099
_uid : str
101-
The default field name used to uniquely identify records.
100+
The field name used to uniquely identify records.
102101
_cache: Optional[List[T]]
103102
"""
104103
super().__init__()
105104
self._ctx = ctx
106-
self._path: str = posixpath.join(base, name)
107-
self._endpoint: Url = ctx.url + self._path
108-
self._uid: str = uid
105+
self._path = posixpath.join(path, pathinfo)
106+
self._uid = uid
109107
self._cache: Optional[List[T]] = None
110108

111109
@property
@@ -125,7 +123,8 @@ def _data(self) -> List[T]:
125123
if self._cache:
126124
return self._cache
127125

128-
response = self._ctx.session.get(self._endpoint)
126+
endpoint = self._ctx.url + self._path
127+
response = self._ctx.session.get(endpoint)
129128
results = response.json()
130129

131130
self._cache = []
@@ -155,7 +154,7 @@ def __repr__(self) -> str:
155154
return repr(self._data)
156155

157156
@abstractmethod
158-
def _create_instance(self, base: str, uid: str, /, **kwargs: Any) -> T:
157+
def _create_instance(self, path: str, pathinfo: str, /, **kwargs: Any) -> T:
159158
"""Create an instance of 'T'.
160159
161160
Returns
@@ -201,7 +200,7 @@ def find(self, uid) -> T:
201200
if result:
202201
return result
203202

204-
endpoint = self._endpoint + uid
203+
endpoint = self._ctx.url + self._path + uid
205204
response = self._ctx.session.get(endpoint)
206205
result = response.json()
207206
result = self._create_instance(self._path, uid, **result)

0 commit comments

Comments
 (0)