@@ -320,18 +320,18 @@ protected function retrieveData(string $endpoint, array $params = []): array
320
320
321
321
$ returnData = [];
322
322
323
- $ limit = $ params ['limit ' ];
323
+ $ requestedLimit = $ remaininglimit = $ params ['limit ' ];
324
324
$ offset = $ params ['offset ' ];
325
325
326
- while ($ limit > 0 ) {
327
- if ($ limit > 100 ) {
328
- $ _limit = 100 ;
329
- $ limit -= 100 ;
326
+ while ($ remaininglimit > 0 ) {
327
+ if ($ remaininglimit > 100 ) {
328
+ $ realLimit = 100 ;
329
+ $ remaininglimit -= 100 ;
330
330
} else {
331
- $ _limit = $ limit ;
332
- $ limit = 0 ;
331
+ $ realLimit = $ remaininglimit ;
332
+ $ remaininglimit = 0 ;
333
333
}
334
- $ params ['limit ' ] = $ _limit ;
334
+ $ params ['limit ' ] = $ realLimit ;
335
335
$ params ['offset ' ] = $ offset ;
336
336
337
337
$ this ->lastResponse = $ this ->getHttpClient ()->request (HttpFactory::makeRequest (
@@ -344,16 +344,34 @@ protected function retrieveData(string $endpoint, array $params = []): array
344
344
345
345
$ returnData = array_merge_recursive ($ returnData , $ newDataSet );
346
346
347
- $ offset += $ _limit ;
347
+ // After the first request we know the total_count for this endpoint
348
+ // so lets use the total_count to correct $requestedLimit to save us
349
+ // from making unnecessary requests
350
+ // e.g. total_count = 5; $requestedLimit = 500 will make only 1 request instead of 5
351
+ if (isset ($ newDataSet ['total_count ' ]) && $ newDataSet ['total_count ' ] < $ requestedLimit ) {
352
+ $ requestedLimit = $ remaininglimit = (int ) $ newDataSet ['total_count ' ];
353
+
354
+ if ($ remaininglimit > 100 ) {
355
+ $ realLimit = 100 ;
356
+ $ remaininglimit -= 100 ;
357
+ } else {
358
+ $ realLimit = $ remaininglimit ;
359
+ $ remaininglimit = 0 ;
360
+ }
361
+ }
362
+
363
+ $ offset += $ realLimit ;
348
364
349
365
if (
350
- empty ($ newDataSet ) || !isset ($ newDataSet ['limit ' ]) || (
351
- isset ($ newDataSet ['offset ' ]) &&
352
- isset ($ newDataSet ['total_count ' ]) &&
353
- $ newDataSet ['offset ' ] >= $ newDataSet ['total_count ' ]
366
+ empty ($ newDataSet )
367
+ || !isset ($ newDataSet ['limit ' ])
368
+ || (
369
+ isset ($ newDataSet ['offset ' ])
370
+ && isset ($ newDataSet ['total_count ' ])
371
+ && $ newDataSet ['offset ' ] >= $ newDataSet ['total_count ' ]
354
372
)
355
373
) {
356
- $ limit = 0 ;
374
+ $ remaininglimit = 0 ;
357
375
}
358
376
}
359
377
0 commit comments