Skip to content

Commit 05829fe

Browse files
committed
merge main
2 parents 76e8eee + e6809f3 commit 05829fe

32 files changed

+374
-434
lines changed

src/posit/connect/bundles.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
from __future__ import annotations
44

55
import io
6-
from typing import List
6+
from typing import TYPE_CHECKING, List
77

88
from . import resources, tasks
99

10+
if TYPE_CHECKING:
11+
from .context import Context
12+
1013

1114
class BundleMetadata(resources.Resource):
1215
pass
@@ -15,13 +18,12 @@ class BundleMetadata(resources.Resource):
1518
class Bundle(resources.Resource):
1619
@property
1720
def metadata(self) -> BundleMetadata:
18-
return BundleMetadata(self.params, **self.get("metadata", {}))
21+
return BundleMetadata(self._ctx, **self.get("metadata", {}))
1922

2023
def delete(self) -> None:
2124
"""Delete the bundle."""
2225
path = f"v1/content/{self['content_guid']}/bundles/{self['id']}"
23-
url = self.params.url + path
24-
self.params.session.delete(url)
26+
self._ctx.client.delete(path)
2527

2628
def deploy(self) -> tasks.Task:
2729
"""Deploy the bundle.
@@ -40,10 +42,9 @@ def deploy(self) -> tasks.Task:
4042
None
4143
"""
4244
path = f"v1/content/{self['content_guid']}/deploy"
43-
url = self.params.url + path
44-
response = self.params.session.post(url, json={"bundle_id": self["id"]})
45+
response = self._ctx.client.post(path, json={"bundle_id": self["id"]})
4546
result = response.json()
46-
ts = tasks.Tasks(self.params)
47+
ts = tasks.Tasks(self._ctx)
4748
return ts.get(result["task_id"])
4849

4950
def download(self, output: io.BufferedWriter | str) -> None:
@@ -78,8 +79,7 @@ def download(self, output: io.BufferedWriter | str) -> None:
7879
)
7980

8081
path = f"v1/content/{self['content_guid']}/bundles/{self['id']}/download"
81-
url = self.params.url + path
82-
response = self.params.session.get(url, stream=True)
82+
response = self._ctx.client.get(path, stream=True)
8383
if isinstance(output, io.BufferedWriter):
8484
for chunk in response.iter_content():
8585
output.write(chunk)
@@ -109,10 +109,10 @@ class Bundles(resources.Resources):
109109

110110
def __init__(
111111
self,
112-
params: resources.ResourceParameters,
112+
ctx: Context,
113113
content_guid: str,
114114
) -> None:
115-
super().__init__(params)
115+
super().__init__(ctx)
116116
self.content_guid = content_guid
117117

118118
def create(self, archive: io.BufferedReader | bytes | str) -> Bundle:
@@ -164,10 +164,9 @@ def create(self, archive: io.BufferedReader | bytes | str) -> Bundle:
164164
)
165165

166166
path = f"v1/content/{self.content_guid}/bundles"
167-
url = self.params.url + path
168-
response = self.params.session.post(url, data=data)
167+
response = self._ctx.client.post(path, data=data)
169168
result = response.json()
170-
return Bundle(self.params, **result)
169+
return Bundle(self._ctx, **result)
171170

172171
def find(self) -> List[Bundle]:
173172
"""Find all bundles.
@@ -178,10 +177,9 @@ def find(self) -> List[Bundle]:
178177
List of all found bundles.
179178
"""
180179
path = f"v1/content/{self.content_guid}/bundles"
181-
url = self.params.url + path
182-
response = self.params.session.get(url)
180+
response = self._ctx.client.get(path)
183181
results = response.json()
184-
return [Bundle(self.params, **result) for result in results]
182+
return [Bundle(self._ctx, **result) for result in results]
185183

186184
def find_one(self) -> Bundle | None:
187185
"""Find a bundle.
@@ -208,7 +206,6 @@ def get(self, uid: str) -> Bundle:
208206
The bundle with the specified ID.
209207
"""
210208
path = f"v1/content/{self.content_guid}/bundles/{uid}"
211-
url = self.params.url + path
212-
response = self.params.session.get(url)
209+
response = self._ctx.client.get(path)
213210
result = response.json()
214-
return Bundle(self.params, **result)
211+
return Bundle(self._ctx, **result)

src/posit/connect/client.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .groups import Groups
1515
from .metrics import Metrics
1616
from .oauth import OAuth
17-
from .resources import ResourceParameters, _PaginatedResourceSequence, _ResourceSequence
17+
from .resources import _PaginatedResourceSequence, _ResourceSequence
1818
from .system import System
1919
from .tags import Tags
2020
from .tasks import Tasks
@@ -23,7 +23,7 @@
2323

