From ef1013b9b5ea507fa966e0b1c04201e1c0074a3c Mon Sep 17 00:00:00 2001 From: Caelan Hill Date: Fri, 27 Sep 2024 20:33:56 +0200 Subject: [PATCH] feat: added endpoint to api --- api/handlers/workflow/model.go | 34 +++++++++++++++++++++++++++++++ api/handlers/workflow/workflow.go | 26 ++++++++++++++++++++++- api/models/responseModels.go | 5 +++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/api/handlers/workflow/model.go b/api/handlers/workflow/model.go index 51638742..e73b0192 100644 --- a/api/handlers/workflow/model.go +++ b/api/handlers/workflow/model.go @@ -310,6 +310,7 @@ type OrchestratorRequest struct { } type WorkflowOrchestrator interface { MakeRequestToOrchestrator(endpoint string, payload OrchestratorRequest) (string, error) + GetTriggers() (string, error) } type OrchestratorReal struct { @@ -366,3 +367,36 @@ func (w OrchestratorReal) MakeRequestToOrchestrator(endpoint string, payload Orc return responseBody, nil } + +func (w OrchestratorReal) GetTriggers() (string, error) { + logger := utilities.NewLogger().LogWithCaller() + + // Send the GET request to the orchestrator + resp, err := http.Get("http://localhost:8090/triggers") + if err != nil { + logger.Error("get error: ", err) + return "", fmt.Errorf("internal server error") + } + defer resp.Body.Close() + + // Check for a successful status code (200 OK) + if resp.StatusCode != http.StatusOK { + logger.Error("status code error: ", resp.StatusCode) + return "", fmt.Errorf("internal server error") + } + + // Read the response body + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + logger.Error("read body error: ", err) + return "", fmt.Errorf("internal server error") + } + + // Convert the response body to a string + responseBody := string(bodyBytes) + + // Log the response body for debugging + logger.Info("Response Body: ", responseBody) + + return responseBody, nil +} diff --git a/api/handlers/workflow/workflow.go b/api/handlers/workflow/workflow.go index 9c5557ca..d84f2ae5 100644 --- a/api/handlers/workflow/workflow.go +++ b/api/handlers/workflow/workflow.go @@ -23,9 +23,11 @@ func SetupWorkflowRoutes(g *gin.RouterGroup, h Workflow) { g.PATCH("/:id", h.UpdateWorkflow) g.DELETE("/:id", h.DeleteWorkflow) + //manage active workflows // g.POST("/activate", h.NewActiveWorkflow) - g.POST("/reset", h.ResetActiveWorkflow) + g.PATCH("/reset", h.ResetActiveWorkflow) + g.GET("/triggers", h.GetTriggers) // g.POST("/complete", h.CompleteActiveWorkflow) } @@ -574,4 +576,26 @@ func (w Workflow) ResetActiveWorkflow(c *gin.Context) { c.JSON(http.StatusOK, models.Response{Data: "Database updated and request to Reset workflow sent"}) } +func (w Workflow) GetTriggers(c *gin.Context) { + logger := utilities.NewLogger().LogWithCaller() + + triggers, err := w.OrchestratorEntity.GetTriggers() + if err != nil { + logger.Error(err) + c.JSON(http.StatusInternalServerError, models.Response{Error: "Internal Server Error"}) + return + } + + var response models.TriggerResponse + err = json.Unmarshal([]byte(triggers), &response) + if err != nil { + logger.Error(err) + c.JSON(http.StatusInternalServerError, models.Response{Error: "Internal Server Error"}) + return + } + + c.JSON(http.StatusOK, models.Response{Data: response}) + +} + diff --git a/api/models/responseModels.go b/api/models/responseModels.go index 6123165f..80f1e0ac 100644 --- a/api/models/responseModels.go +++ b/api/models/responseModels.go @@ -177,3 +177,8 @@ func (d *DurationWrapper) UnmarshalJSON(b []byte) error { d.Duration = dur return nil } + + +type TriggerResponse struct { + Trigger []string `json:"trigger"` +} \ No newline at end of file