Skip to content

Commit

Permalink
refactor: Remove dict parent class from Context (#335)
Browse files Browse the repository at this point in the history
Co-authored-by: Taylor Steinberg <taylor@steinberg.xyz>
  • Loading branch information
schloerke and tdstein authored Nov 14, 2024
1 parent 540c693 commit 4051bfb
Showing 1 changed file with 15 additions and 12 deletions.
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

0 comments on commit 4051bfb

Please sign in to comment.