Skip to content

Commit

Permalink
✨ Feat: refactor client methods
Browse files Browse the repository at this point in the history
added get_auth_token for explicit usage if needed otherwise login is using it to set up
  • Loading branch information
guptadev21 committed Nov 22, 2024
1 parent 84a2eb1 commit 6bf91bf
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 128 deletions.
2 changes: 2 additions & 0 deletions rapyuta_io_sdk_v2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ruff: noqa
from rapyuta_io_sdk_v2.config import Configuration
from rapyuta_io_sdk_v2.client import Client
from rapyuta_io_sdk_v2.utils import walk_pages
from rapyuta_io_sdk_v2.async_client import AsyncClient

__version__ = "0.0.1"
110 changes: 68 additions & 42 deletions rapyuta_io_sdk_v2/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import platform
from rapyuta_io_sdk_v2.config import Configuration
from rapyuta_io_sdk_v2.utils import (
handle_auth_token,
handle_and_munchify_response,
walk_pages,
handle_server_errors,
)


Expand Down Expand Up @@ -48,25 +47,8 @@ def __init__(self, config=None, **kwargs):
)
},
)
self.rip_host = self.config.hosts.get("rip_host")
self.v2api_host = self.config.hosts.get("v2api_host")

@handle_auth_token
def login(
self, email: str = None, password: str = None, environment: str = "ga"
) -> str:
"""Get the authentication token for the user.
Args:
email (str, optional): async defaults to None.
password (str, optional): async defaults to None.
environment (str, optional): async defaults to "ga".
Returns:
str: auth token
"""
sync_client = httpx.Client(
self.sync_client = httpx.Client(
timeout=timeout,
limits=httpx.Limits(
max_keepalive_connections=5,
max_connections=5,
Expand All @@ -83,35 +65,79 @@ def login(
)
},
)
if email is None and password is None and self.config is None:
raise ValueError("email and password are required")
self.rip_host = self.config.hosts.get("rip_host")
self.v2api_host = self.config.hosts.get("v2api_host")

def get_auth_token(self, email: str, password: str) -> str:
"""Get the authentication token for the user.
if self.config is None:
self.config = Configuration(
email=email, password=password, environment=environment
)
Args:
email (str)
password (str)
return sync_client.post(
Returns:
str: authentication token
"""
response = self.sync_client.post(
url=f"{self.rip_host}/user/login",
headers={"Content-Type": "application/json"},
json={
"email": email or self.config.email,
"password": password or self.config.password,
"email": email,
"password": password,
},
)
handle_server_errors(response)
return response.json()["data"].get("token")

def login(
self,
email: str,
password: str,
environment: str = "ga",
) -> None:
"""Get the authentication token for the user.
Args:
email (str)
password (str)
environment (str)
def logout(self, token=None):
pass
Returns:
str: authentication token
"""

token = self.get_auth_token(email, password)
self.config.auth_token = token

@handle_and_munchify_response
def logout(self, token: str = None) -> None:
"""Expire the authentication token.
Args:
token (str): The token to expire.
"""

if token is None:
token = self.config.auth_token

return self.sync_client.post(
url=f"{self.rip_host}/user/logout",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
},
)

async def refresh_token(self, token: str = None) -> str:
if token is None:
token = self.config.auth_token

return await self.c.post(
response = await self.c.post(
url=f"{self.rip_host}/refreshtoken",
headers={"Content-Type": "application/json"},
json={"token": token},
)
return response.json()["data"].get("token")

def set_organization(self, organization_guid: str) -> None:
"""Set the organization GUID.
Expand All @@ -130,7 +156,7 @@ def set_project(self, project_guid: str) -> None:
self.config.set_project(project_guid)

# ----------------- Projects -----------------
@walk_pages
@handle_and_munchify_response
async def list_projects(self, cont: int = 0, limit: int = 50, **kwargs) -> Munch:
"""List all projects.
Expand Down Expand Up @@ -243,7 +269,7 @@ async def update_project_owner(
)

# -------------------Package-------------------
@walk_pages
@handle_and_munchify_response
async def list_packages(self, cont: int = 0, limit: int = 10, **kwargs) -> Munch:
"""List all packages in a project.
Expand Down Expand Up @@ -304,7 +330,7 @@ async def delete_package(self, name: str, **kwargs) -> Munch:
)

# -------------------Deployment-------------------
@walk_pages
@handle_and_munchify_response
async def list_deployments(self, cont: int = 0, limit: int = 50, **kwargs) -> Munch:
"""List all deployments in a project.
Expand Down Expand Up @@ -380,7 +406,7 @@ async def delete_deployment(self, name: str, **kwargs) -> Munch:
)

# -------------------Disks-------------------
@walk_pages
@handle_and_munchify_response
async def list_disks(self, cont: int = 0, limit: int = 50, **kwargs) -> Munch:
"""List all disks in a project.
Expand Down Expand Up @@ -441,7 +467,7 @@ async def delete_disk(self, name: str, **kwargs) -> Munch:
)

# -------------------Static Routes-------------------
@walk_pages
@handle_and_munchify_response
async def list_staticroutes(self, cont: int = 0, limit: int = 0, **kwargs) -> Munch:
"""List all static routes in a project.
Expand Down Expand Up @@ -520,7 +546,7 @@ async def delete_staticroute(self, name: str, **kwargs) -> Munch:
)

# -------------------Networks-------------------
@walk_pages
@handle_and_munchify_response
async def list_networks(self, cont: int = 0, limit: int = 0, **kwargs) -> Munch:
"""List all networks in a project.
Expand Down Expand Up @@ -581,7 +607,7 @@ async def delete_network(self, name: str, **kwargs) -> Munch:
)

# -------------------Secrets-------------------
@walk_pages
@handle_and_munchify_response
async def list_secrets(self, cont: int = 0, limit: int = 50, **kwargs) -> Munch:
"""List all secrets in a project.
Expand Down Expand Up @@ -660,7 +686,7 @@ async def delete_secret(self, name: str, **kwargs) -> Munch:
)

# -------------------Config Trees-------------------
@walk_pages
@handle_and_munchify_response
async def list_configtrees(self, cont: int = 0, limit: int = 50, **kwargs) -> Munch:
"""List all config trees in a project.
Expand Down Expand Up @@ -762,7 +788,7 @@ async def delete_configtree(self, name: str, **kwargs) -> Munch:
headers=self.config.get_headers(**kwargs),
)

@walk_pages
@handle_and_munchify_response
async def list_revisions(
self,
name: str,
Expand Down
Loading

0 comments on commit 6bf91bf

Please sign in to comment.