Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update add router to accept url
  • Loading branch information
Sudhamsha committed Apr 17, 2025
commit 9e08acecf80f4b03b2cb9c97c8b7fe9f8b7a8f43
59 changes: 36 additions & 23 deletions cognee/api/v1/add/routers/get_add_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,50 @@ def get_add_router() -> APIRouter:

@router.post("/", response_model=None)
async def add(
data: List[UploadFile],
file: UploadFile = File(None),
url: str = Form(None),
datasetId: str = Form(...),
user: User = Depends(get_authenticated_user),
):
"""This endpoint is responsible for adding data to the graph."""
"""
This endpoint is responsible for adding data to the graph.
Accepts either:
- file: a single file upload
- url: a URL to fetch data from (supports GitHub clone or direct file download)
"""
from cognee.api.v1.add import add as cognee_add

try:
if isinstance(data, str) and data.startswith("http"):
if "github" in data:
# Perform git clone if the URL is from GitHub
repo_name = data.split("/")[-1].replace(".git", "")
subprocess.run(["git", "clone", data, f".data/{repo_name}"], check=True)
await cognee_add(
"data://.data/",
f"{repo_name}",
)
else:
# Fetch and store the data from other types of URL using curl
response = requests.get(data)
response.raise_for_status()

file_data = await response.content()

return await cognee_add(file_data)
else:
await cognee_add(
data,
if file:
# Handle file upload
return await cognee_add(
file,
datasetId,
user=user,
)
elif url:
if url.startswith("http"):
if "github" in url:
# Perform git clone if the URL is from GitHub
repo_name = url.split("/")[-1].replace(".git", "")
subprocess.run(["git", "clone", url, f".data/{repo_name}"], check=True)
return await cognee_add(
"data://.data/",
f"{repo_name}",
user=user,
)
else:
# Fetch and store the data from other types of URL
response = requests.get(url)
response.raise_for_status()
return await cognee_add(
response.content,
datasetId,
user=user,
)
else:
return JSONResponse(status_code=400, content={"error": "Invalid URL format"})
else:
return JSONResponse(status_code=400, content={"error": "No file or URL provided"})
except Exception as error:
return JSONResponse(status_code=409, content={"error": str(error)})

Expand Down