From ffa262b2b6bbbe5b60d1c9e5b6dfcc98e32e32df Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Thu, 3 Oct 2019 16:49:12 +0200 Subject: [PATCH] Simplify internal/client/set.go by leveraging internal/http.Request() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Héctor Hurtado --- internal/client/set.go | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/internal/client/set.go b/internal/client/set.go index 32d8dc0..f10fda1 100644 --- a/internal/client/set.go +++ b/internal/client/set.go @@ -3,34 +3,13 @@ package client import ( "fmt" "io" - "net/http" -) - -const ( - errMandatoryParam = "Mandatory parameter %s missing" - errInvalidURL = "kapowURL, handlerId or path has invalid format" - errNotFound = "Resource Item Not Found" - errNotValidResource = "Invalid Resource Path" - serverURLTemplate = "%s/%s%s" + + "github.com/BBVA/kapow/internal/http" ) +// TODO: Review spec: Data API > Error responses > 204 Resource Item Not Found should not be 2xx func SetData(kapowURL, handlerId, path string, r io.Reader) error { - req, err := http.NewRequest("PUT", fmt.Sprintf(serverURLTemplate, kapowURL, handlerId, path), r) - if err != nil { - return err - } + return http.Put(fmt.Sprintf("%s/%s%s", kapowURL, handlerId, path), r, nil) - kpowClient := &http.Client{} - if resp, err := kpowClient.Do(req); err != nil { - return err - } else if resp.StatusCode == http.StatusNoContent { - return fmt.Errorf(errNotFound) - } else if resp.StatusCode == http.StatusBadRequest { - return fmt.Errorf(errNotValidResource) - } else if resp.StatusCode >= http.StatusNotFound { - return fmt.Errorf(resp.Status[4:]) - } - - return nil }