Skip to content

Commit cefa444

Browse files
committed
Added testing and examples for products lister
1 parent 74aa30d commit cefa444

File tree

4 files changed

+331
-80
lines changed

4 files changed

+331
-80
lines changed

examples/products/main.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"github.com/erply/api-go-wrapper/pkg/api"
88
"github.com/erply/api-go-wrapper/pkg/api/auth"
9+
sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common"
910
"github.com/erply/api-go-wrapper/pkg/api/products"
1011
"net/http"
1112
"time"
@@ -32,7 +33,13 @@ func main() {
3233
panic(err)
3334
}
3435

35-
fmt.Printf("%+v\n", prods)
36+
fmt.Printf("GetProductsBulk:\n%+v\n", prods)
37+
38+
prods, err = GetProductsInParallel(apiClient)
39+
if err != nil {
40+
panic(err)
41+
}
42+
fmt.Printf("GetProductsInParallel:\n%+v\n", prods)
3643
}
3744

3845
func GetProductsBulk(cl *api.Client) (prods []products.Product, err error) {
@@ -65,3 +72,42 @@ func GetProductsBulk(cl *api.Client) (prods []products.Product, err error) {
6572

6673
return
6774
}
75+
76+
func GetProductsInParallel(cl *api.Client) ([]products.Product, error) {
77+
productsDataProvider := products.NewListingDataProvider(cl.ProductManager)
78+
79+
lister := sharedCommon.NewLister(
80+
sharedCommon.ListingSettings{
81+
MaxRequestsCountPerSecond: 5,
82+
StreamBufferLength: 10,
83+
MaxItemsPerRequest: 10,
84+
MaxFetchersCount: 2,
85+
},
86+
productsDataProvider,
87+
func(sleepTime time.Duration) {
88+
time.Sleep(sleepTime)
89+
},
90+
)
91+
92+
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 5)
93+
defer cancel()
94+
95+
prodsChan := lister.Get(ctx, map[string]interface{}{})
96+
97+
prods := make([]products.Product, 0)
98+
var err error
99+
doneChan := make(chan struct{}, 1)
100+
go func() {
101+
defer close(doneChan)
102+
for prod := range prodsChan {
103+
if prod.Err != nil {
104+
err = prod.Err
105+
return
106+
}
107+
prods = append(prods, prod.Payload.(products.Product))
108+
}
109+
}()
110+
111+
<-doneChan
112+
return prods, err
113+
}

pkg/api/common/listing.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type ItemsStream chan Item
2626
type Item struct {
2727
Err error
2828
TotalCount int
29-
ProgressCount int
3029
Payload interface{}
3130
}
3231

@@ -35,8 +34,8 @@ func setListingSettingsDefaults(settingsFromInput ListingSettings) ListingSettin
3534
settingsFromInput.MaxRequestsCountPerSecond = DefaultMaxRequestsCountPerSecond
3635
}
3736

38-
if settingsFromInput.MaxItemsPerRequest == 0 || settingsFromInput.MaxItemsPerRequest > MaxCountPerBulkRequestItem {
39-
settingsFromInput.MaxItemsPerRequest = MaxCountPerBulkRequestItem
37+
if settingsFromInput.MaxItemsPerRequest == 0 || settingsFromInput.MaxItemsPerRequest > MaxCountPerBulkRequestItem * MaxCountPerBulkRequestItem {
38+
settingsFromInput.MaxItemsPerRequest = MaxCountPerBulkRequestItem * MaxCountPerBulkRequestItem
4039
}
4140

4241
if settingsFromInput.MaxFetchersCount == 0 {
@@ -131,25 +130,21 @@ func (p *Lister) getCursors(ctx context.Context, totalCount int) chan []Cursor {
131130
p.listingSettings.MaxItemsPerRequest = MaxCountPerBulkRequestItem*MaxBulkRequestsCount
132131
}
133132

134-
for ; leftCount > 0; { //leftCount 1000, p.listingSettings.MaxItemsPerRequest 100
133+
for ; leftCount > 0; {
135134
countToFetchForBulkRequest := leftCount
136135
if leftCount > p.listingSettings.MaxItemsPerRequest {
137136
countToFetchForBulkRequest = p.listingSettings.MaxItemsPerRequest
138137
}
139138

140-
//countToFetchForBulkRequest 100
141-
142139
bulkItemsCount := CeilDivisionInt(countToFetchForBulkRequest, MaxCountPerBulkRequestItem)
143140
if bulkItemsCount > MaxBulkRequestsCount {
144141
bulkItemsCount = MaxBulkRequestsCount
145142
}
146-
//bulkItemsCount 1
147143

148144
limit := CeilDivisionInt(p.listingSettings.MaxItemsPerRequest, bulkItemsCount)
149145
if limit > MaxCountPerBulkRequestItem {
150146
limit = MaxCountPerBulkRequestItem
151147
}
152-
//limit 100
153148

154149
cursorsForBulkRequest := make([]Cursor, 0, bulkItemsCount)
155150
for i := 0; i < bulkItemsCount; i++ {
@@ -244,8 +239,3 @@ func (p *Lister) mergeChannels(ctx context.Context, childChans ...ItemsStream) I
244239
func CeilDivisionInt(x, y int) int {
245240
return int(math.Ceil(float64(x) / float64(y)))
246241
}
247-
248-
func AddDefaultPaginationOption(filters map[string]interface{}, limit, page int) {
249-
filters["recordsOnPage"] = limit
250-
filters["pageNo"] = page
251-
}

0 commit comments

Comments
 (0)