Skip to content

Commit

Permalink
feat: add some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
guptadev21 committed Oct 25, 2024
1 parent 3896d9e commit 0b9144f
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 60 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/lint-check.yml

This file was deleted.

14 changes: 14 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: ✅ Quality Checks
on: [ push ]

jobs:
code-quality-checks:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Run checks
uses: astral-sh/ruff-action@v1
with:
args: "check"
23 changes: 23 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: 📦️ Upload to PyPi
on:
release:
types:
- published

jobs:
upload:
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v3

- name: Publish to pypi
run: |
uv build
uv publish --trusted-publishing always
12 changes: 8 additions & 4 deletions .github/workflows/python-compatibility.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: 🐍 Python Compatibility Check
on: [ push ]
on: [push]

jobs:
python-compatibility:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.8.10', '3.9', '3.10', '3.11', '3.12', '3.13' ]
python-version: ['3.8.10', '3.9', '3.10', '3.11', '3.12', '3.13']
steps:
- name: Checkout Code
uses: actions/checkout@v4
Expand All @@ -17,5 +17,9 @@ jobs:
- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}

- name: PyTest
run: uv pytest tests/test_get_token.py
- name: Install the project
run: uv sync --all-extras --dev

- name: Run tests
# For example, using `pytest`
run: uv run pytest tests/test_get_token.py
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: 🎉 Release
on:
push:
branches:
- main

jobs:
release:
runs-on: ubuntu-20.04
steps:
- name: Checkout Code
uses: actions/[email protected]
with:
token: ${{ secrets.GH_TOKEN }}

