Skip to content

Commit

Permalink
refactor: resource parameter object (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein authored Jul 29, 2024
1 parent b04a500 commit 53d95a1
Show file tree
Hide file tree
Showing 25 changed files with 269 additions and 294 deletions.
35 changes: 14 additions & 21 deletions src/posit/connect/bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import io
from typing import List

import requests

from . import config, resources, tasks
from . import resources, tasks


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

@property
def metadata(self) -> BundleMetadata:
return BundleMetadata(
self.config, self.session, **self.get("metadata", {})
)
return BundleMetadata(self.params, **self.get("metadata", {}))

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

def deploy(self) -> tasks.Task:
Expand All @@ -164,10 +160,10 @@ def deploy(self) -> tasks.Task:
None
"""
path = f"v1/content/{self.content_guid}/deploy"
url = self.config.url + path
url = self.url + path
response = self.session.post(url, json={"bundle_id": self.id})
result = response.json()
ts = tasks.Tasks(self.config, self.session)
ts = tasks.Tasks(self.params)
return ts.get(result["task_id"])

def download(self, output: io.BufferedWriter | str) -> None:
Expand Down Expand Up @@ -202,7 +198,7 @@ def download(self, output: io.BufferedWriter | str) -> None:
)

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

def __init__(
self,
config: config.Config,
session: requests.Session,
params: resources.ResourceParameters,
content_guid: str,
) -> None:
super().__init__(config, session)
super().__init__(params)
self.content_guid = content_guid

def create(self, input: io.BufferedReader | bytes | str) -> Bundle:
Expand Down Expand Up @@ -289,10 +284,10 @@ def create(self, input: io.BufferedReader | bytes | str) -> Bundle:
)

path = f"v1/content/{self.content_guid}/bundles"
url = self.config.url + path
url = self.url + path
response = self.session.post(url, data=data)
result = response.json()
return Bundle(self.config, self.session, **result)
return Bundle(self.params, **result)

def find(self) -> List[Bundle]:
"""Find all bundles.
Expand All @@ -303,12 +298,10 @@ def find(self) -> List[Bundle]:
List of all found bundles.
"""
path = f"v1/content/{self.content_guid}/bundles"
url = self.config.url + path
url = self.url + path
response = self.session.get(url)
results = response.json()
return [
Bundle(self.config, self.session, **result) for result in results
]
return [Bundle(self.params, **result) for result in results]

def find_one(self) -> Bundle | None:
"""Find a bundle.
Expand All @@ -335,7 +328,7 @@ def get(self, id: str) -> Bundle:
The bundle with the specified ID.
"""
path = f"v1/content/{self.content_guid}/bundles/{id}"
url = self.config.url + path
url = self.url + path
response = self.session.get(url)
result = response.json()
return Bundle(self.config, self.session, **result)
return Bundle(self.params, **result)
33 changes: 18 additions & 15 deletions src/posit/connect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from requests import Response, Session

from posit.connect.resources import ResourceParameters

from . import hooks, me
from .auth import Auth
from .config import Config
Expand Down Expand Up @@ -155,12 +157,13 @@ def __init__(self, *args, **kwargs) -> None:
if "url" in kwargs and isinstance(kwargs["url"], str):
url = kwargs["url"]

self.config = Config(api_key=api_key, url=url)
self.cfg = Config(api_key=api_key, url=url)
session = Session()
session.auth = Auth(config=self.config)
session.auth = Auth(config=self.cfg)
session.hooks["response"].append(hooks.check_for_deprecation_header)
session.hooks["response"].append(hooks.handle_errors)
self.session = session
self.resource_params = ResourceParameters(session, self.cfg.url)

@property
def version(self) -> str:
Expand All @@ -184,7 +187,7 @@ def me(self) -> User:
User
The currently authenticated user.
"""
return me.get(self.config, self.session)
return me.get(self.resource_params)

@property
def oauth(self) -> OAuthIntegration:
Expand All @@ -196,7 +199,7 @@ def oauth(self) -> OAuthIntegration:
OAuthIntegration
The OAuth integration instance.
"""
return OAuthIntegration(config=self.config, session=self.session)
return OAuthIntegration(self.cfg, self.session)

@property
def groups(self) -> Groups:
Expand All @@ -207,7 +210,7 @@ def groups(self) -> Groups:
Groups
The groups resource interface.
"""
return Groups(self.config, self.session)
return Groups(self.resource_params)

@property
def tasks(self) -> Tasks:
Expand All @@ -219,7 +222,7 @@ def tasks(self) -> Tasks:
tasks.Tasks
The tasks resource instance.
"""
return Tasks(self.config, self.session)
return Tasks(self.resource_params)

@property
def users(self) -> Users:
Expand All @@ -231,7 +234,7 @@ def users(self) -> Users:
Users
The users resource instance.
"""
return Users(config=self.config, session=self.session)
return Users(self.resource_params)

@property
def content(self) -> Content:
Expand All @@ -243,7 +246,7 @@ def content(self) -> Content:
Content
The content resource instance.
"""
return Content(config=self.config, session=self.session)
return Content(self.resource_params)

@property
def metrics(self) -> Metrics:
Expand Down Expand Up @@ -271,7 +274,7 @@ def metrics(self) -> Metrics:
>>> len(events)
24
"""
return Metrics(self.config, self.session)
return Metrics(self.resource_params)

def __del__(self):
"""Close the session when the Client instance is deleted."""
Expand Down Expand Up @@ -318,7 +321,7 @@ def request(self, method: str, path: str, **kwargs) -> Response:
Response
A [](`requests.Response`) object.
"""
url = self.config.url + path
url = self.cfg.url + path
return self.session.request(method, url, **kwargs)

def get(self, path: str, **kwargs) -> Response:
Expand All @@ -339,7 +342,7 @@ def get(self, path: str, **kwargs) -> Response:
Response
A [](`requests.Response`) object.
"""
url = self.config.url + path
url = self.cfg.url + path
return self.session.get(url, **kwargs)

def post(self, path: str, **kwargs) -> Response:
Expand All @@ -360,7 +363,7 @@ def post(self, path: str, **kwargs) -> Response:
Response
A [](`requests.Response`) object.
"""
url = self.config.url + path
url = self.cfg.url + path
return self.session.post(url, **kwargs)

def put(self, path: str, **kwargs) -> Response:
Expand All @@ -381,7 +384,7 @@ def put(self, path: str, **kwargs) -> Response:
Response
A [](`requests.Response`) object.
"""
url = self.config.url + path
url = self.cfg.url + path
return self.session.put(url, **kwargs)

def patch(self, path: str, **kwargs) -> Response:
Expand All @@ -402,7 +405,7 @@ def patch(self, path: str, **kwargs) -> Response:
Response
A [](`requests.Response`) object.
"""
url = self.config.url + path
url = self.cfg.url + path
return self.session.patch(url, **kwargs)

def delete(self, path: str, **kwargs) -> Response:
Expand All @@ -423,5 +426,5 @@ def delete(self, path: str, **kwargs) -> Response:
Response
A [](`requests.Response`) object.
"""
url = self.config.url + path
url = self.cfg.url + path
return self.session.delete(url, **kwargs)
53 changes: 24 additions & 29 deletions src/posit/connect/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

from __future__ import annotations

import posixpath
import secrets
from posixpath import dirname
from typing import List, Optional, overload

from requests import Session

from . import tasks
from .bundles import Bundles
from .config import Config
from .env import EnvVars
from .permissions import Permissions
from .resources import Resource, Resources
from .resources import Resource, ResourceParameters, Resources
from .tasks import Task
from .variants import Variants

Expand Down Expand Up @@ -144,7 +142,7 @@ class ContentItem(Resource):
def delete(self) -> None:
"""Delete the content item."""
path = f"v1/content/{self.guid}"
url = self.config.url + path
url = self.url + path
self.session.delete(url)

def deploy(self) -> tasks.Task:
Expand All @@ -164,10 +162,10 @@ def deploy(self) -> tasks.Task:
None
"""
path = f"v1/content/{self.guid}/deploy"
url = self.config.url + path
url = self.url + path
response = self.session.post(url, json={"bundle_id": None})
result = response.json()
ts = tasks.Tasks(self.config, self.session)
ts = tasks.Tasks(self.params)
return ts.get(result["task_id"])

def render(self) -> Task:
Expand Down Expand Up @@ -237,7 +235,7 @@ def restart(self) -> None:
self.environment_variables.create(key, random_hash)
self.environment_variables.delete(key)
# GET via the base Connect URL to force create a new worker thread.
url = dirname(self.config.url) + f"/content/{self.guid}"
url = posixpath.join(dirname(self.url), f"content/{self.guid}")
self.session.get(url)
return None
else:
Expand Down Expand Up @@ -314,23 +312,23 @@ def update(self, *args, **kwargs) -> None:
def update(self, *args, **kwargs) -> None:
"""Update the content item."""
body = dict(*args, **kwargs)
url = self.config.url + f"v1/content/{self.guid}"
url = self.url + f"v1/content/{self.guid}"
response = self.session.patch(url, json=body)
super().update(**response.json())

# Relationships

@property
def bundles(self) -> Bundles:
return Bundles(self.config, self.session, self.guid)
return Bundles(self.params, self.guid)

@property
def environment_variables(self) -> EnvVars:
return EnvVars(self.config, self.session, self.guid)
return EnvVars(self.params, self.guid)

@property
def permissions(self) -> Permissions:
return Permissions(self.config, self.session, self.guid)
return Permissions(self.params, self.guid)

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

self["owner"] = Users(self.config, self.session).get(
self.owner_guid
)
return ContentItemOwner(self.config, self.session, **self["owner"])
self["owner"] = Users(self.params).get(self.owner_guid)
return ContentItemOwner(self.params, **self["owner"])

@property
def _variants(self) -> Variants:
return Variants(self.config, self.session, self.guid)
return Variants(self.params, self.guid)

# Properties

Expand Down Expand Up @@ -543,14 +539,11 @@ class Content(Resources):

def __init__(
self,
config: Config,
session: Session,
params: ResourceParameters,
*,
owner_guid: str | None = None,
) -> None:
self.url = config.url + "v1/content"
self.config = config
self.session = session
super().__init__(params)
self.owner_guid = owner_guid

def _get_default_params(self) -> dict:
Expand Down Expand Up @@ -656,9 +649,9 @@ def create(self, *args, **kwargs) -> ContentItem:
"""
body = dict(*args, **kwargs)
path = "v1/content"
url = self.config.url + path
url = self.url + path
response = self.session.post(url, json=body)
return ContentItem(self.config, self.session, **response.json())
return ContentItem(self.params, **response.json())

@overload
def find(
Expand Down Expand Up @@ -719,11 +712,12 @@ def find(
params.update(args)
params.update(kwargs)
params["include"] = include
response = self.session.get(self.url, params=params)
path = "v1/content"
url = self.url + path
response = self.session.get(url, params=params)
return [
ContentItem(
config=self.config,
session=self.session,
self.params,
**result,
)
for result in response.json()
Expand Down Expand Up @@ -798,6 +792,7 @@ def get(self, guid: str) -> ContentItem:
-------
ContentItem
"""
url = self.url + guid
path = f"v1/content/{guid}"
url = self.url + path
response = self.session.get(url)
return ContentItem(self.config, self.session, **response.json())
return ContentItem(self.params, **response.json())
Loading

0 comments on commit 53d95a1

Please sign in to comment.