2424
if TYPE_CHECKING:
2525
from .environments import Environments
26-
from .packages import _Packages
26+
from .packages import Packages
2727

2828

2929
class Client(ContextManager):
@@ -160,7 +160,6 @@ def __init__(self, *args, **kwargs) -> None:
160160
session.hooks["response"].append(hooks.check_for_deprecation_header)
161161
session.hooks["response"].append(hooks.handle_errors)
162162
self.session = session
163-
self.resource_params = ResourceParameters(session, self.cfg.url)
164163
self._ctx = Context(self)
165164

166165
@property
@@ -208,7 +207,7 @@ def tasks(self) -> Tasks:
208207
tasks.Tasks
209208
The tasks resource instance.
210209
"""
211-
return Tasks(self.resource_params)
210+
return Tasks(self._ctx)
212211

213212
@property
214213
def users(self) -> Users:
@@ -282,7 +281,7 @@ def metrics(self) -> Metrics:
282281
>>> len(events)
283282
24
284283
"""
285-
return Metrics(self.resource_params)
284+
return Metrics(self._ctx)
286285

287286
@property
288287
@requires(version="2024.08.0")
@@ -295,16 +294,16 @@ def oauth(self) -> OAuth:
295294
OAuth
296295
The oauth API instance.
297296
"""
298-
return OAuth(self.resource_params, self.cfg.api_key)
297+
return OAuth(self._ctx, self.cfg.api_key)
299298

300299
@property
301300
@requires(version="2024.11.0")
302-
def packages(self) -> _Packages:
301+
def packages(self) -> Packages:
303302
return _PaginatedResourceSequence(self._ctx, "v1/packages", uid="name")
304303

305304
@property
306305
def vanities(self) -> Vanities:
307-
return Vanities(self.resource_params)
306+
return Vanities(self._ctx)
308307

309308
@property
310309
def system(self) -> System:

src/posit/connect/content.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
from .resources import (
3333
Active,
3434
Resource,
35-
ResourceParameters,
3635
Resources,
3736
_Resource,
38-
_ResourcePatch,
3937
_ResourceSequence,
4038
_ResourceUpdatePatchMixin,
4139
)
@@ -46,7 +44,7 @@
4644
if TYPE_CHECKING:
4745
from .context import Context
4846
from .jobs import Jobs
49-
from .packages import _ContentPackages
47+
from .packages import ContentPackages
5048
from .tasks import Task
5149

5250

@@ -115,13 +113,13 @@ def update(
115113

116114

117115
class ContentItemOAuth(Resource):
118-
def __init__(self, params: ResourceParameters, content_guid: str) -> None:
119-
super().__init__(params)
116+
def __init__(self, ctx: Context, content_guid: str) -> None:
117+
super().__init__(ctx)
120118
self["content_guid"] = content_guid
121119

122120
@property
123121
def associations(self) -> ContentItemAssociations:
124-
return ContentItemAssociations(self.params, content_guid=self["content_guid"])
122+
return ContentItemAssociations(self._ctx, content_guid=self["content_guid"])
125123

126124

127125
class ContentItemOwner(Resource):
@@ -208,12 +206,12 @@ def __init__(
208206
def __getitem__(self, key: Any) -> Any:
209207
v = super().__getitem__(key)
210208
if key == "owner" and isinstance(v, dict):
211-
return ContentItemOwner(params=self.params, **v)
209+
return ContentItemOwner(self._ctx, **v)
212210
return v
213211

214212
@property
215213
def oauth(self) -> ContentItemOAuth:
216-
return ContentItemOAuth(self.params, content_guid=self["guid"])
214+
return ContentItemOAuth(self._ctx, content_guid=self["guid"])
217215

218216
@property
219217
def repository(self) -> ContentItemRepository | None:
@@ -270,8 +268,7 @@ def create_repository(self, /, **attributes) -> ContentItemRepository:
270268
def delete(self) -> None:
271269
"""Delete the content item."""
272270
path = f"v1/content/{self['guid']}"
273-
url = self._ctx.url + path
274-
self._ctx.session.delete(url)
271+
self._ctx.client.delete(path)
275272

276273
def deploy(self) -> tasks.Task:
277274
"""Deploy the content.
@@ -290,10 +287,9 @@ def deploy(self) -> tasks.Task:
290287
None
291288
"""
292289
path = f"v1/content/{self['guid']}/deploy"
293-
url = self._ctx.url + path
294-
response = self._ctx.session.post(url, json={"bundle_id": None})
290+
response = self._ctx.client.post(path, json={"bundle_id": None})
295291
result = response.json()
296-
ts = tasks.Tasks(self.params)
292+
ts = tasks.Tasks(self._ctx)
297293
return ts.get(result["task_id"])
298294

299295
def render(self) -> Task:
@@ -346,8 +342,8 @@ def restart(self) -> None:
346342
self.environment_variables.create(key, unix_epoch_in_seconds)
347343
self.environment_variables.delete(key)
348344
# GET via the base Connect URL to force create a new worker thread.
349-
url = posixpath.join(dirname(self._ctx.url), f"content/{self['guid']}")
350-
self._ctx.session.get(url)
345+
path = f"../content/{self['guid']}"
346+
self._ctx.client.get(path)
351347
return None
352348
else:
353349
raise ValueError(
@@ -417,23 +413,22 @@ def update(
417413
-------
418414
None
419415
"""
420-
url = self._ctx.url + f"v1/content/{self['guid']}"
421-
response = self._ctx.session.patch(url, json=attrs)
416+
response = self._ctx.client.patch(f"v1/content/{self['guid']}", json=attrs)
422417
super().update(**response.json())
423418

