mirror of
https://github.com/IBM/fp-go.git
synced 2025-11-23 22:14:53 +02:00
* fix: initial checkin of v2 Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: slowly migrate IO Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: migrate MonadTraverseArray and TraverseArray Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: migrate traversal Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: complete migration of IO Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: migrate ioeither Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: refactorY Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: next step in migration Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: adjust IO generation code Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: get rid of more IO methods Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: get rid of more IO * fix: convert iooption Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: convert reader Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: convert a bit of reader Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: new build script Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: cleanup Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: reformat Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: simplify Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: some cleanup Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: adjust Pair to Haskell semantic Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: documentation and testcases Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: some performance optimizations Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: remove coverage Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: better doc Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> --------- Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
146 lines
4.1 KiB
Go
146 lines
4.1 KiB
Go
// Copyright (c) 2023 - 2025 IBM Corp.
|
|
// All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
|
|
B "github.com/IBM/fp-go/v2/bytes"
|
|
FL "github.com/IBM/fp-go/v2/file"
|
|
F "github.com/IBM/fp-go/v2/function"
|
|
H "github.com/IBM/fp-go/v2/http"
|
|
"github.com/IBM/fp-go/v2/ioeither"
|
|
IOEF "github.com/IBM/fp-go/v2/ioeither/file"
|
|
J "github.com/IBM/fp-go/v2/json"
|
|
P "github.com/IBM/fp-go/v2/pair"
|
|
)
|
|
|
|
type (
|
|
// Requester is a reader that constructs a request
|
|
Requester = ioeither.IOEither[error, *http.Request]
|
|
|
|
Client interface {
|
|
Do(Requester) ioeither.IOEither[error, *http.Response]
|
|
}
|
|
|
|
client struct {
|
|
delegate *http.Client
|
|
doIOE func(*http.Request) ioeither.IOEither[error, *http.Response]
|
|
}
|
|
)
|
|
|
|
var (
|
|
// MakeRequest is an eitherized version of [http.NewRequest]
|
|
MakeRequest = ioeither.Eitherize3(http.NewRequest)
|
|
makeRequest = F.Bind13of3(MakeRequest)
|
|
|
|
// specialize
|
|
MakeGetRequest = makeRequest("GET", nil)
|
|
)
|
|
|
|
// MakeBodyRequest creates a request that carries a body
|
|
func MakeBodyRequest(method string, body ioeither.IOEither[error, []byte]) func(url string) ioeither.IOEither[error, *http.Request] {
|
|
onBody := F.Pipe1(
|
|
body,
|
|
ioeither.Map[error](F.Flow2(
|
|
bytes.NewReader,
|
|
FL.ToReader[*bytes.Reader],
|
|
)),
|
|
)
|
|
onRelease := ioeither.Of[error, io.Reader]
|
|
withMethod := F.Bind1of3(MakeRequest)(method)
|
|
|
|
return F.Flow2(
|
|
F.Bind1of2(withMethod),
|
|
ioeither.WithResource[*http.Request](onBody, onRelease),
|
|
)
|
|
}
|
|
|
|
func (client client) Do(req Requester) ioeither.IOEither[error, *http.Response] {
|
|
return F.Pipe1(
|
|
req,
|
|
ioeither.Chain(client.doIOE),
|
|
)
|
|
}
|
|
|
|
func MakeClient(httpClient *http.Client) Client {
|
|
return client{delegate: httpClient, doIOE: ioeither.Eitherize1(httpClient.Do)}
|
|
}
|
|
|
|
// ReadFullResponse sends a request, reads the response as a byte array and represents the result as a tuple
|
|
func ReadFullResponse(client Client) func(Requester) ioeither.IOEither[error, H.FullResponse] {
|
|
return F.Flow3(
|
|
client.Do,
|
|
ioeither.ChainEitherK(H.ValidateResponse),
|
|
ioeither.Chain(func(resp *http.Response) ioeither.IOEither[error, H.FullResponse] {
|
|
return F.Pipe1(
|
|
F.Pipe3(
|
|
resp,
|
|
H.GetBody,
|
|
ioeither.Of[error, io.ReadCloser],
|
|
IOEF.ReadAll[io.ReadCloser],
|
|
),
|
|
ioeither.Map[error](F.Bind1st(P.MakePair[*http.Response, []byte], resp)),
|
|
)
|
|
}),
|
|
)
|
|
}
|
|
|
|
// ReadAll sends a request and reads the response as bytes
|
|
func ReadAll(client Client) func(Requester) ioeither.IOEither[error, []byte] {
|
|
return F.Flow2(
|
|
ReadFullResponse(client),
|
|
ioeither.Map[error](H.Body),
|
|
)
|
|
}
|
|
|
|
// ReadText sends a request, reads the response and represents the response as a text string
|
|
func ReadText(client Client) func(Requester) ioeither.IOEither[error, string] {
|
|
return F.Flow2(
|
|
ReadAll(client),
|
|
ioeither.Map[error](B.ToString),
|
|
)
|
|
}
|
|
|
|
// ReadJson sends a request, reads the response and parses the response as JSON
|
|
//
|
|
// Deprecated: use [ReadJSON] instead
|
|
func ReadJson[A any](client Client) func(Requester) ioeither.IOEither[error, A] {
|
|
return ReadJSON[A](client)
|
|
}
|
|
|
|
// readJSON sends a request, reads the response and parses the response as a []byte
|
|
func readJSON(client Client) func(Requester) ioeither.IOEither[error, []byte] {
|
|
return F.Flow3(
|
|
ReadFullResponse(client),
|
|
ioeither.ChainFirstEitherK(F.Flow2(
|
|
H.Response,
|
|
H.ValidateJSONResponse,
|
|
)),
|
|
ioeither.Map[error](H.Body),
|
|
)
|
|
}
|
|
|
|
// ReadJSON sends a request, reads the response and parses the response as JSON
|
|
func ReadJSON[A any](client Client) func(Requester) ioeither.IOEither[error, A] {
|
|
return F.Flow2(
|
|
readJSON(client),
|
|
ioeither.ChainEitherK[error](J.Unmarshal[A]),
|
|
)
|
|
}
|