Skip to content

Commit

Permalink
add link to parent
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein committed Oct 23, 2024
1 parent 279fcd6 commit ab6c2cb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
29 changes: 29 additions & 0 deletions integration/tests/posit/connect/test_jobs.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 5 additions & 9 deletions src/posit/connect/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
18 changes: 13 additions & 5 deletions src/posit/connect/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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("")
Expand Down

0 comments on commit ab6c2cb

Please sign in to comment.