From d23108a4215d88e4ffbcc43609a2306ce7e7bcb6 Mon Sep 17 00:00:00 2001 From: Piotr Date: Mon, 29 Apr 2024 18:57:02 +0200 Subject: [PATCH] Implement ControllerClient and ProcessResponse Created the ControllerClient interface and implemented its Execute method for making HTTP requests. Also, the ProcessResponse struct was created for parsing the API response. These changes better modularize the code for reusability and improved response handling. --- internal/controllers/controller-client.go | 51 +++++++++++++++++++++++ internal/controllers/rest-controller.go | 6 ++- web/modules/editor/editor.go | 5 +++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 internal/controllers/controller-client.go diff --git a/internal/controllers/controller-client.go b/internal/controllers/controller-client.go new file mode 100644 index 0000000..b9f176a --- /dev/null +++ b/internal/controllers/controller-client.go @@ -0,0 +1,51 @@ +package controllers + +import ( + "bytes" + "encoding/json" + "github.com/PiotrFerenc/mash2/api/types" + "github.com/google/uuid" + "io" + "log" + "net/http" +) + +type ControllerClient interface { + Execute(url string, pipeline types.Pipeline) (uuid.UUID, error) +} +type client struct { +} + +func CreateControllerClient() ControllerClient { + return &client{} +} +func (c *client) Execute(url string, pipeline types.Pipeline) (uuid.UUID, error) { + + data := []byte(`{"key":"value"}`) + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) + if err != nil { + log.Fatalf("Error occurred while creating request: %s", err.Error()) + } + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Fatalf("Error occurred while sending request: %s", err.Error()) + } + + defer resp.Body.Close() + + body, readErr := io.ReadAll(resp.Body) + if readErr != nil { + log.Fatalf("Error occurred while reading response: %s", readErr.Error()) + } + + var processResponse ProcessResponse + jsonErr := json.Unmarshal(body, &processResponse) + if jsonErr != nil { + log.Fatalf("Error occurred while unmarshalling response: %s", jsonErr.Error()) + } + return processResponse.ProcessId, nil +} diff --git a/internal/controllers/rest-controller.go b/internal/controllers/rest-controller.go index 677d88a..2a506dc 100644 --- a/internal/controllers/rest-controller.go +++ b/internal/controllers/rest-controller.go @@ -112,6 +112,10 @@ func executeHandler(c *controller) func(context *gin.Context) { if err != nil { context.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err}) } - context.JSON(http.StatusOK, gin.H{"processId": processId}) + context.JSON(http.StatusOK, ProcessResponse{ProcessId: processId}) } } + +type ProcessResponse struct { + ProcessId uuid.UUID `json:"processId"` +} diff --git a/web/modules/editor/editor.go b/web/modules/editor/editor.go index 7070665..09afc0b 100644 --- a/web/modules/editor/editor.go +++ b/web/modules/editor/editor.go @@ -28,6 +28,11 @@ func CreateEditorHandler() func(c echo.Context) error { return c.Render(http.StatusOK, "edytor.html", data) } } +func ExecutePipelineHandler() func(c echo.Context) error { + return func(c echo.Context) error { + + } +} func initActions() (map[string]string, error) { actions := executor.CreateActionMap(&configuration.Config{}) result := make(map[string]string, len(actions))