From ab6c2cbed07efb08a3a7c030dbd1cbd38f1b8dfb Mon Sep 17 00:00:00 2001 From: tdstein Date: Wed, 23 Oct 2024 12:44:06 -0400 Subject: [PATCH] add link to parent --- integration/tests/posit/connect/test_jobs.py | 29 ++++++++++++++++++++ src/posit/connect/jobs.py | 14 ++++------ src/posit/connect/resources.py | 18 ++++++++---- 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 integration/tests/posit/connect/test_jobs.py diff --git a/integration/tests/posit/connect/test_jobs.py b/integration/tests/posit/connect/test_jobs.py new file mode 100644 index 00000000..5cf5723f --- /dev/null +++ b/integration/tests/posit/connect/test_jobs.py @@ -0,0 +1,29 @@ +from pathlib import Path + +from posit import connect + + +class TestContent: + @classmethod + def setup_class(cls): + cls.client = connect.Client() + cls.content = cls.client.content.create(name="example-flask-minimal") + + @classmethod + def teardown_class(cls): + cls.content.delete() + assert cls.client.content.count() == 0 + + def test(self): + content = self.content + + path = Path("../../../resources/connect/bundles/example-flask-minimal/bundle.tar.gz") + path = Path(__file__).parent / path + path = path.resolve() + path = str(path) + + bundle = content.bundles.create(path) + bundle.deploy() + + jobs = content.jobs + assert len(jobs) == 1 diff --git a/src/posit/connect/jobs.py b/src/posit/connect/jobs.py index 66b28b6e..c18a279d 100644 --- a/src/posit/connect/jobs.py +++ b/src/posit/connect/jobs.py @@ -99,12 +99,13 @@ class _Job(TypedDict): tag: Required[JobTag] """A tag categorizing the job type. Options are build_jupyter, build_report, build_site, configure_report, git, packrat_restore, python_restore, render_shiny, run_api, run_app, run_bokeh_app, run_dash_app, run_fastapi_app, run_pyshiny_app, run_python_api, run_streamlit, run_tensorflow, run_voila_app, testing, unknown, val_py_ext_pkg, val_r_ext_pkg, and val_r_install.""" - def __init__(self, /, params, **kwargs: Unpack[_Job]): - super().__init__(params, **kwargs) + def __init__(self, ctx, parent: Active, **kwargs: Unpack[_Job]): + super().__init__(ctx, parent, **kwargs) + self._parent = parent @property def _endpoint(self) -> str: - return self._ctx.url + f"v1/content/{self['app_id']}/jobs/{self['key']}" + return self._ctx.url + f"v1/content/{self._parent['guid']}/jobs/{self['key']}" def destroy(self) -> None: """Destroy the job. @@ -128,7 +129,7 @@ class Jobs(ActiveFinderMethods[Job]): _uid = "key" def __init__(self, cls, ctx, parent: Active): - super().__init__(cls, ctx) + super().__init__(cls, ctx, parent) self._parent = parent @property @@ -267,11 +268,6 @@ def find_by(self, **conditions) -> Optional[Job]: class JobsMixin(Active, Resource): """Mixin class to add a jobs attribute to a resource.""" - class HasGuid(TypedDict): - """Has a guid.""" - - guid: Required[str] - def __init__(self, ctx, **kwargs): super().__init__(ctx, **kwargs) self.jobs = Jobs(Job, ctx, self) diff --git a/src/posit/connect/resources.py b/src/posit/connect/resources.py index 18e70594..0630c9dd 100644 --- a/src/posit/connect/resources.py +++ b/src/posit/connect/resources.py @@ -2,7 +2,7 @@ import warnings from abc import ABC, abstractmethod from dataclasses import dataclass -from typing import Any, Generic, List, Optional, Sequence, Type, TypeVar +from typing import Any, Generic, List, Optional, Sequence, Type, TypeVar, overload import requests @@ -54,17 +54,19 @@ def __init__(self, params: ResourceParameters) -> None: class Active(Resource): - def __init__(self, ctx: Context, **kwargs): + def __init__(self, ctx: Context, parent: Optional["Active"] = None, **kwargs): params = ResourceParameters(ctx.session, ctx.url) super().__init__(params, **kwargs) self._ctx = ctx + self._parent = parent class ActiveReader(ABC, Generic[T], Sequence[T]): - def __init__(self, cls: Type[T], ctx: Context): + def __init__(self, cls: Type[T], ctx: Context, parent: Optional[Active] = None): super().__init__() self._cls = cls self._ctx = ctx + self._parent = parent self._cache = None @property @@ -79,9 +81,15 @@ def _data(self) -> List[T]: response = self._ctx.session.get(self._endpoint) results = response.json() - self._cache = [self._cls(self._ctx, **result) for result in results] + self._cache = [self._cls(self._ctx, self._parent, **result) for result in results] return self._cache + @overload + def __getitem__(self, index: int) -> T: ... + + @overload + def __getitem__(self, index: slice) -> Sequence[T]: ... + def __getitem__(self, index): """Retrieve an item or slice from the sequence.""" return self._data[index] @@ -112,7 +120,7 @@ def find(self, uid) -> T: endpoint = posixpath.join(self._endpoint + uid) response = self._ctx.session.get(endpoint) result = response.json() - result = self._cls(self._ctx, **result) + result = self._cls(self._ctx, self._parent, **result) if not result: raise ValueError("")