Skip to content

Commit

Permalink
feat: initial support for getting content
Browse files Browse the repository at this point in the history
  • Loading branch information
nealrichardson committed Feb 24, 2024
1 parent 26c2730 commit dc05afa
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/posit/connect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .auth import Auth
from .config import Config
from .content import Content
from .users import Users


Expand Down Expand Up @@ -48,6 +49,10 @@ def connect_version(self):
def users(self) -> Users:
return Users(config=self.config, session=self.session)

@property
def content(self) -> Content:
return Content(config=self.config, session=self.session)

def __del__(self):
"""
Close the session when the Client instance is deleted.
Expand Down
42 changes: 42 additions & 0 deletions src/posit/connect/content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import annotations

from typing import Callable, List, TypedDict

from requests import Session

from . import urls

from .config import Config


class ContentItem(TypedDict, total=False):
# TODO: specify types
pass


class Content:
def __init__(self, config: Config, session: Session) -> None:
self.url = urls.append_path(config.url, "v1/content")
self.config = config
self.session = session

def find(
self, filter: Callable[[ContentItem], bool] = lambda _: True
) -> List[ContentItem]:
results = self.session.get(self.url).json()
return [ContentItem(**c) for c in results if filter(ContentItem(**c))]

def find_one(
self, filter: Callable[[ContentItem], bool] = lambda _: True
) -> ContentItem | None:
results = self.session.get(self.url).json()
for c in results:
content_item = ContentItem(**c)
if filter(content_item):
return content_item
return None

def get(self, id: str) -> ContentItem:
url = urls.append_path(self.url, id)
response = self.session.get(url)
return ContentItem(**response.json())
212 changes: 212 additions & 0 deletions src/posit/connect/content_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import responses

from .client import Client


class TestContents:

@responses.activate
def test_get_all_content(self):
responses.get(
"https://connect.example/__api__/v1/content",
json=[
{
"guid": "39c8d85a-37ae-4b8b-8655-30a06adff2f1",
"name": "team-admin-dashboard",
"title": "Team admin dashboard",
"description": "",
"access_type": "logged_in",
"connection_timeout": None,
"read_timeout": None,
"init_timeout": None,
"idle_timeout": None,
"max_processes": None,
"min_processes": None,
"max_conns_per_process": None,
"load_factor": None,
"memory_request": None,
"memory_limit": None,
"cpu_request": None,
"cpu_limit": None,
"amd_gpu_limit": None,
"nvidia_gpu_limit": None,
"service_account_name": None,
"default_image_name": None,
"created_time": "2020-02-23T20:28:20Z",
"last_deployed_time": "2020-10-13T21:11:24Z",
"bundle_id": "16145",
"app_mode": "shiny",
"content_category": "",
"parameterized": False,
"cluster_name": None,
"image_name": None,
"r_version": "3.5.3",
"py_version": None,
"quarto_version": None,
"r_environment_management": True,
"default_r_environment_management": None,
"py_environment_management": None,
"default_py_environment_management": None,
"run_as": None,
"run_as_current_user": False,
"owner_guid": "a01792e3-2e67-402e-99af-be04a48da074",
"content_url": "https://connect.example/content/39c8d85a-37ae-4b8b-8655-30a06adff2f1/",
"dashboard_url": "https://connect.example/connect/#/apps/39c8d85a-37ae-4b8b-8655-30a06adff2f1",
"app_role": "viewer",
"id": "583",
},
{
"guid": "f2f37341-e21d-3d80-c698-a935ad614066",
"name": "Performance-Data-1671216053560",
"title": "Performance Data",
"description": "",
"access_type": "logged_in",
"connection_timeout": None,
"read_timeout": None,
"init_timeout": None,
"idle_timeout": None,
"max_processes": None,
"min_processes": None,
"max_conns_per_process": None,
"load_factor": None,
"memory_request": None,
"memory_limit": None,
"cpu_request": None,
"cpu_limit": None,
"amd_gpu_limit": None,
"nvidia_gpu_limit": None,
"service_account_name": None,
"default_image_name": None,
"created_time": "2022-12-16T18:40:53Z",
"last_deployed_time": "2024-02-24T09:56:30Z",
"bundle_id": "401171",
"app_mode": "quarto-static",
"content_category": "",
"parameterized": False,
"cluster_name": "Local",
"image_name": None,
"r_version": None,
"py_version": "3.9.17",
"quarto_version": "1.3.340",
"r_environment_management": None,
"default_r_environment_management": None,
"py_environment_management": True,
"default_py_environment_management": None,
"run_as": None,
"run_as_current_user": False,
"owner_guid": "87c12c08-11cd-4de1-8da3-12a7579c4998",
"content_url": "https://connect.example/content/f2f37341-e21d-3d80-c698-a935ad614066/",
"dashboard_url": "https://connect.example/connect/#/apps/f2f37341-e21d-3d80-c698-a935ad614066",
"app_role": "viewer",
"id": "8274",
},
{
"guid": "93a3cd6d-5a1b-236c-9808-6045f2a73fb5",
"name": "My-Streamlit-app",
"title": "My Streamlit app",
"description": "",
"access_type": "logged_in",
"connection_timeout": None,
"read_timeout": None,
"init_timeout": None,
"idle_timeout": None,
"max_processes": None,
"min_processes": None,
"max_conns_per_process": None,
"load_factor": None,
"memory_request": None,
"memory_limit": None,
"cpu_request": None,
"cpu_limit": None,
"amd_gpu_limit": None,
"nvidia_gpu_limit": None,
"service_account_name": None,
"default_image_name": None,
"created_time": "2023-02-28T14:00:17Z",
"last_deployed_time": "2023-03-01T14:12:21Z",
"bundle_id": "217640",
"app_mode": "python-streamlit",
"content_category": "",
"parameterized": False,
"cluster_name": "Local",
"image_name": None,
"r_version": None,
"py_version": "3.9.17",
"quarto_version": None,
"r_environment_management": None,
"default_r_environment_management": None,
"py_environment_management": True,
"default_py_environment_management": None,
"run_as": None,
"run_as_current_user": False,
"owner_guid": "20a79ce3-6e87-4522-9faf-be24228800a4",
"content_url": "https://connect.example/content/93a3cd6d-5a1b-236c-9808-6045f2a73fb5/",
"dashboard_url": "https://connect.example/connect/#/apps/93a3cd6d-5a1b-236c-9808-6045f2a73fb5",
"app_role": "viewer",
"id": "8462",
},
],
)
responses.get(
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066",
json=
{
"guid": "f2f37341-e21d-3d80-c698-a935ad614066",
"name": "Performance-Data-1671216053560",
"title": "Performance Data",
"description": "",
"access_type": "logged_in",
"connection_timeout": None,
"read_timeout": None,
"init_timeout": None,
"idle_timeout": None,
"max_processes": None,
"min_processes": None,
"max_conns_per_process": None,
"load_factor": None,
"memory_request": None,
"memory_limit": None,
"cpu_request": None,
"cpu_limit": None,
"amd_gpu_limit": None,
"nvidia_gpu_limit": None,
"service_account_name": None,
"default_image_name": None,
"created_time": "2022-12-16T18:40:53Z",
"last_deployed_time": "2024-02-24T09:56:30Z",
"bundle_id": "401171",
"app_mode": "quarto-static",
"content_category": "",
"parameterized": False,
"cluster_name": "Local",
"image_name": None,
"r_version": None,
"py_version": "3.9.17",
"quarto_version": "1.3.340",
"r_environment_management": None,
"default_r_environment_management": None,
"py_environment_management": True,
"default_py_environment_management": None,
"run_as": None,
"run_as_current_user": False,
"owner_guid": "87c12c08-11cd-4de1-8da3-12a7579c4998",
"content_url": "https://connect.example/content/f2f37341-e21d-3d80-c698-a935ad614066/",
"dashboard_url": "https://connect.example/connect/#/apps/f2f37341-e21d-3d80-c698-a935ad614066",
"app_role": "viewer",
"id": "8274",
}
)
con = Client("12345", "https://connect.example")
all_content = con.content.find()
assert len(all_content) == 3

# Test find_one
one = con.content.find_one(lambda c: c["title"] == "Performance Data")
assert one["name"] == "Performance-Data-1671216053560"

# Test get
get_one = con.content.get("f2f37341-e21d-3d80-c698-a935ad614066")
assert one == get_one

# Test find_one doesn't find any
assert con.content.find_one(lambda c: c["title"] == "Does not exist") is None

0 comments on commit dc05afa

Please sign in to comment.