Skip to content

Commit 53d95a1

Browse files
authored
refactor: resource parameter object (#240)
1 parent b04a500 commit 53d95a1

25 files changed

+269
-294
lines changed

src/posit/connect/bundles.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import io
66
from typing import List
77

8-
import requests
9-
10-
from . import config, resources, tasks
8+
from . import resources, tasks
119

1210

1311
class BundleMetadata(resources.Resource):
@@ -137,14 +135,12 @@ def size(self) -> int | None:
137135

138136
@property
139137
def metadata(self) -> BundleMetadata:
140-
return BundleMetadata(
141-
self.config, self.session, **self.get("metadata", {})
142-
)
138+
return BundleMetadata(self.params, **self.get("metadata", {}))
143139

144140
def delete(self) -> None:
145141
"""Delete the bundle."""
146142
path = f"v1/content/{self.content_guid}/bundles/{self.id}"
147-
url = self.config.url + path
143+
url = self.url + path
148144
self.session.delete(url)
149145

150146
def deploy(self) -> tasks.Task:
@@ -164,10 +160,10 @@ def deploy(self) -> tasks.Task:
164160
None
165161
"""
166162
path = f"v1/content/{self.content_guid}/deploy"
167-
url = self.config.url + path
163+
url = self.url + path
168164
response = self.session.post(url, json={"bundle_id": self.id})
169165
result = response.json()
170-
ts = tasks.Tasks(self.config, self.session)
166+
ts = tasks.Tasks(self.params)
171167
return ts.get(result["task_id"])
172168

173169
def download(self, output: io.BufferedWriter | str) -> None:
@@ -202,7 +198,7 @@ def download(self, output: io.BufferedWriter | str) -> None:
202198
)
203199

204200
path = f"v1/content/{self.content_guid}/bundles/{self.id}/download"
205-
url = self.config.url + path
201+
url = self.url + path
206202
response = self.session.get(url, stream=True)
207203
if isinstance(output, io.BufferedWriter):
208204
for chunk in response.iter_content():
@@ -233,11 +229,10 @@ class Bundles(resources.Resources):
233229

234230
def __init__(
235231
self,
236-
config: config.Config,
237-
session: requests.Session,
232+
params: resources.ResourceParameters,
238233
content_guid: str,
239234
) -> None:
240-
super().__init__(config, session)
235+
super().__init__(params)
241236
self.content_guid = content_guid
242237

243238
def create(self, input: io.BufferedReader | bytes | str) -> Bundle:
@@ -289,10 +284,10 @@ def create(self, input: io.BufferedReader | bytes | str) -> Bundle:
289284
)
290285

291286
path = f"v1/content/{self.content_guid}/bundles"
292-
url = self.config.url + path
287+
url = self.url + path
293288
response = self.session.post(url, data=data)
294289
result = response.json()
295-
return Bundle(self.config, self.session, **result)
290+
return Bundle(self.params, **result)
296291

297292
def find(self) -> List[Bundle]:
298293
"""Find all bundles.
@@ -303,12 +298,10 @@ def find(self) -> List[Bundle]:
303298
List of all found bundles.
304299
"""
305300
path = f"v1/content/{self.content_guid}/bundles"
306-
url = self.config.url + path
301+
url = self.url + path
307302
response = self.session.get(url)
308303
results = response.json()
309-
return [
310-
Bundle(self.config, self.session, **result) for result in results
311-
]
304+
return [Bundle(self.params, **result) for result in results]
312305

313306
def find_one(self) -> Bundle | None:
314307
"""Find a bundle.
@@ -335,7 +328,7 @@ def get(self, id: str) -> Bundle:
335328
The bundle with the specified ID.
336329
"""
337330
path = f"v1/content/{self.content_guid}/bundles/{id}"
338-
url = self.config.url + path
331+
url = self.url + path
339332
response = self.session.get(url)
340333
result = response.json()
341-
return Bundle(self.config, self.session, **result)
334+
return Bundle(self.params, **result)

src/posit/connect/client.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from requests import Response, Session
88

9+
from posit.connect.resources import ResourceParameters
10+
911
from . import hooks, me
1012
from .auth import Auth
1113
from .config import Config
@@ -155,12 +157,13 @@ def __init__(self, *args, **kwargs) -> None:
155157
if "url" in kwargs and isinstance(kwargs["url"], str):
156158
url = kwargs["url"]
157159

158-
self.config = Config(api_key=api_key, url=url)
160+
self.cfg = Config(api_key=api_key, url=url)
159161
session = Session()
160-
session.auth = Auth(config=self.config)
162+
session.auth = Auth(config=self.cfg)
161163
session.hooks["response"].append(hooks.check_for_deprecation_header)
162164
session.hooks["response"].append(hooks.handle_errors)
163165
self.session = session
166+
self.resource_params = ResourceParameters(session, self.cfg.url)
164167

165168
@property
166169
def version(self) -> str:
@@ -184,7 +187,7 @@ def me(self) -> User:
184187
User
185188
The currently authenticated user.
186189
"""
187-
return me.get(self.config, self.session)
190+
return me.get(self.resource_params)
188191

189192
@property
190193
def oauth(self) -> OAuthIntegration:
@@ -196,7 +199,7 @@ def oauth(self) -> OAuthIntegration:
196199
OAuthIntegration
197200
The OAuth integration instance.
198201
"""
199-
return OAuthIntegration(config=self.config, session=self.session)
202+
return OAuthIntegration(self.cfg, self.session)
200203

201204
@property
202205
def groups(self) -> Groups:
@@ -207,7 +210,7 @@ def groups(self) -> Groups:
207210
Groups
208211
The groups resource interface.
209212
"""
210-
return Groups(self.config, self.session)
213+
return Groups(self.resource_params)
211214

212215
@property
213216
def tasks(self) -> Tasks:
@@ -219,7 +222,7 @@ def tasks(self) -> Tasks:
219222
tasks.Tasks
220223
The tasks resource instance.
221224
"""
222-
return Tasks(self.config, self.session)
225+
return Tasks(self.resource_params)
223226

224227
@property
225228
def users(self) -> Users:
@@ -231,7 +234,7 @@ def users(self) -> Users:
231234
Users
232235
The users resource instance.
233236
"""
234-
return Users(config=self.config, session=self.session)
237+
return Users(self.resource_params)
235238

236239
@property
237240
def content(self) -> Content:
@@ -243,7 +246,7 @@ def content(self) -> Content:
243246
Content
244247
The content resource instance.
245248
"""
246-
return Content(config=self.config, session=self.session)
249+
return Content(self.resource_params)
247250

248251
@property
249252
def metrics(self) -> Metrics:
@@ -271,7 +274,7 @@ def metrics(self) -> Metrics:
271274
>>> len(events)
272275
24
273276
"""
274-
return Metrics(self.config, self.session)
277+
return Metrics(self.resource_params)
275278

276279
def __del__(self):
277280
"""Close the session when the Client instance is deleted."""
@@ -318,7 +321,7 @@ def request(self, method: str, path: str, **kwargs) -> Response:
318321
Response
319322
A [](`requests.Response`) object.
320323
"""
321-
url = self.config.url + path
324+
url = self.cfg.url + path
322325
return self.session.request(method, url, **kwargs)
323326

324327
def get(self, path: str, **kwargs) -> Response:
@@ -339,7 +342,7 @@ def get(self, path: str, **kwargs) -> Response:
339342
Response
340343
A [](`requests.Response`) object.
341344
"""
342-
url = self.config.url + path
345+
url = self.cfg.url + path
343346
return self.session.get(url, **kwargs)
344347

345348
def post(self, path: str, **kwargs) -> Response:
@@ -360,7 +363,7 @@ def post(self, path: str, **kwargs) -> Response:
360363
Response
361364
A [](`requests.Response`) object.
362365
"""
363-
url = self.config.url + path
366+
url = self.cfg.url + path
364367
return self.session.post(url, **kwargs)
365368

366369
def put(self, path: str, **kwargs) -> Response:
@@ -381,7 +384,7 @@ def put(self, path: str, **kwargs) -> Response:
381384
Response
382385
A [](`requests.Response`) object.
383386
"""
384-
url = self.config.url + path
387+
url = self.cfg.url + path
385388
return self.session.put(url, **kwargs)
386389

387390
def patch(self, path: str, **kwargs) -> Response:
@@ -402,7 +405,7 @@ def patch(self, path: str, **kwargs) -> Response:
402405
Response
403406
A [](`requests.Response`) object.
404407
"""
405-
url = self.config.url + path
408+
url = self.cfg.url + path
406409
return self.session.patch(url, **kwargs)
407410

408411
def delete(self, path: str, **kwargs) -> Response:
@@ -423,5 +426,5 @@ def delete(self, path: str, **kwargs) -> Response:
423426
Response
424427
A [](`requests.Response`) object.
425428
"""
426-
url = self.config.url + path
429+
url = self.cfg.url + path
427430
return self.session.delete(url, **kwargs)

src/posit/connect/content.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
from __future__ import annotations
44

5+
import posixpath
56
import secrets
67
from posixpath import dirname
78
from typing import List, Optional, overload
89

9-
from requests import Session
10-
1110
from . import tasks
1211
from .bundles import Bundles
13-
from .config import Config
1412
from .env import EnvVars
1513
from .permissions import Permissions
16-
from .resources import Resource, Resources
14+
from .resources import Resource, ResourceParameters, Resources
1715
from .tasks import Task
1816
from .variants import Variants
1917

@@ -144,7 +142,7 @@ class ContentItem(Resource):
144142
def delete(self) -> None:
145143
"""Delete the content item."""
146144
path = f"v1/content/{self.guid}"
147-
url = self.config.url + path
145+
url = self.url + path
148146
self.session.delete(url)
149147

150148
def deploy(self) -> tasks.Task:
@@ -164,10 +162,10 @@ def deploy(self) -> tasks.Task:
164162
None
165163
"""
166164
path = f"v1/content/{self.guid}/deploy"
167-
url = self.config.url + path
165+
url = self.url + path
168166
response = self.session.post(url, json={"bundle_id": None})
169167
result = response.json()
170-
ts = tasks.Tasks(self.config, self.session)
168+
ts = tasks.Tasks(self.params)
171169
return ts.get(result["task_id"])
172170

173171
def render(self) -> Task:
@@ -237,7 +235,7 @@ def restart(self) -> None:
237235
self.environment_variables.create(key, random_hash)
238236
self.environment_variables.delete(key)
239237
# GET via the base Connect URL to force create a new worker thread.
240-
url = dirname(self.config.url) + f"/content/{self.guid}"
238+
url = posixpath.join(dirname(self.url), f"content/{self.guid}")
241239
self.session.get(url)
242240
return None
243241
else:
@@ -314,23 +312,23 @@ def update(self, *args, **kwargs) -> None:
314312
def update(self, *args, **kwargs) -> None:
315313
"""Update the content item."""
316314
body = dict(*args, **kwargs)
317-
url = self.config.url + f"v1/content/{self.guid}"
315+
url = self.url + f"v1/content/{self.guid}"
318316
response = self.session.patch(url, json=body)
319317
super().update(**response.json())
320318

321319
# Relationships
322320

323321
@property
324322
def bundles(self) -> Bundles:
325-
return Bundles(self.config, self.session, self.guid)
323+
return Bundles(self.params, self.guid)
326324

327325
@property
328326
def environment_variables(self) -> EnvVars:
329-
return EnvVars(self.config, self.session, self.guid)
327+
return EnvVars(self.params, self.guid)
330328

331329
@property
332330
def permissions(self) -> Permissions:
333-
return Permissions(self.config, self.session, self.guid)
331+
return Permissions(self.params, self.guid)
334332

335333
@property
336334
def owner(self) -> ContentItemOwner:
@@ -340,14 +338,12 @@ def owner(self) -> ContentItemOwner:
340338
# If it's not included, we can retrieve the information by `owner_guid`
341339
from .users import Users
342340

343-
self["owner"] = Users(self.config, self.session).get(
344-
self.owner_guid
345-
)
346-
return ContentItemOwner(self.config, self.session, **self["owner"])
341+
self["owner"] = Users(self.params).get(self.owner_guid)
342+
return ContentItemOwner(self.params, **self["owner"])
347343

348344
@property
349345
def _variants(self) -> Variants:
350-
return Variants(self.config, self.session, self.guid)
346+
return Variants(self.params, self.guid)
351347

352348
# Properties
353349

@@ -543,14 +539,11 @@ class Content(Resources):
543539

544540
def __init__(
545541
self,
546-
config: Config,
547-
session: Session,
542+
params: ResourceParameters,
548543
*,
549544
owner_guid: str | None = None,
550545
) -> None:
551-
self.url = config.url + "v1/content"
552-
self.config = config
553-
self.session = session
546+
super().__init__(params)
554547
self.owner_guid = owner_guid
555548

556549
def _get_default_params(self) -> dict:
@@ -656,9 +649,9 @@ def create(self, *args, **kwargs) -> ContentItem:
656649
"""
657650
body = dict(*args, **kwargs)
658651
path = "v1/content"
659-
url = self.config.url + path
652+
url = self.url + path
660653
response = self.session.post(url, json=body)
661-
return ContentItem(self.config, self.session, **response.json())
654+
return ContentItem(self.params, **response.json())
662655

663656
@overload
664657
def find(
@@ -719,11 +712,12 @@ def find(
719712
params.update(args)
720713
params.update(kwargs)
721714
params["include"] = include
722-
response = self.session.get(self.url, params=params)
715+
path = "v1/content"
716+
url = self.url + path
717+
response = self.session.get(url, params=params)
723718
return [
724719
ContentItem(
725-
config=self.config,
726-
session=self.session,
720+
self.params,
727721
**result,
728722
)
729723
for result in response.json()
@@ -798,6 +792,7 @@ def get(self, guid: str) -> ContentItem:
798792
-------
799793
ContentItem
800794
"""
801-
url = self.url + guid
795+
path = f"v1/content/{guid}"
796+
url = self.url + path
802797
response = self.session.get(url)
803-
return ContentItem(self.config, self.session, **response.json())
798+
return ContentItem(self.params, **response.json())

0 commit comments

Comments
 (0)