File Coverage

blib/lib/WebService/AntiCaptcha.pm
Criterion Covered Total %
statement 23 49 46.9
branch 0 10 0.0
condition 0 7 0.0
subroutine 8 16 50.0
pod 5 8 62.5
total 36 90 40.0


line stmt bran cond sub pod time code
1             package WebService::AntiCaptcha;
2              
3 1     1   64234 use strict;
  1         3  
  1         37  
4 1     1   28 use 5.008_005;
  1         3  
5             our $VERSION = '0.01';
6              
7 1     1   5 use Carp 'croak';
  1         2  
  1         53  
8 1     1   419 use LWP::UserAgent;
  1         36033  
  1         36  
9 1     1   10 use URI;
  1         3  
  1         21  
10 1     1   385 use MIME::Base64;
  1         560  
  1         57  
11 1     1   419 use JSON;
  1         6964  
  1         6  
12              
13 1     1   141 use vars qw/$errstr/;
  1         2  
  1         341  
14 0     0 0   sub errstr { $errstr }
15              
16             sub new {
17 0     0 0   my $class = shift;
18 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
19              
20 0 0         $args{clientKey} or croak "clientKey is required.\n";
21 0   0       $args{ua} ||= LWP::UserAgent->new;
22 0   0       $args{url} ||= 'https://api.anti-captcha.com/';
23 0   0       $args{sleep} ||= 3;
24              
25 0           return bless \%args, $class;
26             }
27              
28             sub createTask {
29 0     0 1   my ($self, $task, $softId, $languagePool) = @_;
30              
31 0 0         $self->request('createTask',
    0          
32             task => $task,
33             $softId ? (softId => $softId) : (),
34             $languagePool ? (languagePool => $languagePool) : (),
35             );
36             }
37              
38             sub getTaskResult {
39 0     0 1   my ($self, $taskId) = @_;
40              
41 0           $self->request('getTaskResult', taskId => $taskId);
42             }
43              
44             sub getBalance {
45 0     0 1   my ($self) = @_;
46              
47 0           $self->request('getBalance');
48             }
49              
50             sub getQueueStats {
51 0     0 1   my ($self, $queueId) = @_;
52              
53 0           $self->request('getQueueStats', queueId => $queueId);
54             }
55              
56             sub reportIncorrectImageCaptcha {
57 0     0 1   my ($self, $taskId) = @_;
58              
59 0           $self->request('reportIncorrectImageCaptcha', taskId => $taskId);
60             }
61              
62             sub request {
63 0     0 0   my ($self, $url, %params) = @_;
64              
65 0           $params{clientKey} = $self->{clientKey};
66              
67 0           my $res = $self->{ua}->post($self->{url} . $url,
68             'Accept' => 'application/json',
69             'Content-Type' => 'application/json',
70             'Content' => encode_json(\%params),
71             );
72              
73             # print Dumper(\$res); use Data::Dumper;
74              
75 0 0         unless ($res->is_success) {
76 0           $errstr = "Failed to post $url: " . $res->status_line;
77 0           return;
78             }
79 0           return decode_json($res->decoded_content);
80             }
81              
82             1;
83             __END__