1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: add MakeBodyRequest

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-12-15 16:00:58 +01:00
parent 8f49c1328c
commit 4d2f410c98
2 changed files with 34 additions and 1 deletions

View File

@@ -15,7 +15,10 @@
package file package file
import "path/filepath" import (
"io"
"path/filepath"
)
// Join appends a filename to a root path // Join appends a filename to a root path
func Join(name string) func(root string) string { 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) 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
}

View File

@@ -16,10 +16,12 @@
package http package http
import ( import (
"bytes"
"io" "io"
"net/http" "net/http"
B "github.com/IBM/fp-go/bytes" B "github.com/IBM/fp-go/bytes"
FL "github.com/IBM/fp-go/file"
F "github.com/IBM/fp-go/function" F "github.com/IBM/fp-go/function"
H "github.com/IBM/fp-go/http" H "github.com/IBM/fp-go/http"
IOE "github.com/IBM/fp-go/ioeither" IOE "github.com/IBM/fp-go/ioeither"
@@ -51,6 +53,24 @@ var (
MakeGetRequest = makeRequest("GET", nil) 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] { func (client client) Do(req Requester) IOE.IOEither[error, *http.Response] {
return F.Pipe1( return F.Pipe1(
req, req,