Skip to content

Commit

Permalink
fix(datastore): change api expected request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan-pad committed Apr 26, 2024
1 parent 4066413 commit b3abca5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 50 deletions.
9 changes: 6 additions & 3 deletions internal/datastore/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package api_test

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand All @@ -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))
Expand Down
11 changes: 3 additions & 8 deletions internal/datastore/api/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
22 changes: 5 additions & 17 deletions internal/datastore/api/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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())
Expand Down
25 changes: 3 additions & 22 deletions internal/datastore/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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},
Expand All @@ -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 {
Expand Down

0 comments on commit b3abca5

Please sign in to comment.