@@ -1271,16 +1271,29 @@ sub invoke_api {
12711271 if ($use_paginator) {
12721272 my $result = $self->decode_response;
12731273
1274+ DEBUG(
1275+ sub {
1276+ return Dumper(
1277+ [ paginator => $paginator,
1278+ paged_results => @paged_results,
1279+ result => $result
1280+ ]
1281+ );
1282+ }
1283+ );
1284+
1285+ my $actual_result = dig( $result, $paginator, 'result_key' );
1286+
12741287 return \@paged_results
1275- if !$result->{ $paginator->{result_key} } ;
1288+ if !$actual_result ;
12761289
12771290 DEBUG(
12781291 sub {
12791292 return Dumper( [ result => $result, ] );
12801293 }
12811294 );
12821295
1283- push @paged_results, @{ $result->{ $paginator->{result_key} } };
1296+ push @paged_results, @{$actual_result };
12841297
12851298 DEBUG(
12861299 sub {
@@ -1293,9 +1306,8 @@ sub invoke_api {
12931306 }
12941307 );
12951308
1296- if ( !$result->{ $paginator->{more_results} } ) {
1297- delete $result->{ $paginator->{more_results} };
1298-
1309+ if ( !dig( $result, $paginator, 'more_results' ) ) {
1310+ # might have to dig here too, but generally I don't think so
12991311 if ( $paginator->{limit_key} ) {
13001312 delete $result->{ $paginator->{limit_key} };
13011313 }
@@ -1327,9 +1339,8 @@ sub invoke_api {
13271339 }
13281340 }
13291341
1330- if ($use_paginator) {
1331- return { $paginator->{result_key} => \@paged_results };
1332- }
1342+ return bury( \@paged_results, $paginator, 'result_key' )
1343+ if $use_paginator;
13331344
13341345 my $results = $self->decode_response;
13351346
@@ -1343,15 +1354,74 @@ sub invoke_api {
13431354 # 'use_paginator' is tested above and if it is false (you told me not to use the
13441355 # paginator) we return the results...
13451356
1346- if ( !$results->{ $paginator->{more_results} } ) {
1347- delete $results->{ $paginator->{more_results} };
1357+ if ( !dig( $results, $paginator, 'more_results' ) ) {
13481358 delete $results->{ $paginator->{input_token} };
13491359 delete $results->{ $paginator->{limit_key} };
13501360 }
13511361
13521362 return $results;
13531363}
13541364
1365+ ########################################################################
1366+ # the analog to dig, we need to set the result to a hash element specified
1367+ # by a dot encoded string. Example: DistributionList.Item
1368+ ########################################################################
1369+ sub bury {
1370+ ########################################################################
1371+ my ( $result, $paginator, $key ) = @_;
1372+
1373+ return $result
1374+ if !$paginator;
1375+
1376+ $key = $paginator->{$key};
1377+
1378+ return { $key => $result }
1379+ if $key !~ /[.]/xsm;
1380+
1381+ my $parent = {};
1382+ my $child = $parent;
1383+
1384+ my @keys = split /[.]/xsm, $key;
1385+ my $result_key = pop @keys; # remove last element
1386+
1387+ foreach (@keys) {
1388+ $child->{$_} = {};
1389+ $child = $child->{$_};
1390+ }
1391+
1392+ $child->{$result_key} = $result;
1393+
1394+ return $parent;
1395+ }
1396+
1397+ ########################################################################
1398+ # follow the . encoded key to find the hash element
1399+ # example: DistributionList.Items
1400+ ########################################################################
1401+ sub dig {
1402+ ########################################################################
1403+ my ( $result, $paginator, $key, $delete ) = @_;
1404+
1405+ return $result
1406+ if !$paginator || !$paginator->{$key};
1407+
1408+ $key = $paginator->{$key};
1409+
1410+ return $result->{$key}
1411+ if $key !~ /[.]/xsm;
1412+
1413+ foreach ( split /[.]/xsm, $key ) {
1414+ $result = $result->{$_};
1415+ $key = $_;
1416+ }
1417+
1418+ if ($delete) {
1419+ delete $result->{$key};
1420+ }
1421+
1422+ return $result;
1423+ }
1424+
13551425########################################################################
13561426sub print_error {
13571427########################################################################
0 commit comments