- name: Run semantic-release
run: |
npm install --save-dev [email protected]
npm install @semantic-release/git -D
npm install @semantic-release/changelog -D
npm install @semantic-release/exec -D
npx semantic-release
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ wheels/
.venv*/
.ruff_cache
.idea
.github/workflows/release.yml
.vscode
main_test.py
test_config.json
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description = "Python SDK for rapyuta.io v2 APIs"
dependencies = [
"httpx>=0.27.2",
"mock>=5.1.0",
"munch>=4.0.0",
"pytest-mock>=3.14.0",
"pytest>=8.3.3",
"tenacity>=9.0.0",
Expand Down
43 changes: 42 additions & 1 deletion rapyuta_io_sdk_v2/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from rapyuta_io_sdk_v2.client import Client
from rapyuta_io_sdk_v2.config import Configuration
from rapyuta_io_sdk_v2.utils import handle_server_errors
from rapyuta_io_sdk_v2.utils import handle_server_errors, projects_list_munch


class AsyncClient(Client):
Expand Down Expand Up @@ -84,3 +84,44 @@ async def refresh_token(self, token: str) -> str:

data = response.json()["data"]
return data["Token"]

async def list_projects(self, organization_guid: str):
if organization_guid is None:
raise ValueError("organization_guid is required")
v2api_host = self.config.hosts.get("v2api_host")
self.config.organization_guid = organization_guid
headers = self._get_headers(with_project=False)

async with httpx.AsyncClient() as asyncClient:
response = await asyncClient.get(
url="{}/v2/projects/".format(v2api_host), headers=headers, timeout=10
)
handle_server_errors(response)
return projects_list_munch(response)

async def get_project(self, organization_guid: str, project_guid: str):
"""Get a project by its GUID
Args:
organization_guid (str): Organization GUID
project_guid (str): Project GUID
Raises:
ValueError: If organization_guid or project_guid is None
Returns:
_type_: Project details in json
"""
if organization_guid is None or project_guid is None:
raise ValueError("organization_guid and project_guid are required")
v2api_host = self.config.hosts.get("v2api_host")
self.config.organization_guid = organization_guid
headers = self._get_headers(with_project=False)
async with httpx.AsyncClient() as asyncClient:
response = await asyncClient.get(
url="{}/v2/projects/{}/".format(v2api_host, project_guid),
headers=headers,
timeout=10,
)
handle_server_errors(response)
return response.json()
42 changes: 40 additions & 2 deletions rapyuta_io_sdk_v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from rapyuta_io_sdk_v2.config import Configuration
from rapyuta_io_sdk_v2.constants import GET_USER_API_PATH
from rapyuta_io_sdk_v2.utils import handle_server_errors
from rapyuta_io_sdk_v2.utils import handle_server_errors, projects_list_munch


class Client(object):
Expand Down Expand Up @@ -115,14 +115,52 @@ def set_project(self, project_guid: str):
def set_organization(self, organization_guid: str):
self.config.organization_guid = organization_guid

# Projects
def list_projects(self, organization_guid: str):
"""List all projects in the organization
Args:
organization_guid (str): The organization GUID
Raises:
ValueError: If organization_guid is None
Returns:
_type_: List of projects (Munch object)
"""
if organization_guid is None:
raise ValueError("organization_guid is required")
v2api_host = self.config.hosts.get("v2api_host")
self.config.organization_guid = organization_guid
self.set_organization(organization_guid)
headers = self._get_headers(with_project=False)
response = httpx.get(
url="{}/v2/projects/".format(v2api_host), headers=headers, timeout=10
)
handle_server_errors(response)
return projects_list_munch(response)

def get_project(self, organization_guid: str, project_guid: str):
"""Get a project by its GUID
Args:
organization_guid (str): Organization GUID
project_guid (str): Project GUID
Raises:
ValueError: If organization_guid or project_guid is None
Returns:
_type_: Project details in json
"""
if organization_guid is None or project_guid is None:
raise ValueError("organization_guid and project_guid are required")
v2api_host = self.config.hosts.get("v2api_host")
self.set_organization(organization_guid)
headers = self._get_headers(with_project=False)
response = httpx.get(
url="{}/v2/projects/{}/".format(v2api_host, project_guid),
headers=headers,
timeout=10,
)
handle_server_errors(response)
return response.json()
58 changes: 29 additions & 29 deletions rapyuta_io_sdk_v2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,60 @@
# limitations under the License.
import json
from dataclasses import dataclass
import os

from rapyuta_io_sdk_v2.constants import (
NAMED_ENVIRONMENTS,
PROD_ENVIRONMENT_SUBDOMAIN,
STAGING_ENVIRONMENT_SUBDOMAIN,
)
from rapyuta_io_sdk_v2.utils import get_default_app_dir
from rapyuta_io_sdk_v2.exceptions import ValidationError


@dataclass
class Configuration(object):
email: str
_password: str
auth_token: str
project_guid: str
organization_guid: str
email: str = None
_password: str = None
auth_token: str = None
project_guid: str = None
organization_guid: str = None
environment: str = "ga" # Default environment is prod

def __init__(
self,
project_guid: str = None,
organization_guid: str = None,
password: str = None,
auth_token: str = None,
environment: str = None,
email: str = None,
):
self.email = email
self._password = password
self.auth_token = auth_token
self.project_guid = project_guid
self.organization_guid = organization_guid
self.environment = environment
def __post_init__(self):
self.hosts = {}
self.set_environment(environment)
self.set_environment(self.environment)

@staticmethod
def from_file(file_path: str) -> "Configuration":
"""Create a configuration object from a file.
Args:
file_path (str): Path to the file.
Returns:
Configuration: Configuration object.
"""
if file_path is None:
app_name = "rio_cli"
default_dir = get_default_app_dir(app_name)
file_path = os.path.join(default_dir, "config.json")

with open(file_path, "r") as file:
data = json.load(file)
return Configuration(
email=data.get("email"),
password=data.get("password"),
_password=data.get("password"),
project_guid=data.get("project_guid"),
organization_guid=data.get("organization_guid"),
environment=data.get("environment"),
auth_token=data.get("auth_token"),
)

def set_project(self, project) -> None:
self.project_guid = project
def set_project(self, project_guid: str) -> None:
self.project_guid = project_guid

def set_organization(self, organization_guid) -> None:
def set_organization(self, organization_guid: str) -> None:
self.organization_guid = organization_guid

def set_environment(self, name: str = None) -> None:
Expand All @@ -81,12 +83,10 @@ def set_environment(self, name: str = None) -> None:
if self.environment is not None:
name = self.environment
if name is not None:
is_valid_env = name in NAMED_ENVIRONMENTS or name.startswith("pr")
if not is_valid_env:
if not (name in NAMED_ENVIRONMENTS or name.startswith("pr")):
raise ValidationError("Invalid environment")
subdomain = STAGING_ENVIRONMENT_SUBDOMAIN
else:
name = "ga"
name = name or "ga"

rip = "https://{}rip.{}".format(name, subdomain)
v2api = "https://{}api.{}".format(name, subdomain)
Expand Down
2 changes: 1 addition & 1 deletion rapyuta_io_sdk_v2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

STAGING_ENVIRONMENT_SUBDOMAIN = "apps.okd4v2.okd4beta.rapyuta.io"
PROD_ENVIRONMENT_SUBDOMAIN = "apps.okd4v2.prod.rapyuta.io"
NAMED_ENVIRONMENTS = ["v11", "v12", "v13", "v14", "v15", "qa", "dev"]
NAMED_ENVIRONMENTS = ["ga", "qa", "dev"]
Loading

0 comments on commit 0b9144f

Please sign in to comment.