Skip to content

Commit

Permalink
Implement Variant with ResourceDict
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Nov 12, 2024
1 parent 7efb8fa commit 5998b15
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/posit/connect/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Generic, List, Optional, Sequence, TypeVar, overload

from .context import Context

if TYPE_CHECKING:
import requests

from ._typing_extensions import Self
from .context import Context
from .urls import Url


Expand All @@ -31,6 +32,16 @@ class ResourceParameters:
url: Url


def context_to_resource_parameters(ctx: Context) -> ResourceParameters:
"""Temp method to aid in transitioning from `Context` to `ResourceParameters`."""
return ResourceParameters(ctx.session, ctx.url)


def resource_parameters_to_context(params: ResourceParameters) -> Context:
"""Temp method to aid in transitioning from `ResourceParameters` to `Context`."""
return Context(params.session, params.url)


class Resource(dict):
def __init__(self, /, params: ResourceParameters, **kwargs):
self.params = params
Expand Down
22 changes: 16 additions & 6 deletions src/posit/connect/variants.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from typing import List

from .resources import Resource, ResourceParameters, Resources
from ._active import ResourceDict
from .resources import (
ResourceParameters,
Resources,
context_to_resource_parameters,
resource_parameters_to_context,
)
from .tasks import Task


class Variant(Resource):
class Variant(ResourceDict):
def render(self) -> Task:
# TODO Move to within Task logic?
path = f"variants/{self['id']}/render"
url = self.params.url + path
response = self.params.session.post(url)
return Task(self.params, **response.json())
url = self._ctx.url + path
response = self._ctx.session.post(url)
return Task(context_to_resource_parameters(self._ctx), **response.json())


# TODO; Inherit from ActiveList
class Variants(Resources):
def __init__(self, params: ResourceParameters, content_guid: str) -> None:
super().__init__(params)
Expand All @@ -22,4 +30,6 @@ def find(self) -> List[Variant]:
url = self.params.url + path
response = self.params.session.get(url)
results = response.json() or []
return [Variant(self.params, **result) for result in results]
return [
Variant(resource_parameters_to_context(self.params), **result) for result in results
]

0 comments on commit 5998b15

Please sign in to comment.