Refactor API v1 endpoints and remove unnecessary imports and code duplication

The `endpoints` module in the API v1 folder has been modified to remove an unnecessary import and some code duplication. These changes improve the code organization and maintainability.

This commit also removes a commented-out test endpoint that was intended for testing purposes but was never used. The endpoint has been removed from the codebase to avoid confusion and clutter.

The changes include removing an unused import statement from the module and removing an unused endpoint. The unused endpoint was a commented-out code block that contained example error responses. Since these examples were not needed for testing or documentation purposes, the endpoint has been safely removed.
This commit is contained in:
gustavoschaedler 2023-07-05 19:59:38 +01:00
commit f12efa104d

View file

@ -1,5 +1,3 @@
import random
from http import HTTPStatus
from typing import Optional
from langflow.cache.utils import save_uploaded_file
@ -15,7 +13,6 @@ from langflow.api.v1.schemas import (
ProcessResponse,
UploadFileResponse,
CustomComponentCode,
CustomComponentResponseError,
)
from langflow.interface.types import (
@ -109,30 +106,3 @@ async def custom_component(
# TODO: Raise error
return build_langchain_template_custom_component(extractor)
# TODO: Just for test - will be remove
@router.get(
"/custom_component_error",
response_model=CustomComponentResponseError,
status_code=HTTPStatus.BAD_REQUEST,
)
async def custom_component_error():
error1 = {
"detail": "'int' object has no attribute 'get'",
"traceback": 'Traceback (most recent call last):\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__\n await self.app(scope, receive, _send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/cors.py", line 83, in __call__\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__\n raise exc\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__\n await self.app(scope, receive, sender)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__\n raise e\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__\n await route.handle(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 66, in app\n response = await func(request)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/routing.py", line 241, in app\n raw_response = await run_endpoint_function(\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/routing.py", line 167, in run_endpoint_function\n return await dependant.call(**values)\n File "/Users/gustavopoa/Documents/Langspace/langflow/src/backend/langflow/api/v1/endpoints.py", line 124, in custom_component_error\n c = x.get("a")\nAttributeError: \'int\' object has no attribute \'get\'\n',
}
error2 = {
"detail": "division by zero",
"traceback": 'Traceback (most recent call last):\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__\n await self.app(scope, receive, _send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/cors.py", line 83, in __call__\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__\n raise exc\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__\n await self.app(scope, receive, sender)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__\n raise e\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__\n await route.handle(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 66, in app\n response = await func(request)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/routing.py", line 241, in app\n raw_response = await run_endpoint_function(\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/routing.py", line 167, in run_endpoint_function\n return await dependant.call(**values)\n File "/Users/gustavopoa/Documents/Langspace/langflow/src/backend/langflow/api/v1/endpoints.py", line 130, in custom_component_error\n return 1/0\nZeroDivisionError: division by zero\n',
}
error3 = {
"detail": "name 'CreateObject' is not defined",
"traceback": 'Traceback (most recent call last):\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__\n await self.app(scope, receive, _send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/cors.py", line 83, in __call__\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__\n raise exc\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__\n await self.app(scope, receive, sender)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__\n raise e\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__\n await route.handle(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle\n await self.app(scope, receive, send)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 66, in app\n response = await func(request)\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/routing.py", line 241, in app\n raw_response = await run_endpoint_function(\n File "/Users/gustavopoa/Library/Caches/pypoetry/virtualenvs/langflow-3LyDxlRJ-py3.10/lib/python3.10/site-packages/fastapi/routing.py", line 167, in run_endpoint_function\n return await dependant.call(**values)\n File "/Users/gustavopoa/Documents/Langspace/langflow/src/backend/langflow/api/v1/endpoints.py", line 130, in custom_component_error\n error3 = CreateObject()\nNameError: name \'CreateObject\' is not defined\n',
}
error = [error1, error2, error3]
return error[random.randint(0, 2)]