blib/lib/WWW/StreamAPI.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 169 CC 17 SCAR 44.7
CC
17
Cov
0%
request
182.0
live_sessions
12.0
new
6.0
CRAP 87.2
1 package WWW::StreamAPI;
2  
3 use Carp qw( carp croak );
4 use Time::HiRes qw( gettimeofday );
5 use Digest::MD5 qw( md5_hex );
6 use Data::Dumper qw( Dumper );
7 use XML::Simple qw( XMLin );
8 use LWP::UserAgent;
9 use UNIVERSAL::require;
10  
11  
12 our $VERSION = '0.01';
13  
14 use base 'Class::Accessor::Fast';
15  
16 =pod
17  
18 =head1 NAME
19  
20 WWW::StreamAPI - Perl interface for the HTTP StreamAPI API
21  
22 =head1 SYNOPSIS
23  
24 use WWW::StreamAPI;
25  
26 my $streamapi = WWW::StreamAPI->new(
27 secret_key => 'YOUR_SECRET_KEY_HERE',
28 api_key => 'YOUR_API_KEY_HERE',
29 debug => 1,
30 );
31  
32 # Fetch a list of live sessions using the simple request method.
33 my $live_session_list = $streamapi->request('/session/live/list');
34 print Dumper \$live_session_list;
35  
36 # Using the live_sessions helper method.
37 my @live_sessions = $streamapi->live_sessions;
38 print Dumper \@live_sessions;
39  
40 # Request a list of all recordings.
41 my $recordings = $streamapi->request('/video/list');
42 print Dumper \$recordings;
43  
44  
45  
46 =head1 DESCRIPTION
47  
48 A minimal Perl interface to the HTTP API for StreamAPI. L<http://streamapi.com>
49  
50 =head1 Methods
51  
52 =cut
53  
54 __PACKAGE__->mk_accessors(qw( base_url api_key secret_key user_agent timeout debug ));
55  
56 sub new {
57 my ($class, %args) = @_;
58  
59 my $self = {};
60  
61 bless $self, $class;
62  
63 $self->base_url($args{base_url} || 'http://api.streamapi.com/service');
64 $self->api_key($args{api_key} || croak 'REQUIRED argument No api_key was NOT specified. Dying.');
65 $self->secret_key($args{secret_key} || croak 'REQUIRED argument secret_key was NOT specified. Dying.');
66  
67 $self->timeout($args{timeout} || 30);
68 $self->debug($args{debug} || 0);
69  
70 $self->user_agent(LWP::UserAgent->new(
71 agent => __PACKAGE__ . '/' . $VERSION,
72 timeout => $self->timeout,
73 ));
74  
75 if ($self->debug) {
76 carp 'base_url: ' . $self->base_url;
77 carp 'api_key: ' . $self->api_key;
78 carp 'secret_key: ' . $self->secret_key;
79 carp 'debug: ' . $self->debug;
80 carp 'timeout: ' . $self->timeout;
81 }
82  
83 return $self;
84 }
85  
86 =pod
87  
88 =head2 C<request>
89  
90 The first parameter is the HTTP path to call, so '/video/list' becomes http://api.streamapi.com/service/video/list.
91  
92 The second parameter is a hashref of arguments to send along with the required parametes (rid, sig, api key).
93  
94 The third parameter sets the HTTP request method. The default method is GET.
95  
96 Examples:
97  
98 my $request= $streamapi->request('/video/list');
99  
100 my $request = $stickam->request('/path/to/call', { arg1 => 'val1' });
101  
102 my $request = $stickam->request('/path/to/call', { arg1 => 'val1' }, 'POST');
103  
104 The return value is always the datastructure returned by XML::Simple::XMLin.
105  
106 =cut
107  
108 sub request {
109 my ($self, $call_uri, $args, $method) = @_;
110  
111 $method ||= 'GET';
112  
113 my $request_id = int(gettimeofday * 1000);
114  
115 $args->{rid} = $request_id;
116 $args->{key} = $self->api_key;
117  
118 my $signature = '';
119  
120 foreach my $key (sort keys %{$args}) {
121 next if $key eq 'rid';
122 $signature .= $args->{$key};
123 }
124  
125 $args->{sig} = md5_hex($signature . $self->secret_key . $request_id);
126  
127 my $response;
128  
129 my $uri = URI->new($self->base_url . $call_uri);
130  
131 if ($self->debug) {
132 carp "$method: Using URL '$uri'";
133 }
134  
135 if ($method eq 'GET') {
136 $uri->query_form(%{$args});
137 $response = $self->user_agent->get($uri->as_string);
138 } elsif ($method eq 'POST') {
139 $response = $self->user_agent->post($uri->as_string, $args);
140 }
141  
142 if ($response->is_success) {
143  
144 my $content = $response->content;
145  
146 if ($self->debug) {
147 carp "XML response: $content\n";
148 }
149  
150 my $xml = eval { XMLin $content };
151  
152  
153 if (defined $xml and $xml) {
154  
155 if ($xml->{code} == 0) {
156 return $xml;
157 } else {
158 if ($self->debug) {
159 carp "HTTP response was successful but response code was non-zero:\n$content";
160 if ($method eq 'POST') {
161 carp "POST parameters:";
162 carp "\t$k => '$v'" while (my ($k, $v) = each %{$args});
163 }
164 }
165 }
166  
167 } else {
168  
169 if ($self->debug) {
170 carp "No XML able to parse";
171 }
172  
173 }
174  
175 } else {
176 if ($self->debug) {
177 carp "HTTP response unsuccessful: " . $response->status_line;
178 }
179 }
180 }
181  
182 =pod
183  
184 =head2 C<live_sessions>
185  
186 my @live_sesions = $streamapi->live_sessions;
187  
188 =cut
189  
190 sub live_sessions {
191 my ($self) = @_;
192  
193 my $result = $self->request('/session/live/list');
194  
195 unless (defined $result->{live_sessions} and defined $result->{live_sessions}->{session}) {
196 return;
197 }
198  
199 my $session = $result->{live_sessions}->{session};
200  
201 return (ref $session eq 'HASH') ? $session : return @{$session};
202 }
203  
204 =pod
205  
206 =head2 C<create_session>
207  
208 my ($private_hostid, $public_hostid) = $streamapi->create_session;
209  
210 or
211  
212 my ($private_hostid, $public_hostid) = $streamapi->create_session( username => 'Peter' );
213  
214 =cut
215  
216 sub create_session {
217 my $self = shift;
218 my %args = (@_ >= 2) ? @_ : ();
219  
220 my $result = $self->request('/session/create', \%args, 'POST');
221  
222 return ($result->{private_hostid}, $result->{public_hostid});
223 }
224  
225 =head1 COPYRIGHT
226  
227 Copyright 2008, 2009 by Stickam.com E<lt>support@stickam.comE<gt>
228  
229 This program is free software; you can redistribute it and/or
230 modify it under the same terms as Perl itself.
231  
232 See F<http://www.perl.com/perl/misc/Artistic.html>
233  
234 =head1 AUTHOR
235  
236 Jake Gold <jake@stickam.com>
237  
238 =head1 SEE ALSO
239  
240 L<http://streamapi.com>
241  
242 =cut
243  
244  
245 1;