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

fix: add ioeither

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-07-14 17:30:58 +02:00
parent e350f70659
commit 5020437b6a
80 changed files with 5436 additions and 2110 deletions

58
ioeither/http/request.go Normal file
View File

@@ -0,0 +1,58 @@
package http
import (
"io"
"net/http"
B "github.com/ibm/fp-go/bytes"
ER "github.com/ibm/fp-go/errors"
F "github.com/ibm/fp-go/function"
IOE "github.com/ibm/fp-go/ioeither"
IOEF "github.com/ibm/fp-go/ioeither/file"
J "github.com/ibm/fp-go/json"
)
type Client interface {
Do(req *http.Request) IOE.IOEither[error, *http.Response]
}
type client struct {
delegate *http.Client
}
func (client client) Do(req *http.Request) IOE.IOEither[error, *http.Response] {
return IOE.TryCatch(func() (*http.Response, error) {
return client.delegate.Do(req)
}, ER.IdentityError)
}
func MakeClient(httpClient *http.Client) Client {
return client{delegate: httpClient}
}
func ReadAll(client Client) func(*http.Request) IOE.IOEither[error, []byte] {
return func(req *http.Request) IOE.IOEither[error, []byte] {
return IOEF.ReadAll(F.Pipe2(
req,
client.Do,
IOE.Map[error](func(resp *http.Response) io.ReadCloser {
return resp.Body
}),
),
)
}
}
func ReadText(client Client) func(*http.Request) IOE.IOEither[error, string] {
return F.Flow2(
ReadAll(client),
IOE.Map[error](B.ToString),
)
}
func ReadJson[A any](client Client) func(*http.Request) IOE.IOEither[error, A] {
return F.Flow2(
ReadAll(client),
IOE.ChainEitherK(J.Unmarshal[A]),
)
}