From 4d2f410c980a070207008788b55de772026e5715 Mon Sep 17 00:00:00 2001 From: "Dr. Carsten Leue" Date: Fri, 15 Dec 2023 16:00:58 +0100 Subject: [PATCH] fix: add MakeBodyRequest Signed-off-by: Dr. Carsten Leue --- file/getters.go | 15 ++++++++++++++- ioeither/http/request.go | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/file/getters.go b/file/getters.go index 113edc1..4bc38b8 100644 --- a/file/getters.go +++ b/file/getters.go @@ -15,7 +15,10 @@ package file -import "path/filepath" +import ( + "io" + "path/filepath" +) // Join appends a filename to a root path func Join(name string) func(root string) string { @@ -23,3 +26,13 @@ func Join(name string) func(root string) string { return filepath.Join(root, name) } } + +// ToReader converts a [io.Reader] +func ToReader[R io.Reader](r R) io.Reader { + return r +} + +// ToCloser converts a [io.Closer] +func ToCloser[C io.Closer](c C) io.Closer { + return c +} diff --git a/ioeither/http/request.go b/ioeither/http/request.go index daa626b..a6bc524 100644 --- a/ioeither/http/request.go +++ b/ioeither/http/request.go @@ -16,10 +16,12 @@ package http import ( + "bytes" "io" "net/http" B "github.com/IBM/fp-go/bytes" + FL "github.com/IBM/fp-go/file" F "github.com/IBM/fp-go/function" H "github.com/IBM/fp-go/http" IOE "github.com/IBM/fp-go/ioeither" @@ -51,6 +53,24 @@ var ( MakeGetRequest = makeRequest("GET", nil) ) +// MakeBodyRequest creates a request that carries a body +func MakeBodyRequest(method string, body IOE.IOEither[error, []byte]) func(url string) IOE.IOEither[error, *http.Request] { + onBody := F.Pipe1( + body, + IOE.Map[error](F.Flow2( + bytes.NewReader, + FL.ToReader[*bytes.Reader], + )), + ) + onRelease := IOE.Of[error, io.Reader] + withMethod := F.Bind1of3(MakeRequest)(method) + + return F.Flow2( + F.Bind1of2(withMethod), + IOE.WithResource[*http.Request](onBody, onRelease), + ) +} + func (client client) Do(req Requester) IOE.IOEither[error, *http.Response] { return F.Pipe1( req,