SoumyaJ commited on
Commit
79ff0e6
·
verified ·
1 Parent(s): a904f32

Upload 5 files

Browse files
Files changed (5) hide show
  1. app.py +53 -0
  2. config.yaml +2 -0
  3. requirements.txt +5 -0
  4. workflow_invocation.py +30 -0
  5. workflowschema.py +15 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI,HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import JSONResponse
4
+ import uvicorn
5
+ from pydantic import BaseModel, Field
6
+ from workflowschema import LibraryModel
7
+ from workflow_invocation import WorkflowInvocation
8
+ import asyncio
9
+ import json
10
+
11
+ app = FastAPI()
12
+
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"], # Allows all origins
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ @app.post("/api/v1/trigger_email_workflow")
22
+ def trigger_workflow(payLoad: LibraryModel):
23
+ try:
24
+ if payLoad:
25
+ wf_invocation = WorkflowInvocation(payLoad.model_dump())
26
+ asyncio.run(wf_invocation.invoke_workflow())
27
+ return JSONResponse(content={"message": "workflow executed successfully"}, status_code=200)
28
+ except Exception as e:
29
+ raise HTTPException(status_code=500, detail=str(e))
30
+
31
+ if __name__ == "__main__":
32
+ uvicorn.run(app, host = "0.0.0.0", port = 8000)
33
+ # schema_input = {
34
+ # "name": "Media",
35
+ # "action": "modified",
36
+ # "change_type": "MediaStatus",
37
+ # "changed_by": "focus",
38
+ # "timestamp": "2025-07-23T13:45:00Z",
39
+ # "change_id": 1001,
40
+ # "change_from": "ON",
41
+ # "change_to": "OFF",
42
+ # "unique_identifier": "library",
43
+ # "title":"OCA"
44
+ # }
45
+ # result = trigger_workflow(schema_input)
46
+ # print(json.loads(result.body))
47
+
48
+
49
+
50
+
51
+
52
+
53
+
config.yaml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ workflow:
2
+ webhook_url: "http://localhost:5678/webhook/5f90600a-ba81-4521-a90b-3852d3613181"
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pydantic
4
+ httpx
5
+ pyyaml
workflow_invocation.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from workflowschema import LibraryModel
2
+ import httpx
3
+ import asyncio
4
+ import yaml
5
+
6
+
7
+ class WorkflowInvocation:
8
+ def __init__(self, payload):
9
+ self.config = self.load_config()
10
+ self.payload = payload
11
+
12
+ def load_config(self):
13
+ with open("config.yaml","r") as file:
14
+ config = yaml.safe_load(file)
15
+ return config
16
+
17
+
18
+ async def invoke_workflow(self):
19
+ try:
20
+ webHookUrl = self.config['workflow']['webhook_url']
21
+
22
+ async with httpx.AsyncClient() as client:
23
+ response = await client.post(webHookUrl, json=self.payload)
24
+ response.raise_for_status()
25
+ json_response = response.json()
26
+ return json_response
27
+ #print("Response status code:", response.json())
28
+ except Exception as e:
29
+ print(f"An error occurred: {e}")
30
+
workflowschema.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field
2
+ from typing import List,Literal,Any
3
+
4
+ class LibraryModel(BaseModel):
5
+ name: str = Field(..., description="Name of the module that has changed")
6
+ action: Literal["added", "removed", "modified"] = Field(..., description="Action performed on that module", examples=["added", "removed", "modified"])
7
+ change_type: str = Field(..., description="The field or the data that has changed in the module")
8
+ changed_by: str = Field(..., description="The user who made the change")
9
+ timestamp: str = Field(..., description="Timestamp of the change in ISO format")
10
+ change_id: int = Field(..., description="Unique identifier for the change in the module")
11
+ change_from: Any = Field(..., description="The previous value of the field that has changed")
12
+ change_to: Any = Field(..., description="The new value of the field that has changed")
13
+ title: str = Field(..., description="Title of the module that has changed")
14
+ unique_identifier: Literal["library","scheduling"] = Field(..., description="Info about the module that has changed", examples=["library", "scheduling"])
15
+