Skip to content

Commit

Permalink
Change private async methods to async_ prefix (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Feb 29, 2024
1 parent d7da2fe commit 1f89173
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 75 deletions.
32 changes: 16 additions & 16 deletions kr8s/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ async def version(self) -> dict:
The Kubernetes version information.
"""
return await self._version()
return await self.async_version()

async def _version(self) -> dict:
async def async_version(self) -> dict:
async with self.call_api(method="GET", version="", base="/version") as response:
return response.json()

Expand All @@ -229,9 +229,9 @@ async def whoami(self):
Returns:
str: The subject that's currently authenticated.
"""
return await self._whoami()
return await self.async_whoami()

async def _whoami(self):
async def async_whoami(self):
if self.auth.token:
payload = {
"apiVersion": "authentication.k8s.io/v1",
Expand All @@ -253,7 +253,7 @@ async def _whoami(self):
return name.value

@contextlib.asynccontextmanager
async def _get_kind(
async def async_get_kind(
self,
kind: Union[str, type],
namespace: str = None,
Expand Down Expand Up @@ -287,7 +287,7 @@ async def _get_kind(
obj_cls = kind
else:
try:
resources = await self._api_resources()
resources = await self.async_api_resources()
for resource in resources:
if "shortNames" in resource and kind in resource["shortNames"]:
kind = resource["name"]
Expand Down Expand Up @@ -341,7 +341,7 @@ async def get(
List[object]
The resources.
"""
return await self._get(
return await self.async_get(
kind,
*names,
namespace=namespace,
Expand All @@ -351,7 +351,7 @@ async def get(
**kwargs,
)

async def _get(
async def async_get(
self,
kind: Union[str, type],
*names: List[str],
Expand All @@ -367,7 +367,7 @@ async def _get(
headers["Accept"] = (
f"application/json;as={as_object.kind};v={version};g={group}"
)
async with self._get_kind(
async with self.async_get_kind(
kind,
namespace=namespace,
label_selector=label_selector,
Expand Down Expand Up @@ -400,7 +400,7 @@ async def watch(
since: str = None,
):
"""Watch a Kubernetes resource."""
async for t, object in self._watch(
async for t, object in self.async_watch(
kind,
namespace=namespace,
label_selector=label_selector,
Expand All @@ -409,7 +409,7 @@ async def watch(
):
yield t, object

async def _watch(
async def async_watch(
self,
kind: str,
namespace: str = None,
Expand All @@ -418,7 +418,7 @@ async def _watch(
since: str = None,
) -> Tuple[str, object]:
"""Watch a Kubernetes resource."""
async with self._get_kind(
async with self.async_get_kind(
kind,
namespace=namespace,
label_selector=label_selector,
Expand All @@ -433,12 +433,12 @@ async def _watch(

async def api_resources(self) -> dict:
"""Get the Kubernetes API resources."""
return await self._api_resources()
return await self.async_api_resources()

# Cache for 6 hours because kubectl does
# https://github.com/kubernetes/cli-runtime/blob/980bedf450ab21617b33d68331786942227fe93a/pkg/genericclioptions/config_flags.go#L297
@cached(TTLCache(1, 60 * 60 * 6))
async def _api_resources(self) -> dict:
async def async_api_resources(self) -> dict:
"""Get the Kubernetes API resources."""
resources = []
async with self.call_api(method="GET", version="", base="/api") as response:
Expand Down Expand Up @@ -475,10 +475,10 @@ async def _api_resources(self) -> dict:

async def api_versions(self) -> List[str]:
"""Get the Kubernetes API versions."""
async for version in self._api_versions():
async for version in self.async_api_versions():
yield version

async def _api_versions(self) -> List[str]:
async def async_api_versions(self) -> List[str]:
async with self.call_api(method="GET", version="", base="/api") as response:
core_api_list = response.json()
for version in core_api_list["versions"]:
Expand Down
2 changes: 1 addition & 1 deletion kr8s/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def sync(source: object) -> object:
for name in dir(source):
method = getattr(source, name)

if not name.startswith("_"):
if not name.startswith("_") and not name.startswith("async_"):
if inspect.iscoroutinefunction(method) or inspect.isasyncgenfunction(
method
):
Expand Down
Loading

0 comments on commit 1f89173

Please sign in to comment.