Skip to content

Commit

Permalink
Followup to using *path for endpoint calls
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Nov 8, 2024
1 parent 0de7d4f commit 208048d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/posit/connect/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class ApiDictEndpoint(ApiCallMixin, ReadOnlyDict):
_path: str
"""The HTTP path component for the resource endpoint."""

def _get_api(self, *, extra_endpoint: str = "") -> JsonifiableDict | None:
super()._get_api(extra_endpoint=extra_endpoint)
def _get_api(self, *path) -> JsonifiableDict | None:
super()._get_api(*path)

def __init__(
self,
Expand Down Expand Up @@ -246,7 +246,7 @@ def find(self, uid: str) -> T | None:
:
Single instance of T if found, else None
"""
result: Jsonifiable = self._get_api(extra_endpoint=uid)
result: Jsonifiable = self._get_api(uid)
result_obj = cast(JsonifiableDict, result)

return self._to_instance(result_obj)
Expand Down
45 changes: 26 additions & 19 deletions src/posit/connect/_api_call.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import posixpath
from typing import TYPE_CHECKING, Protocol

if TYPE_CHECKING:
Expand All @@ -11,55 +12,61 @@ class ApiCallProtocol(Protocol):
_ctx: Context
_path: str

def _endpoint(self, extra_endpoint: str = "") -> str: ...
def _get_api(self, *, extra_endpoint: str = "") -> Jsonifiable: ...
def _delete_api(self, *, extra_endpoint: str = "") -> Jsonifiable | None: ...
def _patch_api(self, json: Jsonifiable | None, *, extra_endpoint: str = "") -> Jsonifiable: ...
def _put_api(self, json: Jsonifiable | None, *, extra_endpoint: str = "") -> Jsonifiable: ...
def _endpoint(self, *path) -> str: ...
def _get_api(self, *path) -> Jsonifiable: ...
def _delete_api(self, *path) -> Jsonifiable | None: ...
def _patch_api(self, *path, json: Jsonifiable | None) -> Jsonifiable: ...
def _put_api(self, *path, json: Jsonifiable | None) -> Jsonifiable: ...


def endpoint(ctx: Context, *path) -> str:
return ctx.url + posixpath.join(*path)


# Helper methods for API interactions
def get_api(ctx: Context, path: str, *, extra_endpoint: str = "") -> Jsonifiable:
response = ctx.session.get(endpoint(ctx, path, extra_endpoint=extra_endpoint))
def get_api(ctx: Context, *path) -> Jsonifiable:
response = ctx.session.get(endpoint(ctx, *path))
return response.json()


def put_api(
ctx: Context, path: str, json: Jsonifiable | None, *, extra_endpoint: str = ""
ctx: Context,
*path,
json: Jsonifiable | None,
) -> Jsonifiable:
response = ctx.session.put(endpoint(ctx, path, extra_endpoint=extra_endpoint), json=json)
response = ctx.session.put(endpoint(ctx, *path), json=json)
return response.json()


# Mixin class for API interactions


class ApiCallMixin:
def _endpoint(self: ApiCallProtocol, extra_endpoint: str = "") -> str:
return endpoint(self._ctx, self._path, extra_endpoint=extra_endpoint)
def _endpoint(self: ApiCallProtocol, *path) -> str:
return endpoint(self._ctx, self._path, *path)

def _get_api(self: ApiCallProtocol, *, extra_endpoint: str = "") -> Jsonifiable:
response = self._ctx.session.get(self._endpoint(extra_endpoint))
def _get_api(self: ApiCallProtocol, *path) -> Jsonifiable:
response = self._ctx.session.get(self._endpoint(*path))
return response.json()

def _delete_api(self: ApiCallProtocol, *, extra_endpoint: str = "") -> Jsonifiable | None:
response = self._ctx.session.delete(self._endpoint(extra_endpoint))
def _delete_api(self: ApiCallProtocol, *path) -> Jsonifiable | None:
response = self._ctx.session.delete(self._endpoint(*path))
if len(response.content) == 0:
return None
return response.json()

def _patch_api(
self: ApiCallProtocol, json: Jsonifiable | None, *, extra_endpoint: str = ""
self: ApiCallProtocol,
*path,
json: Jsonifiable | None,
) -> Jsonifiable:
response = self._ctx.session.patch(self._endpoint(extra_endpoint), json=json)
response = self._ctx.session.patch(self._endpoint(*path), json=json)
return response.json()

def _put_api(
self: ApiCallProtocol, json: Jsonifiable | None, *, extra_endpoint: str = ""
self: ApiCallProtocol,
*path,
json: Jsonifiable | None,
) -> Jsonifiable:
response = self._ctx.session.put(self._endpoint(extra_endpoint), json=json)
response = self._ctx.session.put(self._endpoint(*path), json=json)
return response.json()
2 changes: 1 addition & 1 deletion src/posit/connect/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def update(
--------
* https://docs.posit.co/connect/api/#patch-/v1/content/-guid-/repository
"""
result = self._patch_api(cast(JsonifiableDict, dict(attrs)))
result = self._patch_api(json=cast(JsonifiableDict, dict(attrs)))
return ContentItemRepository(
self._ctx,
content_guid=self["content_guid"],
Expand Down

0 comments on commit 208048d

Please sign in to comment.