The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WebService::APIKeys::AutoChanger - Throttler and auto-changer for Web-API's restriction.

SYNOPSIS

For example, use it with WebService::Simple for YahooJapan Web-API.

  use WebService::Simple;
  use WebService::APIKeys::AutoChanger;
  use Data::Dumper;
  
  my $changer = WebService::APIKeys::AutoChanger->new;
  
  $changer->set(
      api_keys => [
          'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
          'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
          'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz',
      ],
      ## you don't have to set below explicitly
      throttle_class => 'Data::Valve', # backend throttler class. (default: Data::Valve)
      throttle_conf  => { 
          max_items  => 50000,         # default: 50000
          interval   => 86400,         # default: 86400
      },
  );
  
  my $yahooJapan = WebService::Simple->new(
      base_url => 'http://search.yahooapis.jp/WebSearchService/V1/webSearch',
  );
  
  my @query = qw( foo bar baz ... );
  for my $query(@query){
      # available APIKey(appid) will be changed automatically
      my $available_key = $changer->get_available;
      # you can use it
      my $response = $yahooJapan->get( {
          appid => $available_key,
          query => $query } );
      do_something($response);
  }

DESCRIPTION

WebService::APIKeys::AutoChanger is a throttler and auto-changer for Web API's restriction.

For example, YahooJapan sets a restriction 50,000 times per day as an API-key. You can use it over the limitation if you set plural APIKey beforehand.

This module provides available-key, seamlessly.

METHODS

new([%args])
    my $changer = WebService::APIKeys::AutoChanger->new(
        api_keys => \@api_keys,
        throttle_class => 'Data::Valve',
        throttle_config => {
            max_items  => 50000,
            interval   => 86400,
        } 
    );

Create and return new WebService::APIKeys::AutoChanger object. You can set api-keys and throttle information as option parameters.

set(%api_keys, [%throttle_class], [%throttle_config]);
    $changer->set(
        api_keys => \@api_keys,
        throttle_class => 'Data::Valve',
        throttle_config => {
            max_items  => 50000,
            interval   => 86400,
        } 
    );

If your circumstance does not allow use Moose, set 'Data::Throttler' instead of 'Data::Valve' for throttle_class. (But Data::Throttler is slower than Data::Valve)

get_available

Returns an available API Key at that point.

set_api_keys(\@api_keys)
set_throttle(%args)

AUTHOR

Takeshi Miki <miki@cpan.org>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO