TT#10964 paginated retrieval of /api/customerbalances

+ filter by "prepaid"

Change-Id: Ifb84124353b353ac9d84423b497201d91159b617
changes/17/14917/3
Rene Krenn 8 years ago
parent 26d232437e
commit 9f7800ab20

@ -95,15 +95,27 @@ EOF
} }
sub get_data { sub get_data {
my ($uri, $link) = @_; my ($uri, $link, $process_code) = @_;
$process_code = sub { my $data = shift; return 0; } unless 'CODE' eq ref $process_code;
my $client = new NGCP::API::Client; my $client = new NGCP::API::Client;
$client->set_verbose($opts->{verbose}); $client->set_verbose($opts->{verbose});
my $res = $client->request("GET", $uri); $client->set_page_rows(10);
die $res->result unless $res->is_success; my @result = ();
my $res_hash = $res->as_hash; while (my $res = $client->next_page($uri)) {
return [] unless $res_hash->{total_count} && $res_hash->{total_count} > 0; die $res->result unless $res->is_success;
my $data = $res_hash->{_embedded}{'ngcp:'.$link}; my $res_hash = $res->as_hash;
return ref $data eq 'ARRAY' ? $data : [ $data ]; my $data = $res_hash->{_embedded}{'ngcp:'.$link};
if ('ARRAY' eq ref $data) {
unless (&$process_code($data)) {
push(@result,@$data);
}
} elsif ($data) {
unless (&$process_code([$data])) {
push(@result,$data);
}
}
}
return \@result;
} }
sub get_email_template { sub get_email_template {
@ -129,15 +141,19 @@ sub main {
die "Missing domain in a credit warning check"; die "Missing domain in a credit warning check";
} }
my @contracts; my @contracts;
my $balances = get_data(sprintf('/api/customerbalances/?domain=%s&prepaid=1',
get_data(sprintf('/api/customerbalances/?domain=%s', $cwarning->{domain}),
$cwarning->{domain}), 'customerbalances',
'customerbalances'); sub {
foreach my $balance (@{$balances}) { my $balances = shift;
next if $balance->{cash_balance} >= $cwarning->{threshold}; foreach my $balance (@{$balances}) {
push @contracts, next if $balance->{cash_balance} >= $cwarning->{threshold};
{ map { $_ => $balance->{$_} } qw(id cash_balance) }; push @contracts,
} { map { $_ => $balance->{$_} } qw(id cash_balance) };
}
return 1;
}
);
if (@contracts) { if (@contracts) {
eval { eval {
send_email($cwarning, \@contracts); send_email($cwarning, \@contracts);
@ -214,5 +230,3 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut

Loading…
Cancel
Save