Skip to content

Commit

Permalink
add webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Jul 9, 2024
1 parent e3f4235 commit 7472cd9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
10 changes: 8 additions & 2 deletions apsbot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .chat import *
from .revit import *
from .settings import *

from .webhook import *

@click.group()
def apsbot():
Expand Down Expand Up @@ -46,6 +46,12 @@ def apsbot():
apsbot.add_command(data_revit_by_cats_params)
apsbot.add_command(data_revit_by_family)
apsbot.add_command(data_revit_by_family_types)

# webhook
apsbot.add_command(webhooks_get_all)
apsbot.add_command(webhook_get_by_id)
apsbot.add_command(webhook_create)
apsbot.add_command(webhook_delete)
# chat
apsbot.add_command(chat)
apsbot.add_command(chat_data)
apsbot.add_command(chat_data)
10 changes: 10 additions & 0 deletions apsbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ def load_region(cls):
if region is None or region == '':
return 'US'
return region

@classmethod
def load_webhook_id(cls):
"""Load the webhook ID from a JSON file."""
return cls._load_from_config('WEBHOOK_ID')
@classmethod
def save_webhook_id(cls, webhook_id):
"""Save the webhook ID to a JSON file."""
cls._save_to_config('WEBHOOK_ID', webhook_id)

@classmethod
def save_ai_model(cls, ai_model):
"""Save the default AI model to a JSON file."""
Expand Down
69 changes: 69 additions & 0 deletions apsbot/webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import click
from .tokenconfig import TokenConfig
from .config import Config
from aps_toolkit import Webhooks
import pandas as pd
import json
import os
from tabulate import tabulate
@click.command()
@click.option('--webhook_id', prompt='Webhook Id', help='The id of the webhook.')
def webhook_delete(webhook_id):
"""This command deletes a webhook."""
token = TokenConfig.load_config()
webhook = Webhooks(token)
result = webhook.delete_hook_by_id(webhook_id)
if not result:
click.echo(result)
return
click.echo("Webhook deleted successfully!")

@click.command()
@click.option('--save_data', prompt='Save Data(y/n)', default='n', help='Save data to file.')
def webhooks_get_all(save_data):
"""This command lists all webhooks."""
token = TokenConfig.load_config()
webhook = Webhooks(token)
result = webhook.batch_report_all_hooks()
if result.empty:
click.echo("No webhooks found.")
return
if str.lower(save_data) == 'y':
folder = Config.load_folder_path()
file_path = os.path.join(folder, 'webhooks.csv')
result.to_csv(file_path, index=False)
click.echo(f"Webhooks data saved to {file_path}")
# just show hookId, event, folder
df = result[["hookId","folder","projectId","event"]]
print(tabulate(df, headers="keys", tablefmt="psql"))

@click.command()
@click.option('--hook_id', prompt='Hook Id',default=Config.load_webhook_id(), help='The id of the webhook.')
@click.option('--event', prompt='Event', default='dm.version.added', help='The event.')
@click.option('--system', prompt='System', default='data', help='The system.')
def webhook_get_by_id(hook_id,event,system):
"Get webhook by id from user input"
token = TokenConfig.load_config()
webhook = Webhooks(token)
result = webhook.get_hook_by_id(hook_id,event,system)
#save webhook_id to config
Config.save_webhook_id(hook_id)
# dump json
print(json.dumps(result, indent=4))


@click.command()
@click.option('--scope', prompt='Scope', default='data:read data:write', help='The scope.')
@click.option('--callback', prompt='Callback URL', default='http://localhost:8000/api/auth/callback', help='The callback URL.')
@click.option('--event', prompt='Event', default='dm.version.added', help='The event.')
@click.option('--system', prompt='System', default='data', help='The system.')
@click.option('--hookAttribute', prompt='Hook Attribute', default=None, help='The hook attribute.')
def webhook_create(scope, callback, event, system, hookAttribute):
"""This command lists all webhooks."""
token = TokenConfig.load_config()
webhook = Webhooks(token)
result = webhook.create_system_event_hook(scope, callback, event, system, hookAttribute)
if not result:
click.echo("No webhooks found.")
return
print(json.dumps(result, indent=4))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="apsbot",
version="0.2.9",
version="0.3.0",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down

0 comments on commit 7472cd9

Please sign in to comment.