From cb2875bf1e3d99eb6895635710fb55109ff51de4 Mon Sep 17 00:00:00 2001 From: "Dr. Carsten Leue" Date: Thu, 20 Jul 2023 11:48:01 +0200 Subject: [PATCH] doc: add sample for http Signed-off-by: Dr. Carsten Leue --- samples/http/http_test.go | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 samples/http/http_test.go diff --git a/samples/http/http_test.go b/samples/http/http_test.go new file mode 100644 index 0000000..52a94ab --- /dev/null +++ b/samples/http/http_test.go @@ -0,0 +1,52 @@ +package http + +import ( + "context" + "fmt" + "testing" + + HTTP "net/http" + + A "github.com/IBM/fp-go/array" + R "github.com/IBM/fp-go/context/readerioeither" + H "github.com/IBM/fp-go/context/readerioeither/http" + E "github.com/IBM/fp-go/either" + F "github.com/IBM/fp-go/function" + IO "github.com/IBM/fp-go/io" + "github.com/stretchr/testify/assert" +) + +type PostItem struct { + UserId uint `json:"userId"` + Id uint `json:"id"` + Title string `json:"title"` + Body string `json:"body"` +} + +func idxToUrl(idx int) string { + return fmt.Sprintf("https://jsonplaceholder.typicode.com/posts/%d", idx+1) +} + +func TestMultipleHttpRequests(t *testing.T) { + // prepare the http client + client := H.MakeClient(HTTP.DefaultClient) + readSinglePost := H.ReadJson[PostItem](client) + + // total number of http requests + count := 10 + + data := F.Pipe3( + A.MakeBy(count, idxToUrl), + R.TraverseArray(F.Flow3( + H.MakeGetRequest, + readSinglePost, + R.ChainFirstIOK(IO.Logf[PostItem]("Log Single: %v")), + )), + R.ChainFirstIOK(IO.Logf[[]PostItem]("Log Result: %v")), + R.Map(A.Size[PostItem]), + ) + + result := data(context.Background()) + + assert.Equal(t, E.Of[error](count), result()) +}