mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(nodes): raise HTTPExceptions instead of returning Reponses
This commit is contained in:
parent
50ac3eb28d
commit
73cdd36594
@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
from typing import Annotated, List, Optional, Union
|
from typing import Annotated, List, Optional, Union
|
||||||
|
|
||||||
from fastapi import Body, Path, Query
|
from fastapi import Body, HTTPException, Path, Query
|
||||||
from fastapi.responses import Response
|
|
||||||
from fastapi.routing import APIRouter
|
from fastapi.routing import APIRouter
|
||||||
from pydantic.fields import Field
|
from pydantic.fields import Field
|
||||||
|
|
||||||
@ -76,7 +75,7 @@ async def get_session(
|
|||||||
"""Gets a session"""
|
"""Gets a session"""
|
||||||
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
return Response(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
else:
|
else:
|
||||||
return session
|
return session
|
||||||
|
|
||||||
@ -99,7 +98,7 @@ async def add_node(
|
|||||||
"""Adds a node to the graph"""
|
"""Adds a node to the graph"""
|
||||||
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
return Response(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.add_node(node)
|
session.add_node(node)
|
||||||
@ -108,9 +107,9 @@ async def add_node(
|
|||||||
) # TODO: can this be done automatically, or add node through an API?
|
) # TODO: can this be done automatically, or add node through an API?
|
||||||
return session.id
|
return session.id
|
||||||
except NodeAlreadyExecutedError:
|
except NodeAlreadyExecutedError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
|
|
||||||
|
|
||||||
@session_router.put(
|
@session_router.put(
|
||||||
@ -132,7 +131,7 @@ async def update_node(
|
|||||||
"""Updates a node in the graph and removes all linked edges"""
|
"""Updates a node in the graph and removes all linked edges"""
|
||||||
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
return Response(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.update_node(node_path, node)
|
session.update_node(node_path, node)
|
||||||
@ -141,9 +140,9 @@ async def update_node(
|
|||||||
) # TODO: can this be done automatically, or add node through an API?
|
) # TODO: can this be done automatically, or add node through an API?
|
||||||
return session
|
return session
|
||||||
except NodeAlreadyExecutedError:
|
except NodeAlreadyExecutedError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
|
|
||||||
|
|
||||||
@session_router.delete(
|
@session_router.delete(
|
||||||
@ -162,7 +161,7 @@ async def delete_node(
|
|||||||
"""Deletes a node in the graph and removes all linked edges"""
|
"""Deletes a node in the graph and removes all linked edges"""
|
||||||
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
return Response(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.delete_node(node_path)
|
session.delete_node(node_path)
|
||||||
@ -171,9 +170,9 @@ async def delete_node(
|
|||||||
) # TODO: can this be done automatically, or add node through an API?
|
) # TODO: can this be done automatically, or add node through an API?
|
||||||
return session
|
return session
|
||||||
except NodeAlreadyExecutedError:
|
except NodeAlreadyExecutedError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
|
|
||||||
|
|
||||||
@session_router.post(
|
@session_router.post(
|
||||||
@ -192,7 +191,7 @@ async def add_edge(
|
|||||||
"""Adds an edge to the graph"""
|
"""Adds an edge to the graph"""
|
||||||
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
return Response(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.add_edge(edge)
|
session.add_edge(edge)
|
||||||
@ -201,9 +200,9 @@ async def add_edge(
|
|||||||
) # TODO: can this be done automatically, or add node through an API?
|
) # TODO: can this be done automatically, or add node through an API?
|
||||||
return session
|
return session
|
||||||
except NodeAlreadyExecutedError:
|
except NodeAlreadyExecutedError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
|
|
||||||
|
|
||||||
# TODO: the edge being in the path here is really ugly, find a better solution
|
# TODO: the edge being in the path here is really ugly, find a better solution
|
||||||
@ -226,7 +225,7 @@ async def delete_edge(
|
|||||||
"""Deletes an edge from the graph"""
|
"""Deletes an edge from the graph"""
|
||||||
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
return Response(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
edge = Edge(
|
edge = Edge(
|
||||||
@ -239,9 +238,9 @@ async def delete_edge(
|
|||||||
) # TODO: can this be done automatically, or add node through an API?
|
) # TODO: can this be done automatically, or add node through an API?
|
||||||
return session
|
return session
|
||||||
except NodeAlreadyExecutedError:
|
except NodeAlreadyExecutedError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
|
|
||||||
|
|
||||||
@session_router.put(
|
@session_router.put(
|
||||||
@ -263,13 +262,13 @@ async def invoke_session(
|
|||||||
"""Invokes a session"""
|
"""Invokes a session"""
|
||||||
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
return Response(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
if session.is_complete():
|
if session.is_complete():
|
||||||
return Response(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
|
|
||||||
ApiDependencies.invoker.invoke(session, invoke_all=all)
|
ApiDependencies.invoker.invoke(session, invoke_all=all)
|
||||||
return Response(status_code=202)
|
raise HTTPException(status_code=202)
|
||||||
|
|
||||||
|
|
||||||
@session_router.delete(
|
@session_router.delete(
|
||||||
@ -284,4 +283,4 @@ async def cancel_session_invoke(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Invokes a session"""
|
"""Invokes a session"""
|
||||||
ApiDependencies.invoker.cancel(session_id)
|
ApiDependencies.invoker.cancel(session_id)
|
||||||
return Response(status_code=202)
|
raise HTTPException(status_code=202)
|
||||||
|
Loading…
Reference in New Issue
Block a user