mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: http interface of IOEither
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@@ -275,6 +275,27 @@ func ChainFirstIOK[GA ~func() ET.Either[E, A], GIOB ~func() B, E, A, B any](f fu
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MonadChainFirstEitherK runs the monad returned by the function but returns the result of the original monad
|
||||||
|
func MonadChainFirstEitherK[GA ~func() ET.Either[E, A], E, A, B any](first GA, f func(A) ET.Either[E, B]) GA {
|
||||||
|
return FE.MonadChainFirstEitherK(
|
||||||
|
MonadChain[GA, GA, E, A, A],
|
||||||
|
MonadMap[func() ET.Either[E, B], GA, E, B, A],
|
||||||
|
FromEither[func() ET.Either[E, B], E, B],
|
||||||
|
first,
|
||||||
|
f,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChainFirstEitherK runs the monad returned by the function but returns the result of the original monad
|
||||||
|
func ChainFirstEitherK[GA ~func() ET.Either[E, A], E, A, B any](f func(A) ET.Either[E, B]) func(GA) GA {
|
||||||
|
return FE.ChainFirstEitherK(
|
||||||
|
MonadChain[GA, GA, E, A, A],
|
||||||
|
MonadMap[func() ET.Either[E, B], GA, E, B, A],
|
||||||
|
FromEither[func() ET.Either[E, B], E, B],
|
||||||
|
f,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Swap changes the order of type parameters
|
// Swap changes the order of type parameters
|
||||||
func Swap[GEA ~func() ET.Either[E, A], GAE ~func() ET.Either[A, E], E, A any](val GEA) GAE {
|
func Swap[GEA ~func() ET.Either[E, A], GAE ~func() ET.Either[A, E], E, A any](val GEA) GAE {
|
||||||
return MonadFold(val, Right[GAE], Left[GAE])
|
return MonadFold(val, Right[GAE], Left[GAE])
|
||||||
|
@@ -20,54 +20,94 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
B "github.com/IBM/fp-go/bytes"
|
B "github.com/IBM/fp-go/bytes"
|
||||||
ER "github.com/IBM/fp-go/errors"
|
|
||||||
F "github.com/IBM/fp-go/function"
|
F "github.com/IBM/fp-go/function"
|
||||||
|
H "github.com/IBM/fp-go/http"
|
||||||
IOE "github.com/IBM/fp-go/ioeither"
|
IOE "github.com/IBM/fp-go/ioeither"
|
||||||
IOEF "github.com/IBM/fp-go/ioeither/file"
|
IOEF "github.com/IBM/fp-go/ioeither/file"
|
||||||
J "github.com/IBM/fp-go/json"
|
J "github.com/IBM/fp-go/json"
|
||||||
|
T "github.com/IBM/fp-go/tuple"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client interface {
|
type (
|
||||||
Do(req *http.Request) IOE.IOEither[error, *http.Response]
|
// Requester is a reader that constructs a request
|
||||||
|
Requester = IOE.IOEither[error, *http.Request]
|
||||||
|
|
||||||
|
Client interface {
|
||||||
|
Do(Requester) IOE.IOEither[error, *http.Response]
|
||||||
}
|
}
|
||||||
|
|
||||||
type client struct {
|
client struct {
|
||||||
delegate *http.Client
|
delegate *http.Client
|
||||||
|
doIOE func(*http.Request) IOE.IOEither[error, *http.Response]
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func (client client) Do(req *http.Request) IOE.IOEither[error, *http.Response] {
|
var (
|
||||||
return IOE.TryCatch(func() (*http.Response, error) {
|
// MakeRequest is an eitherized version of [http.NewRequest]
|
||||||
return client.delegate.Do(req)
|
MakeRequest = IOE.Eitherize3(http.NewRequest)
|
||||||
}, ER.IdentityError)
|
makeRequest = F.Bind13of3(MakeRequest)
|
||||||
|
|
||||||
|
// specialize
|
||||||
|
MakeGetRequest = makeRequest("GET", nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (client client) Do(req Requester) IOE.IOEither[error, *http.Response] {
|
||||||
|
return F.Pipe1(
|
||||||
|
req,
|
||||||
|
IOE.Chain(client.doIOE),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeClient(httpClient *http.Client) Client {
|
func MakeClient(httpClient *http.Client) Client {
|
||||||
return client{delegate: httpClient}
|
return client{delegate: httpClient, doIOE: IOE.Eitherize1(httpClient.Do)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadAll(client Client) func(*http.Request) IOE.IOEither[error, []byte] {
|
// ReadFullResponse sends a request, reads the response as a byte array and represents the result as a tuple
|
||||||
return func(req *http.Request) IOE.IOEither[error, []byte] {
|
func ReadFullResponse(client Client) func(Requester) IOE.IOEither[error, H.FullResponse] {
|
||||||
return IOEF.ReadAll(F.Pipe2(
|
return F.Flow3(
|
||||||
req,
|
|
||||||
client.Do,
|
client.Do,
|
||||||
IOE.Map[error](func(resp *http.Response) io.ReadCloser {
|
IOE.ChainEitherK(H.ValidateResponse),
|
||||||
return resp.Body
|
IOE.Chain(func(resp *http.Response) IOE.IOEither[error, H.FullResponse] {
|
||||||
}),
|
return F.Pipe1(
|
||||||
|
F.Pipe3(
|
||||||
|
resp,
|
||||||
|
H.GetBody,
|
||||||
|
IOE.Of[error, io.ReadCloser],
|
||||||
|
IOEF.ReadAll[io.ReadCloser],
|
||||||
),
|
),
|
||||||
|
IOE.Map[error](F.Bind1st(T.MakeTuple2[*http.Response, []byte], resp)),
|
||||||
|
)
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAll sends a request and reads the response as bytes
|
||||||
|
func ReadAll(client Client) func(Requester) IOE.IOEither[error, []byte] {
|
||||||
|
return F.Flow2(
|
||||||
|
ReadFullResponse(client),
|
||||||
|
IOE.Map[error](H.Body),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadText(client Client) func(*http.Request) IOE.IOEither[error, string] {
|
// ReadText sends a request, reads the response and represents the response as a text string
|
||||||
|
func ReadText(client Client) func(Requester) IOE.IOEither[error, string] {
|
||||||
return F.Flow2(
|
return F.Flow2(
|
||||||
ReadAll(client),
|
ReadAll(client),
|
||||||
IOE.Map[error](B.ToString),
|
IOE.Map[error](B.ToString),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadJson[A any](client Client) func(*http.Request) IOE.IOEither[error, A] {
|
// ReadJson sends a request, reads the response and parses the response as JSON
|
||||||
return F.Flow2(
|
func ReadJson[A any](client Client) func(Requester) IOE.IOEither[error, A] {
|
||||||
ReadAll(client),
|
return F.Flow3(
|
||||||
IOE.ChainEitherK(J.Unmarshal[A]),
|
ReadFullResponse(client),
|
||||||
|
IOE.ChainFirstEitherK(F.Flow2(
|
||||||
|
H.Response,
|
||||||
|
H.ValidateJsonResponse,
|
||||||
|
)),
|
||||||
|
IOE.ChainEitherK(F.Flow2(
|
||||||
|
H.Body,
|
||||||
|
J.Unmarshal[A],
|
||||||
|
)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -23,7 +23,6 @@ import (
|
|||||||
|
|
||||||
AR "github.com/IBM/fp-go/array"
|
AR "github.com/IBM/fp-go/array"
|
||||||
E "github.com/IBM/fp-go/either"
|
E "github.com/IBM/fp-go/either"
|
||||||
HE "github.com/IBM/fp-go/either/http"
|
|
||||||
"github.com/IBM/fp-go/errors"
|
"github.com/IBM/fp-go/errors"
|
||||||
F "github.com/IBM/fp-go/function"
|
F "github.com/IBM/fp-go/function"
|
||||||
IOE "github.com/IBM/fp-go/ioeither"
|
IOE "github.com/IBM/fp-go/ioeither"
|
||||||
@@ -53,10 +52,9 @@ func TestRetryHttp(t *testing.T) {
|
|||||||
client := MakeClient(&http.Client{})
|
client := MakeClient(&http.Client{})
|
||||||
|
|
||||||
action := func(status R.RetryStatus) IOE.IOEither[error, *PostItem] {
|
action := func(status R.RetryStatus) IOE.IOEither[error, *PostItem] {
|
||||||
return F.Pipe2(
|
return F.Pipe1(
|
||||||
HE.GetRequest(urls[status.IterNumber]),
|
MakeGetRequest(urls[status.IterNumber]),
|
||||||
IOE.FromEither[error, *http.Request],
|
ReadJson[*PostItem](client),
|
||||||
IOE.Chain(ReadJson[*PostItem](client)),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -211,6 +211,14 @@ func ChainFirst[E, A, B any](f func(A) IOEither[E, B]) func(IOEither[E, A]) IOEi
|
|||||||
return G.ChainFirst[IOEither[E, A]](f)
|
return G.ChainFirst[IOEither[E, A]](f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MonadChainFirstEitherK[A, E, B any](ma IOEither[E, A], f func(A) ET.Either[E, B]) IOEither[E, A] {
|
||||||
|
return G.MonadChainFirstEitherK[IOEither[E, A]](ma, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChainFirstEitherK[A, E, B any](f func(A) ET.Either[E, B]) func(ma IOEither[E, A]) IOEither[E, A] {
|
||||||
|
return G.ChainFirstEitherK[IOEither[E, A]](f)
|
||||||
|
}
|
||||||
|
|
||||||
// MonadChainFirstIOK runs [IO] the monad returned by the function but returns the result of the original monad
|
// MonadChainFirstIOK runs [IO] the monad returned by the function but returns the result of the original monad
|
||||||
func MonadChainFirstIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, A] {
|
func MonadChainFirstIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, A] {
|
||||||
return G.MonadChainFirstIOK(ma, f)
|
return G.MonadChainFirstIOK(ma, f)
|
||||||
|
Reference in New Issue
Block a user