Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove dict parent class from Context #335

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/posit/connect/context.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from __future__ import annotations

import functools
from typing import Optional, Protocol
from typing import TYPE_CHECKING, Protocol

import requests
from packaging.version import Version

from .urls import Url
if TYPE_CHECKING:
import requests

from .urls import Url


def requires(version: str):
Expand All @@ -23,25 +27,24 @@ def wrapper(instance: ContextManager, *args, **kwargs):
return decorator


class Context(dict):
class Context:
def __init__(self, session: requests.Session, url: Url):
self.session = session
self.url = url

@property
def version(self) -> Optional[str]:
try:
value = self["version"]
except KeyError:
def version(self) -> str | None:
if not hasattr(self, "_version"):
endpoint = self.url + "server_settings"
response = self.session.get(endpoint)
result = response.json()
value = self["version"] = result.get("version")
return value
self._version: str | None = result.get("version")

return self._version

@version.setter
def version(self, value):
self["version"] = value
def version(self, value: str | None):
self._version = value


class ContextManager(Protocol):
Expand Down