Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GET /plugins/ returns both installed and available plugins #387

Merged
merged 9 commits into from
Aug 8, 2023
55 changes: 53 additions & 2 deletions core/cat/routes/plugins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import mimetypes
from typing import Dict
import os
from typing import Dict, Annotated
from tempfile import NamedTemporaryFile

from fastapi import Body, Request, APIRouter, HTTPException, UploadFile, BackgroundTasks
from cat.log import log
import httpx
import requests
from urllib.parse import urlparse


router = APIRouter()

Expand All @@ -21,7 +27,7 @@ async def list_available_plugins(request: Request) -> Dict:
plugins.append(p.manifest)

# retrieve plugins from official repo
registry = []
registry = await get_registry_list()

return {
"status": "success",
Expand All @@ -31,6 +37,7 @@ async def list_available_plugins(request: Request) -> Dict:
}



@router.post("/upload/")
async def install_plugin(
request: Request,
Expand Down Expand Up @@ -182,3 +189,47 @@ async def delete_plugin(plugin_id: str, request: Request) -> Dict:
"status": "success",
"deleted": plugin_id
}

async def get_registry_list():
response = httpx.get("http://192.168.1.120:8000/plugins?page=1&page_size=7000")
valentimarco marked this conversation as resolved.
Show resolved Hide resolved
if response.status_code == 200:
return response.json()["plugins"]
else:
return []

@router.post("/upload/wa")
valentimarco marked this conversation as resolved.
Show resolved Hide resolved
async def download_plugin_from_registry(request: Request,background_tasks: BackgroundTasks,url_repo: Dict = Body(example={"url": "https://example.com/file.zip"})):
valentimarco marked this conversation as resolved.
Show resolved Hide resolved
"""Install a new plugin from external repository"""

#Get name of file
url = urlparse(url_repo["url"])
url_path = url.path.split("/")
url_path.reverse()
if "github" in url.netloc:
print(url_path)
plugin_name = str(url_path[2]) + ".zip"
else:
plugin_name = str(url_path[0])
valentimarco marked this conversation as resolved.
Show resolved Hide resolved

print(plugin_name)
with requests.get(url_repo["url"], stream=True) as response:
response.raise_for_status()
valentimarco marked this conversation as resolved.
Show resolved Hide resolved
with NamedTemporaryFile(delete=False,mode="w+b",suffix=plugin_name) as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
log(f"Uploading plugin {plugin_name}", "INFO")

#access cat instance
ccat = request.app.state.ccat

background_tasks.add_task(
ccat.mad_hatter.install_plugin, file.name
)

return {
"status": "success",
"filename": file.name,
"content_type": mimetypes.guess_type(plugin_name)[0],
"info": "Plugin is being installed asynchronously"
}