Skip to content

Commit

Permalink
feat: added endpoint to api
Browse files Browse the repository at this point in the history
  • Loading branch information
CaelanHill committed Sep 27, 2024
1 parent eeb0afb commit ef1013b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
34 changes: 34 additions & 0 deletions api/handlers/workflow/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ type OrchestratorRequest struct {
}
type WorkflowOrchestrator interface {
MakeRequestToOrchestrator(endpoint string, payload OrchestratorRequest) (string, error)
GetTriggers() (string, error)
}

type OrchestratorReal struct {
Expand Down Expand Up @@ -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
}
26 changes: 25 additions & 1 deletion api/handlers/workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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})

}


5 changes: 5 additions & 0 deletions api/models/responseModels.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,8 @@ func (d *DurationWrapper) UnmarshalJSON(b []byte) error {
d.Duration = dur
return nil
}


type TriggerResponse struct {
Trigger []string `json:"trigger"`
}

0 comments on commit ef1013b

Please sign in to comment.