From 5998b15cab67981bac7f495bc93d3ac254aeebf7 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Tue, 12 Nov 2024 15:06:23 -0500 Subject: [PATCH] Implement Variant with ResourceDict --- src/posit/connect/resources.py | 13 ++++++++++++- src/posit/connect/variants.py | 22 ++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/posit/connect/resources.py b/src/posit/connect/resources.py index bfef8365..c60fe493 100644 --- a/src/posit/connect/resources.py +++ b/src/posit/connect/resources.py @@ -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 @@ -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 diff --git a/src/posit/connect/variants.py b/src/posit/connect/variants.py index eb6a28c0..614121be 100644 --- a/src/posit/connect/variants.py +++ b/src/posit/connect/variants.py @@ -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) @@ -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 + ]