Simplify internal/client/set.go by leveraging internal/http.Request()
Co-authored-by: Héctor Hurtado <hector.hurtado@bbva.com>
This commit is contained in:
+4
-25
@@ -3,34 +3,13 @@ package client
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
)
|
"github.com/BBVA/kapow/internal/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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 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 {
|
func SetData(kapowURL, handlerId, path string, r io.Reader) error {
|
||||||
|
|
||||||
req, err := http.NewRequest("PUT", fmt.Sprintf(serverURLTemplate, kapowURL, handlerId, path), r)
|
return http.Put(fmt.Sprintf("%s/%s%s", kapowURL, handlerId, path), r, nil)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user