-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from IBM/cleue-http-example
doc: add sample for http
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |