Skip to content

Commit

Permalink
fix: add WithFormData and WithJson
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 16, 2023
1 parent 1f675e0 commit 9668642
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ioeither/http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (
"bytes"
"io"
"net/http"
"net/url"

FL "github.com/IBM/fp-go/file"
F "github.com/IBM/fp-go/function"
IOE "github.com/IBM/fp-go/ioeither"
IOEH "github.com/IBM/fp-go/ioeither/http"
J "github.com/IBM/fp-go/json"
LZ "github.com/IBM/fp-go/lazy"
L "github.com/IBM/fp-go/optics/lens"
O "github.com/IBM/fp-go/option"
Expand Down Expand Up @@ -217,3 +219,30 @@ func WithHeader(name string) func(value string) BuilderBuilder {
func WithoutHeader(name string) BuilderBuilder {
return Header(name).Set(noHeader)
}

// WithFormData creates a [BuilderBuilder] to send form data payload
func WithFormData(value url.Values) BuilderBuilder {
return F.Flow2(
F.Pipe4(
value,
url.Values.Encode,
S.ToBytes,
IOE.Of[error, []byte],
WithBody,
),
WithContentType("application/x-www-form-urlencoded"),
)
}

// WithJson creates a [BuilderBuilder] to send JSON payload
func WithJson[T any](data T) BuilderBuilder {
return F.Flow2(
F.Pipe3(
data,
J.Marshal[T],
IOE.FromEither[error, []byte],
WithBody,
),
WithContentType("application/json"),
)
}

0 comments on commit 9668642

Please sign in to comment.