From b3abca5bae56f190dffd4e9eb4dadee151de94a8 Mon Sep 17 00:00:00 2001 From: Alan Date: Fri, 26 Apr 2024 10:48:31 +0200 Subject: [PATCH] fix(datastore): change api expected request and response --- internal/datastore/api/api_test.go | 9 ++++++--- internal/datastore/api/logs.go | 11 +++-------- internal/datastore/api/plans.go | 22 +++++----------------- internal/datastore/client/client.go | 25 +++---------------------- 4 files changed, 17 insertions(+), 50 deletions(-) diff --git a/internal/datastore/api/api_test.go b/internal/datastore/api/api_test.go index 821d3421..f3069803 100644 --- a/internal/datastore/api/api_test.go +++ b/internal/datastore/api/api_test.go @@ -2,6 +2,7 @@ package api_test import ( + "bytes" "net/http" "net/http/httptest" "testing" @@ -42,7 +43,8 @@ var _ = BeforeSuite(func() { }) func getContext(method string, path string, params map[string]string, body []byte) echo.Context { - req := httptest.NewRequest(method, path, nil) + buf := bytes.NewBuffer(body) + req := httptest.NewRequest(method, path, buf) rec := httptest.NewRecorder() context := e.NewContext(req, rec) for k, v := range params { @@ -186,7 +188,7 @@ var _ = Describe("Datastore API", func() { Describe("Write", func() { Describe("Logs", func() { It("should return 200 OK", func() { - body := []byte(`{"content": "test1"}`) + body := []byte(`test1`) context := getContext(http.MethodPut, "/logs", map[string]string{ "namespace": "default", "layer": "test1", @@ -200,13 +202,14 @@ var _ = Describe("Datastore API", func() { }) Describe("Plans", func() { It("should return 200 OK", func() { + body := []byte(`test1`) context := getContext(http.MethodPut, "/plans", map[string]string{ "namespace": "default", "layer": "test1", "run": "test1", "attempt": "0", "format": "json", - }, nil) + }, body) err := API.PutPlanHandler(context) Expect(err).NotTo(HaveOccurred()) Expect(context.Response().Status).To(Equal(http.StatusOK)) diff --git a/internal/datastore/api/logs.go b/internal/datastore/api/logs.go index 349227b1..d5ab4301 100644 --- a/internal/datastore/api/logs.go +++ b/internal/datastore/api/logs.go @@ -13,10 +13,6 @@ type GetLogsResponse struct { Results []string `json:"results"` } -type PutLogsRequest struct { - Content string `json:"content"` -} - func getLogsArgs(c echo.Context) (string, string, string, string, error) { namespace := c.QueryParam("namespace") layer := c.QueryParam("layer") @@ -61,11 +57,10 @@ func (a *API) PutLogsHandler(c echo.Context) error { if err != nil { return c.String(http.StatusBadRequest, err.Error()) } - request := PutLogsRequest{} - if err := c.Bind(&request); err != nil { - return c.String(http.StatusBadRequest, "could not read request body") + _, err = c.Request().Body.Read(content) + if err != nil { + return c.String(http.StatusBadRequest, "could not read request body: "+err.Error()) } - content = []byte(request.Content) err = a.Storage.PutLogs(namespace, layer, run, attempt, content) if err != nil { return c.String(http.StatusInternalServerError, "could not put logs, there's an issue with the storage backend") diff --git a/internal/datastore/api/plans.go b/internal/datastore/api/plans.go index 0e5007e4..5fedecfe 100644 --- a/internal/datastore/api/plans.go +++ b/internal/datastore/api/plans.go @@ -8,15 +8,6 @@ import ( storageerrors "github.com/padok-team/burrito/internal/datastore/storage/error" ) -type GetPlanResponse struct { - Plan []byte `json:"plan"` - Format string `json:"format"` -} - -type PutPlanRequest struct { - Plan []byte `json:"content"` -} - func getPlanArgs(c echo.Context) (string, string, string, string, string, error) { namespace := c.QueryParam("namespace") layer := c.QueryParam("layer") @@ -39,7 +30,6 @@ func (a *API) GetPlanHandler(c echo.Context) error { if err != nil { return c.String(http.StatusBadRequest, err.Error()) } - response := GetPlanResponse{} if attempt == "" { content, err = a.Storage.GetLatestPlan(namespace, layer, run, format) } else { @@ -51,9 +41,7 @@ func (a *API) GetPlanHandler(c echo.Context) error { if err != nil { return c.String(http.StatusInternalServerError, "could not get logs, there's an issue with the storage backend") } - response.Plan = content - response.Format = format - return c.JSON(http.StatusOK, &response) + return c.Blob(http.StatusOK, "application/octet-stream", content) } func (a *API) PutPlanHandler(c echo.Context) error { @@ -66,11 +54,11 @@ func (a *API) PutPlanHandler(c echo.Context) error { if attempt == "" || format == "" { return c.String(http.StatusBadRequest, "missing query parameters") } - request := PutPlanRequest{} - if err := c.Bind(&request); err != nil { - return c.String(http.StatusBadRequest, "could not read request body") + + _, err = c.Request().Body.Read(content) + if err != nil { + return c.String(http.StatusBadRequest, "could not read request body: "+err.Error()) } - content = []byte(request.Plan) err = a.Storage.PutPlan(namespace, layer, run, attempt, format, content) if err != nil { return c.String(http.StatusInternalServerError, "could not put logs, there's an issue with the storage backend: "+err.Error()) diff --git a/internal/datastore/client/client.go b/internal/datastore/client/client.go index e73d14d8..4d9a3e9f 100644 --- a/internal/datastore/client/client.go +++ b/internal/datastore/client/client.go @@ -93,22 +93,10 @@ func (c *DefaultClient) GetPlan(namespace string, layer string, run string, atte if err != nil { return nil, err } - jresp := api.GetPlanResponse{} - err = json.Unmarshal(b, &jresp) - if err != nil { - return nil, err - } - return jresp.Plan, nil + return b, nil } func (c *DefaultClient) PutPlan(namespace string, layer string, run string, attempt string, format string, content []byte) error { - requestBody := api.PutLogsRequest{ - Content: string(content), - } - body, err := json.Marshal(requestBody) - if err != nil { - return err - } req, err := c.buildRequest( "/api/plans", url.Values{ @@ -119,7 +107,7 @@ func (c *DefaultClient) PutPlan(namespace string, layer string, run string, atte "format": {format}, }, http.MethodPut, - bytes.NewBuffer(body), + bytes.NewBuffer(content), ) req.Header.Set("Content-Type", "application/json") if err != nil { @@ -183,13 +171,6 @@ func (c *DefaultClient) GetLogs(namespace string, layer string, run string, atte } func (c *DefaultClient) PutLogs(namespace string, layer string, run string, attempt string, content []byte) error { - requestBody := api.PutLogsRequest{ - Content: string(content), - } - body, err := json.Marshal(requestBody) - if err != nil { - return err - } queryParams := url.Values{ "namespace": {namespace}, "layer": {layer}, @@ -200,7 +181,7 @@ func (c *DefaultClient) PutLogs(namespace string, layer string, run string, atte "/api/logs", queryParams, http.MethodPut, - bytes.NewBuffer(body), + bytes.NewBuffer(content), ) req.Header.Set("Content-Type", "application/json") if err != nil {