424419
# Relationships
425420

426421
@property
427422
def bundles(self) -> Bundles:
428-
return Bundles(self.params, self["guid"])
423+
return Bundles(self._ctx, self["guid"])
429424

430425
@property
431426
def environment_variables(self) -> EnvVars:
432-
return EnvVars(self.params, self["guid"])
427+
return EnvVars(self._ctx, self["guid"])
433428

434429
@property
435430
def permissions(self) -> Permissions:
436-
return Permissions(self.params, self["guid"])
431+
return Permissions(self._ctx, self["guid"])
437432

438433
@property
439434
def owner(self) -> dict:
@@ -450,7 +445,7 @@ def owner(self) -> dict:
450445

451446
@property
452447
def _variants(self) -> Variants:
453-
return Variants(self.params, self["guid"])
448+
return Variants(self._ctx, self["guid"])
454449

455450
@property
456451
def is_interactive(self) -> bool:
@@ -494,7 +489,7 @@ def jobs(self) -> Jobs:
494489

495490
@property
496491
@requires(version="2024.11.0")
497-
def packages(self) -> _ContentPackages:
492+
def packages(self) -> ContentPackages:
498493
path = posixpath.join(self._path, "packages")
499494
return _ResourceSequence(self._ctx, path, uid="name")
500495

@@ -518,7 +513,7 @@ def __init__(
518513
*,
519514
owner_guid: str | None = None,
520515
) -> None:
521-
super().__init__(ctx.client.resource_params)
516+
super().__init__(ctx)
522517
self.owner_guid = owner_guid
523518
self._ctx = ctx
524519

@@ -594,9 +589,7 @@ def create(
594589
-------
595590
ContentItem
596591
"""
597-
path = "v1/content"
598-
url = self._ctx.url + path
599-
response = self._ctx.session.post(url, json=attrs)
592+
response = self._ctx.client.post("v1/content", json=attrs)
600593
return ContentItem(self._ctx, **response.json())
601594

602595
@overload
@@ -682,9 +675,7 @@ def find(self, include: Optional[str | list[Any]] = None, **conditions) -> List[
682675
if self.owner_guid:
683676
conditions["owner_guid"] = self.owner_guid
684677

685-
path = "v1/content"
686-
url = self._ctx.url + path
687-
response = self._ctx.session.get(url, params=conditions)
678+
response = self._ctx.client.get("v1/content", params=conditions)
688679
return [
689680
ContentItem(
690681
self._ctx,
@@ -855,7 +846,5 @@ def get(self, guid: str) -> ContentItem:
855846
-------
856847
ContentItem
857848
"""
858-
path = f"v1/content/{guid}"
859-
url = self._ctx.url + path
860-
response = self._ctx.session.get(url)
849+
response = self._ctx.client.get(f"v1/content/{guid}")
861850
return ContentItem(self._ctx, **response.json())

src/posit/connect/context.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
from packaging.version import Version
88

99
if TYPE_CHECKING:
10-
import requests
11-
1210
from .client import Client
13-
from .urls import Url
1411

1512

1613
def requires(version: str):
@@ -31,17 +28,14 @@ def wrapper(instance: ContextManager, *args, **kwargs):
3128

3229
class Context:
3330
def __init__(self, client: Client):
34-
self.session: requests.Session = client.session
35-
self.url: Url = client.cfg.url
3631
# Since this is a child object of the client, we use a weak reference to avoid circular
3732
# references (which would prevent garbage collection)
3833
self.client: Client = weakref.proxy(client)
3934

4035
@property
4136
def version(self) -> str | None:
4237
if not hasattr(self, "_version"):
43-
endpoint = self.url + "server_settings"
44-
response = self.session.get(endpoint)
38+
response = self.client.get("server_settings")
4539
result = response.json()
4640
self._version: str | None = result.get("version")
4741

0 commit comments

Comments
 (0)