line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::PhotoZou; |
2
|
5
|
|
|
5
|
|
190160
|
use strict; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
208
|
|
3
|
5
|
|
|
5
|
|
31
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
169
|
|
4
|
5
|
|
|
5
|
|
26
|
use base qw(Class::Accessor::Fast); |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
6220
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
28163
|
use Carp; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
622
|
|
7
|
5
|
|
|
5
|
|
13477
|
use HTTP::Request::Common; |
|
5
|
|
|
|
|
280369
|
|
|
5
|
|
|
|
|
931
|
|
8
|
5
|
|
|
5
|
|
9055
|
use LWP::UserAgent; |
|
5
|
|
|
|
|
239426
|
|
|
5
|
|
|
|
|
598
|
|
9
|
5
|
|
|
5
|
|
14335
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %API_ROOT = ( |
14
|
|
|
|
|
|
|
jp => 'http://api.photozou.jp/rest/', |
15
|
|
|
|
|
|
|
com => 'http://api.photozou.com/rest/', |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/username password errors/); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
|
|
|
|
|
|
my ($class, %opt) = @_; |
22
|
|
|
|
|
|
|
my $site = $opt{site} || 'jp'; |
23
|
|
|
|
|
|
|
my $api_root = $API_ROOT{$site}; |
24
|
|
|
|
|
|
|
croak("unknown site $site") unless $api_root; |
25
|
|
|
|
|
|
|
my ($host) = $api_root =~ m!^http://(.*?)/!; |
26
|
|
|
|
|
|
|
my $self = bless { |
27
|
|
|
|
|
|
|
username => $opt{username} || '', |
28
|
|
|
|
|
|
|
password => $opt{password} || '', |
29
|
|
|
|
|
|
|
api_root => $api_root, |
30
|
|
|
|
|
|
|
host => $host, |
31
|
|
|
|
|
|
|
}, $class; |
32
|
|
|
|
|
|
|
$self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub ua { |
36
|
|
|
|
|
|
|
my $self = shift; |
37
|
|
|
|
|
|
|
if (@_) { |
38
|
|
|
|
|
|
|
$self->{ua} = shift; |
39
|
|
|
|
|
|
|
} else { |
40
|
|
|
|
|
|
|
$self->{ua} and return $self->{ua}; |
41
|
|
|
|
|
|
|
$self->{ua} = LWP::UserAgent->new; |
42
|
|
|
|
|
|
|
$self->{ua}->agent(__PACKAGE__."/$VERSION"); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
$self->{ua}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _request { |
48
|
|
|
|
|
|
|
my ($self, $url, %param) = @_; |
49
|
|
|
|
|
|
|
$self->ua->credentials("$self->{host}:80", 'photo', $self->username, $self->password); |
50
|
|
|
|
|
|
|
my $req = (grep { ref $_ eq 'ARRAY' } values %param) |
51
|
|
|
|
|
|
|
? POST($self->{api_root}.$url, Content_Type => 'form-data', Content => [%param]) |
52
|
|
|
|
|
|
|
: POST($self->{api_root}.$url, [%param]); |
53
|
|
|
|
|
|
|
my $res = $self->ua->request($req); |
54
|
|
|
|
|
|
|
croak("request failed.\n".$res->status_line) if $res->is_error; |
55
|
|
|
|
|
|
|
$res->content; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _to_result { |
59
|
|
|
|
|
|
|
my ($self, $xml, %opt) = @_; |
60
|
|
|
|
|
|
|
if (exists $opt{ForceArray}) { |
61
|
|
|
|
|
|
|
push @{$opt{ForceArray}}, 'err'; |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
$opt{ForceArray} = ['err']; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
my $result = XML::Simple::XMLin($xml, %opt); |
66
|
|
|
|
|
|
|
if ($result->{stat} eq 'fail') { |
67
|
|
|
|
|
|
|
$self->{errors} = $result->{err}; |
68
|
|
|
|
|
|
|
return; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
$result; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub nop { |
74
|
|
|
|
|
|
|
my $self = shift; |
75
|
|
|
|
|
|
|
my $content = $self->_request('nop'); |
76
|
|
|
|
|
|
|
my $res = $self->_to_result($content) or return; |
77
|
|
|
|
|
|
|
$res->{stat}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub photo_add { |
81
|
|
|
|
|
|
|
my ($self, %param) = @_; |
82
|
|
|
|
|
|
|
croak('paramater photo is required') unless exists $param{photo}; |
83
|
|
|
|
|
|
|
croak('paramater album_id is required') unless exists $param{album_id}; |
84
|
|
|
|
|
|
|
$param{photo} = [$param{photo}]; |
85
|
|
|
|
|
|
|
my $content = $self->_request('photo_add', %param); |
86
|
|
|
|
|
|
|
my $res = $self->_to_result($content) or return; |
87
|
|
|
|
|
|
|
$res->{photo_id}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub photo_add_album { |
91
|
|
|
|
|
|
|
my ($self, %param) = @_; |
92
|
|
|
|
|
|
|
croak('paramater name is required') unless exists $param{name}; |
93
|
|
|
|
|
|
|
my $content = $self->_request('photo_add_album', %param); |
94
|
|
|
|
|
|
|
my $res = $self->_to_result($content) or return; |
95
|
|
|
|
|
|
|
$res->{album_id}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub photo_album { |
99
|
|
|
|
|
|
|
my $self = shift; |
100
|
|
|
|
|
|
|
my $content = $self->_request('photo_album'); |
101
|
|
|
|
|
|
|
my $res = $self->_to_result($content, |
102
|
|
|
|
|
|
|
KeyAttr => 'album', |
103
|
|
|
|
|
|
|
ForceArray => ['album'], |
104
|
|
|
|
|
|
|
) or return; |
105
|
|
|
|
|
|
|
$res->{info}->{album} || []; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub search_public { |
109
|
|
|
|
|
|
|
my ($self, %param) = @_; |
110
|
|
|
|
|
|
|
my $content = $self->_request('search_public', %param); |
111
|
|
|
|
|
|
|
my $res = $self->_to_result($content, ForceArray => ['photo']) or return; |
112
|
|
|
|
|
|
|
$res->{info}->{photo} || []; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub user_group { |
116
|
|
|
|
|
|
|
my $self = shift; |
117
|
|
|
|
|
|
|
my $content = $self->_request('user_group'); |
118
|
|
|
|
|
|
|
my $res = $self->_to_result($content, |
119
|
|
|
|
|
|
|
keyAttr => 'user_group', |
120
|
|
|
|
|
|
|
ForceArray => ['user_group'], |
121
|
|
|
|
|
|
|
) or return; |
122
|
|
|
|
|
|
|
$res->{info}->{user_group} || []; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub errormsg { |
126
|
|
|
|
|
|
|
my $self = shift; |
127
|
|
|
|
|
|
|
my $msg; |
128
|
|
|
|
|
|
|
for my $error (@{$self->errors}) { |
129
|
|
|
|
|
|
|
$msg .= "$error->{code}: $error->{msg}\n"; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
$msg; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
__END__ |