Skip to content

Commit

Permalink
fix: add MakeBodyRequest
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Dec 15, 2023
1 parent 8f49c13 commit 4d2f410
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 14 additions & 1 deletion file/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@

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 {
return 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
}
20 changes: 20 additions & 0 deletions ioeither/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4d2f410

Please sign in to comment.