Skip to content

Commit

Permalink
Merge branch 'main' into schloerke/324-merge-api-and-resource
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored Nov 14, 2024
2 parents a1b4440 + 4051bfb commit 04a5c22
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 04a5c22

Please sign in to comment.