-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f0680e
commit 057ab1a
Showing
6 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
|
||
import click | ||
|
||
from rippling_cli.config.config import get_app_config, save_app_config | ||
from rippling_cli.constants import RIPPLING_API | ||
from rippling_cli.core.api_client import APIClient | ||
from rippling_cli.utils.login_utils import ensure_logged_in | ||
|
||
|
||
@click.group() | ||
@click.pass_context | ||
def app(ctx: click.Context) -> None: | ||
"""Manage flux apps""" | ||
ensure_logged_in(ctx) | ||
|
||
|
||
@app.command() | ||
def list() -> None: | ||
"""This command displays a list of all apps owned by the developer.""" | ||
ctx: click.Context = click.get_current_context() | ||
api_client = APIClient(base_url=RIPPLING_API, headers={"Authorization": f"Bearer {ctx.obj.oauth_token}"}) | ||
endpoint = "/apps/api/integrations" | ||
|
||
for page in api_client.find_paginated(endpoint): | ||
click.echo(f"Page: {len(page)} apps") | ||
|
||
for app in page: | ||
click.echo(f"- {app.get('displayName')} ({app.get('id')})") | ||
|
||
if not click.confirm("Continue"): | ||
break | ||
|
||
click.echo("End of apps list.") | ||
|
||
|
||
@app.command() | ||
@click.option("--app_id", required=True, type=str, help="The app id to set for the current directory.") | ||
def set(app_id: str) -> None: | ||
"""This command sets the current app within the app_config.json file located in the .rippling directory.""" | ||
ctx: click.Context = click.get_current_context() | ||
api_client = APIClient(base_url=RIPPLING_API, headers={"Authorization": f"Bearer {ctx.obj.oauth_token}"}) | ||
|
||
endpoint = "/apps/api/apps/?large_get_query=true" | ||
response = api_client.post(endpoint, data={"query": f"id={app_id}&limit=1"}) | ||
app_list = response.json() if response.status_code == 200 else [] | ||
|
||
if response.status_code != 200 or len(app_list) == 0: | ||
click.echo(f"Invalid app id: {app_id}") | ||
return | ||
|
||
app_name = app_list[0].get("displayName") | ||
|
||
save_app_config(app_id, app_name) | ||
click.echo(f"Current app set to {app_name} ({app_id})") | ||
|
||
|
||
@app.command() | ||
def current() -> None: | ||
"""This command indicates the current app selected by the developer within the directory.""" | ||
app_config_json = get_app_config() | ||
app_config = app_config_json.get(os.getcwd()) | ||
if not app_config: | ||
click.echo("No app selected.") | ||
return | ||
|
||
click.echo(f"{app_config.get('displayName')} ({app_config.get('id')})") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import requests | ||
|
||
|
||
class APIClient: | ||
def __init__(self, base_url, headers=None): | ||
self.base_url = base_url | ||
self.headers = headers or {} | ||
|
||
def make_request(self, method, endpoint, params=None, data=None): | ||
url = f"{self.base_url.rstrip('/')}/{endpoint.lstrip('/')}" | ||
response = requests.request(method, url, params=params, json=data, headers=self.headers) | ||
return response | ||
|
||
def get(self, endpoint, params=None): | ||
return self.make_request("GET", endpoint, params=params) | ||
|
||
def post(self, endpoint, data): | ||
return self.make_request("POST", endpoint, data=data) | ||
|
||
def put(self, endpoint, data): | ||
return self.make_request("PUT", endpoint, data=data) | ||
|
||
def delete(self, endpoint, params=None): | ||
return self.make_request("DELETE", endpoint, params=params) | ||
|
||
def find_paginated(self, endpoint, page=1, page_size=10, read_preference="SECONDARY_PREFERRED"): | ||
""" | ||
Fetch paginated data from the API. | ||
Args: | ||
endpoint (str): The API endpoint. | ||
headers (dict): The headers for the API request. | ||
page (int): The page number to fetch. | ||
per_page (int): The number of items to fetch per page. | ||
Yields: | ||
dict: The data from the API response. | ||
:param endpoint: | ||
:param page: | ||
:param page_size: | ||
:param read_preference: | ||
""" | ||
has_more = True | ||
cursor = None | ||
while has_more: | ||
payload = { | ||
"paginationParams": { | ||
"page": page, | ||
"cursor": cursor, | ||
"sortingMetadata": { | ||
"order": "DESC", | ||
"column": { | ||
"sortKey": "createdAt" | ||
} | ||
} | ||
}, | ||
"pageSize": page_size, | ||
"readPreference": read_preference | ||
} | ||
response = self.make_request("POST", f"{endpoint}/find_paginated", data=payload) | ||
|
||
if response.status_code == 200: | ||
data = response.json() | ||
cursor = data.get("cursor") | ||
has_more = False if not cursor else True | ||
items = data["data"] | ||
page += 1 | ||
print(page) | ||
yield items | ||
else: | ||
response.raise_for_status() | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import click | ||
|
||
from rippling_cli.cli.commands.login import login | ||
from rippling_cli.core.oauth_token import OAuthToken | ||
|
||
|
||
def ensure_logged_in(ctx: click.Context): | ||
if OAuthToken.is_token_expired(): | ||
click.echo("You are not logged in. Please log in first.") | ||
ctx.invoke(login) |