LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
feat: Support downloadable postman collection for API deployments BE (#261)
Chandrasekharan Me2ed9089525f
diff --git a/backend/api/api_deployment_views.py b/backend/api/api_deployment_views.pyindex f8b5007ea..6c8db8249 100644--- a/backend/api/api_deployment_views.py+++ b/backend/api/api_deployment_views.py@@ -1,8 +1,12 @@+import json+import loggingfrom typing import Any, Optional+from api.constants import ApiExecutionfrom api.deployment_helper import DeploymentHelper-from api.exceptions import InvalidAPIRequest+from api.exceptions import InvalidAPIRequest, NoActiveAPIKeyErrorfrom api.models import APIDeployment+from api.postman_collection.dto import PostmanCollectionfrom api.serializers import (APIDeploymentListSerializer,APIDeploymentSerializer,@@ -10,6 +14,7 @@ from api.serializers import (ExecutionRequestSerializer,)from django.db.models import QuerySet+from django.http import HttpResponsefrom permissions.permission import IsOwnerfrom rest_framework import serializers, status, views, viewsetsfrom rest_framework.decorators import action@@ -19,6 +24,8 @@ from rest_framework.serializers import Serializerfrom utils.enums import CeleryTaskStatefrom workflow_manager.workflow.dto import ExecutionResponse+logger = logging.getLogger(__name__)+class DeploymentExecution(views.APIView):def initialize_request(@@ -39,7 +46,7 @@ class DeploymentExecution(views.APIView):def post(self, request: Request, org_name: str, api_name: str, api: APIDeployment) -> Response:- file_objs = request.FILES.getlist("files")+ file_objs = request.FILES.getlist(ApiExecution.FILES_FORM_DATA)serializer = ExecutionRequestSerializer(data=request.data)serializer.is_valid(raise_exception=True)timeout = serializer.get_timeout(serializer.validated_data)@@ -119,9 +126,24 @@ class APIDeploymentViewSet(viewsets.ModelViewSet):headers=headers,)+ @action(detail=True, methods=["get"])+ def download_postman_collection(+ self, request: Request, pk: Optional[str] = None+ ) -> Response:+ """Downloads a Postman Collection of the API deployment instance."""+ instance = self.get_object()+ api_key_inst = instance.apikey_set.filter(is_active=True).first()+ if not api_key_inst:+ logger.error(f"No active API key set for deployment {instance.pk}")+ raise NoActiveAPIKeyError(deployment_name=instance.display_name)-def get_error_from_serializer(error_details: dict[str, Any]) -> Optional[str]:- error_key = next(iter(error_details))- # Get the first error message- error_message: str = f"{error_details[error_key][0]} : {error_key}"- return error_message+ postman_collection = PostmanCollection.create(+ instance=instance, api_key=api_key_inst.api_key+ )+ response = HttpResponse(+ json.dumps(postman_collection.to_dict()), content_type="application/json"+ )+ response["Content-Disposition"] = (+ f'attachment; filename="{instance.display_name}.json"'+ )+ return responsediff --git a/backend/api/constants.py b/backend/api/constants.pyindex 7aa2a4629..c0f056091 100644--- a/backend/api/constants.py+++ b/backend/api/constants.py@@ -1,3 +1,6 @@class ApiExecution:PATH: str = "deployment/api"MAXIMUM_TIMEOUT_IN_SEC: int = 300 # 5 minutes+ DEFAULT_TIMEOUT_IN_SEC: int = 80+ FILES_FORM_DATA: str = "files"+ TIMEOUT_FORM_DATA: str = "timeout"diff --git a/backend/api/exceptions.py b/backend/api/exceptions.pyindex f8bb97c5b..c3a58ff05 100644--- a/backend/api/exceptions.py+++ b/backend/api/exceptions.py@@ -12,13 +12,6 @@ class ApiKeyCreateException(APIException):status_code = 500default_detail = "Exception while create API key"- def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):- if detail is not None:- self.detail = detail- if code is not None:- self.code = code- super().__init__(detail, code)-class Forbidden(APIException):status_code = 403@@ -26,57 +19,37 @@ class Forbidden(APIException):"User is forbidden from performing this action. Please contact admin")- def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):- if detail is not None:- self.detail = detail- if code is not None:- self.code = code- super().__init__(detail, code)-class APINotFound(APIException):status_code = 404- default_detail = "Api not found"-- def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):- if detail is not None:- self.detail = detail- if code is not None:- self.code = code- super().__init__(detail, code)+ default_detail = "API not found"class InvalidAPIRequest(APIException):status_code = 400default_detail = "Bad request"- def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):- if detail is not None:- self.detail = detail- if code is not None:- self.code = code- super().__init__(detail, code)-class InactiveAPI(APIException):status_code = 404default_detail = "API not found or Inactive"- def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):- if detail is not None:- self.detail = detail- if code is not None:- self.code = code- super().__init__(detail, code)-class UnauthorizedKey(APIException):status_code = 401default_detail = "Unauthorized"- def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):- if detail is not None:- self.detail = detail- if code is not None:- self.code = code++class NoActiveAPIKeyError(APIException):+ status_code = 409+ default_detail = "No active API keys configured for this deployment"++ def __init__(+ self,+ detail: Optional[str] = None,+ code: Optional[str] = None,+ deployment_name: str = "this deployment",+ ):+ if detail is None:+ detail = f"No active API keys configured for {deployment_name}"super().__init__(detail, code)diff --git a/backend/api/postman_collection/__init__.py b/backend/api/postman_collection/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/backend/api/postman_collection/constants.py b/backend/api/postman_collection/constants.pynew file mode 100644index 000000000..ffb3ef729--- /dev/null+++ b/backend/api/postman_collection/constants.py@@ -0,0 +1,6 @@+class CollectionKey:+ POSTMAN_COLLECTION_V210 = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" # noqa: E501+ EXECUTE_API_KEY = "Process document"+ STATUS_API_KEY = "Execution status"+ STATUS_EXEC_ID_DEFAULT = "REPLACE_WITH_EXECUTION_ID"+ AUTH_QUERY_PARAM_DEFAULT = "REPLACE_WITH_API_KEY"diff --git a/backend/api/postman_collection/dto.py b/backend/api/postman_collection/dto.pynew file mode 100644index 000000000..e905e64f8--- /dev/null+++ b/backend/api/postman_collection/dto.py@@ -0,0 +1,134 @@+from dataclasses import asdict, dataclass, field+from typing import Any, Optional+from urllib.parse import urlencode, urljoin++from api.constants import ApiExecution+from api.models import APIDeployment+from api.postman_collection.constants import CollectionKey+from django.conf import settings+from utils.request import HTTPMethod+++@dataclass+class HeaderItem:+ key: str+ value: str+++@dataclass+class FormDataItem:+ key: str+ type: str+ src: Optional[str] = None+ value: Optional[str] = None++ def __post_init__(self) -> None:+ if self.type == "file":+ if self.src is None:+ raise ValueError("src must be provided for type 'file'")+ elif self.type == "text":+ if self.value is None:+ raise ValueError("value must be provided for type 'text'")+ else:+ raise ValueError(f"Unsupported type for form data: {self.type}")+++@dataclass+class BodyItem:+ formdata: list[FormDataItem]+ mode: str = "formdata"+++@dataclass+class RequestItem:+ method: HTTPMethod+ url: str+ header: list[HeaderItem]+ body: Optional[BodyItem] = None+++@dataclass+class PostmanItem:+ name: str+ request: RequestItem+++@dataclass+class PostmanInfo:+ name: str = "Unstract's API deployment"+ schema: str = CollectionKey.POSTMAN_COLLECTION_V210+ description: str = "Contains APIs meant for using the deployed Unstract API"+++@dataclass+class PostmanCollection:+ info: PostmanInfo+ item: list[PostmanItem] = field(default_factory=list)++ @classmethod+ def create(+ cls,+ instance: APIDeployment,+ api_key: str = CollectionKey.AUTH_QUERY_PARAM_DEFAULT,+ ) -> "PostmanCollection":+ """Creates a PostmanCollection instance.++ This instance can help represent Postman collections (v2 format) that+ can be used to easily invoke workflows deployed as APIs++ Args:+ instance (APIDeployment): API deployment to generate collection for+ api_key (str, optional): Active API key used to authenticate requests for+ deployed APIs. Defaults to CollectionKey.AUTH_QUERY_PARAM_DEFAULT.++ Returns:+ PostmanCollection: Instance representing PostmanCollection+ """+ postman_info = PostmanInfo(+ name=instance.display_name, description=instance.description+ )+ header_list = [HeaderItem(key="Authorization", value=f"Bearer {api_key}")]+ abs_api_endpoint = urljoin(settings.WEB_APP_ORIGIN_URL, instance.api_endpoint)++ # API execution API+ execute_body = BodyItem(+ formdata=[+ FormDataItem(+ key=ApiExecution.FILES_FORM_DATA, type="file", src="/path_to_file"+ ),+ FormDataItem(+ key=ApiExecution.TIMEOUT_FORM_DATA,+ type="text",+ value=ApiExecution.DEFAULT_TIMEOUT_IN_SEC,+ ),+ ]+ )+ execute_request = RequestItem(+ method=HTTPMethod.POST,+ header=header_list,+ body=execute_body,+ url=abs_api_endpoint,+ )++ # Status API+ status_query_param = {"execution_id": CollectionKey.STATUS_EXEC_ID_DEFAULT}+ status_query_str = urlencode(status_query_param)+ status_url = urljoin(abs_api_endpoint, "?" + status_query_str)+ status_request = RequestItem(+ method=HTTPMethod.GET, header=header_list, url=status_url+ )++ postman_item_list = [+ PostmanItem(name=CollectionKey.EXECUTE_API_KEY, request=execute_request),+ PostmanItem(name=CollectionKey.STATUS_API_KEY, request=status_request),+ ]+ return cls(info=postman_info, item=postman_item_list)++ def to_dict(self) -> dict[str, Any]:+ """Convert PostmanCollection instance to a dict.++ Returns:+ dict[str, Any]: PostmanCollection as a dict+ """+ collection_dict = asdict(self)+ return collection_dictdiff --git a/backend/api/serializers.py b/backend/api/serializers.pyindex 1b56e0d55..6f483f5e9 100644--- a/backend/api/serializers.py+++ b/backend/api/serializers.py@@ -68,7 +68,7 @@ class ExecutionRequestSerializer(Serializer):return valuedef get_timeout(self, validated_data: dict[str, Union[int, None]]) -> int:- value = validated_data.get("timeout", -1)+ value = validated_data.get(ApiExecution.TIMEOUT_FORM_DATA, -1)if not isinstance(value, int):raise ValidationError("timeout must be a integer.")return valuediff --git a/backend/api/urls.py b/backend/api/urls.pyindex 2b149bf48..43eabf73e 100644--- a/backend/api/urls.py+++ b/backend/api/urls.py@@ -17,6 +17,12 @@ deployment_details = APIDeploymentViewSet.as_view("delete": APIDeploymentViewSet.destroy.__name__,})+download_postman_collection = APIDeploymentViewSet.as_view(+ {+ "get": APIDeploymentViewSet.download_postman_collection.__name__,+ }+)+execute = DeploymentExecution.as_view()key_details = APIKeyViewSet.as_view(@@ -41,6 +47,11 @@ urlpatterns = format_suffix_patterns(deployment_details,name="api_deployment_details",),+ path(+ "postman_collection/<uuid:pk>/",+ download_postman_collection,+ name="download_postman_collection",+ ),re_path(r"^api/(?P<org_name>[\w-]+)/(?P<api_name>[\w-]+)/?$",execute,diff --git a/backend/utils/request/request.py b/backend/utils/request/request.pyindex 9c05df155..be802af96 100644--- a/backend/utils/request/request.py+++ b/backend/utils/request/request.py@@ -8,7 +8,7 @@ from requests.exceptions import RequestExceptionlogger = logging.getLogger(__name__)-class HTTPMethod(Enum):+class HTTPMethod(str, Enum):GET = "GET"POST = "POST"DELETE = "DELETE"diff --git a/frontend/src/components/deployments/api-deployment/ApiDeployment.jsx b/frontend/src/components/deployments/api-deployment/ApiDeployment.jsxindex b7be9444a..b9bf99c80 100644--- a/frontend/src/components/deployments/api-deployment/ApiDeployment.jsx+++ b/frontend/src/components/deployments/api-deployment/ApiDeployment.jsx@@ -5,6 +5,7 @@ import {EditOutlined,EllipsisOutlined,KeyOutlined,+ CloudDownloadOutlined,} from "@ant-design/icons";import { Button, Dropdown, Space, Switch, Tooltip, Typography } from "antd";import { useEffect, useState } from "react";@@ -233,6 +234,38 @@ function ApiDeployment() {});};+ const downloadPostmanCollection = () => {+ apiDeploymentsApiService+ .downloadPostmanCollection(selectedRow?.id)+ .then((res) => {+ const { data, headers } = res;+ const href = URL.createObjectURL(data);+ // Get filename from header or use a default+ const filename =+ headers["content-disposition"]+ ?.split("filename=")[1]+ ?.trim()+ .replaceAll('"', "") || "postman_collection.json";+ // create "a" HTML element with href to file & click+ const link = document.createElement("a");+ link.href = href;+ link.setAttribute("download", filename);+ document.body.appendChild(link);+ link.click();++ // clean up "a" element & remove ObjectURL+ document.body.removeChild(link);+ URL.revokeObjectURL(href);+ setAlertDetails({+ type: "success",+ content: "Collection downloaded successfully",+ });+ })+ .catch((err) => {+ setAlertDetails(handleException(err));+ });+ };+const openAddModal = (edit) => {setIsEdit(edit);setOpenAddApiModal(true);@@ -262,7 +295,7 @@ function ApiDeployment() {<Spacedirection="horizontal"className="action-items"- onClick={() => getApiKeys()}+ onClick={getApiKeys}><div><KeyOutlined />@@ -275,6 +308,23 @@ function ApiDeployment() {},{key: "3",+ label: (+ <Space+ direction="horizontal"+ className="action-items"+ onClick={downloadPostmanCollection}+ >+ <div>+ <CloudDownloadOutlined />+ </div>+ <div>+ <Typography.Text>Download Postman Collection</Typography.Text>+ </div>+ </Space>+ ),+ },+ {+ key: "4",label: (<Spacedirection="horizontal"@@ -291,7 +341,7 @@ function ApiDeployment() {),},{- key: "4",+ key: "5",label: (<Spacedirection="horizontal"diff --git a/frontend/src/components/deployments/api-deployment/api-deployments-service.js b/frontend/src/components/deployments/api-deployment/api-deployments-service.jsindex a752f2d47..5f062ced8 100644--- a/frontend/src/components/deployments/api-deployment/api-deployments-service.js+++ b/frontend/src/components/deployments/api-deployment/api-deployments-service.js@@ -81,6 +81,14 @@ function apiDeploymentsService() {};return axiosPrivate(options);},+ downloadPostmanCollection: (id) => {+ options = {+ method: "GET",+ url: `${path}/api/postman_collection/${id}/`,+ responseType: "blob",+ };+ return axiosPrivate(options);+ },};}