connect to server api on request
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 11 May 2019 14:51:36 +0000 (16:51 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 11 May 2019 14:51:36 +0000 (16:51 +0200)
Limit access to blizget() calls; delay setup to first actual usage.

getsc2clan

index 3a6e2375465f1d3ffba41dff85a5b7fee317a5b7..b1a5ebdcc25085ad749d13b0ba9acbdd2a5ee048 100755 (executable)
@@ -13,14 +13,17 @@ my ($profiles, $clanmatches) = part { /\D/ } @ARGV;  # separate numbers
        or die "Usage: $0 <profile id>... [<clan name>...]\n";
 my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches};
 
-my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n";
-my $bliz = LWP::Authen::OAuth2->new(%auth,
-       token_endpoint          => 'https://eu.battle.net/oauth/token',
-       request_required_params => [qw( client_id client_secret grant_type )],
-);
-$bliz->request_tokens(grant_type => 'client_credentials');
-
 sub blizget {
+       state $bliz = do {
+               my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n";
+               my $bliz = LWP::Authen::OAuth2->new(%auth,
+                       token_endpoint          => 'https://eu.battle.net/oauth/token',
+                       request_required_params => [qw( client_id client_secret grant_type )],
+               );
+               $bliz->request_tokens(grant_type => 'client_credentials');
+               $bliz;
+       };
+
        my $args = join('/', @_);
        my $res = $bliz->get("https://eu.api.blizzard.com/sc2/$args");
        $res->is_success or die $res->status_line;