File Coverage

blib/lib/WebService/Ooyala.pm
Criterion Covered Total %
statement 23 100 23.0
branch 0 12 0.0
condition 0 17 0.0
subroutine 8 26 30.7
pod 18 18 100.0
total 49 173 28.3


line stmt bran cond sub pod time code
1             package WebService::Ooyala;
2              
3 1     1   12887 use 5.006;
  1         2  
4 1     1   2 use strict;
  1         1  
  1         17  
5 1     1   3 use warnings FATAL => 'all';
  1         4  
  1         31  
6 1     1   392 use URI::Escape qw(uri_escape);
  1         919  
  1         51  
7 1     1   540 use LWP::UserAgent;
  1         29857  
  1         29  
8 1     1   7 use Carp qw(croak);
  1         1  
  1         45  
9 1     1   435 use Digest::SHA qw(sha256_base64);
  1         2435  
  1         63  
10 1     1   595 use JSON;
  1         8168  
  1         3  
11              
12             =head1 NAME
13              
14             WebService::Ooyala - Perl interface to Ooyala's API, currently only read
15             operations (GET requests) are supported
16              
17             Support for create, update, and delete (PUT, POST, DELETE) operations will be added in future releases.
18              
19             =head1 VERSION
20              
21             Version 0.03
22              
23             =cut
24              
25             our $VERSION = '0.03';
26              
27             =head1 SYNOPSIS
28              
29             my $ooyala = WebService::Ooyala->new({ api_key => $api_key, secret_key => $secret_key });
30              
31             # Grab all video assets (or at least the first page)
32             my $data = $ooyala->get("assets");
33              
34             foreach my $video(@{$data->{items}}) {
35             print "$video->{embed_code} $video->{name}\n";
36             }
37              
38             # Get a particular video based on embed_code
39              
40             my $video = $data->get("assets/$embed_code");
41              
42              
43             =head1 SUBROUTINES/METHODS
44              
45             =head2 new
46              
47             Create a new WebService::Ooyala object with hashref of parameters
48              
49             my $ooyala = WebService::Ooyala->new( { api_key => $api_key, secret_key => $secret_key } );
50              
51             Accepts the following parmeters
52              
53             =over 4
54              
55             =item * api_key
56              
57             Required parameter. Ooyala api_key
58              
59             =item * secret_key
60              
61             Required parameter. Ooyala secret_key
62              
63             =item * base_url
64              
65             Optional parameter -- defaults to "api.ooyala.com"
66              
67             =item * cache_base_url
68              
69             Optional parameter - defaults to "cdn.api.ooyala.com" for GET requests
70              
71             =item * expiration
72              
73             Optional parametter for time to expire url for getting results from API with
74             this url -- defaults to 15 (seconds)
75              
76             =item * api_version
77              
78             Optional parameter - version of API being called -- defaults to "v2", but this
79             module also works for "v3" API requests by changing this parameter
80              
81             =item * agent
82              
83             Agent that acts like LWP::UserAgent used for making requests -- module defaults to creating its own if none is provide
84              
85             =back
86              
87             =cut
88              
89             sub new {
90 0     0 1   my($class, $params) = @_;
91 0   0       $params ||= {};
92              
93 0           my $self = {};
94 0           $self->{api_key} = $params->{api_key};
95 0           $self->{secret_key} = $params->{secret_key};
96              
97 0 0 0       croak "api_key and secret_key both need to be specified" unless $params->{api_key} && $params->{secret_key};
98              
99 0   0       $self->{base_url} = $params->{base_url} || "api.ooyala.com";
100             $self->{cache_base_url} =
101 0   0       $params->{cache_base_url} || "cdn-api.ooyala.com";
102 0   0       $self->{expiration} = $params->{expiration} || 15;
103 0   0       $self->{api_version} = $params->{api_version} || "v2";
104             $self->{agent} =
105 0           LWP::UserAgent->new(
106             agent => "perl/$], WebService::Ooyala/" . $VERSION);
107              
108 0           bless $self, $class;
109              
110             }
111              
112             =head2 get
113              
114             $ooyala->get($path, $params)
115              
116             $ooyala->get("assets");
117              
118             $ooyala->get("assets", { limit => 5 })
119              
120             $ooyala->get("assets", { limit => 5, where => "labels INCLUDES '$label'" })
121              
122             Accepts the following parameters:
123              
124             =over 4
125              
126             =item * path - path of api request (after version part of api request)
127              
128             =item * params - hashref of query parameters to be sent as part of API request
129              
130             =back
131              
132             -
133              
134              
135              
136              
137             =cut
138              
139             sub get {
140 0     0 1   my($self, $path, $params) = @_;
141 0           return $self->send_request('GET', $path, '', $params);
142             }
143              
144             =head2 expires
145              
146             Calculates expiration time
147              
148             $ooyala->expires()
149              
150             =cut
151              
152             sub expires {
153 0     0 1   my($self) = @_;
154 0           my $now_plus_window = time() + $self->{expiration};
155 0           return $now_plus_window + 300 - ($now_plus_window % 300);
156             }
157              
158             =head2 send_request
159              
160             $ooyala->send_request($http_method, $relative_path, $body, $params);
161              
162             $ooyala->send_request("GET", "assets", "", {})
163              
164             Handles sending request to ooyala's API, suggest using simpler
165              
166             $ooyala->get(..) (which calls this) from your application
167              
168             =cut
169              
170             sub send_request {
171 0     0 1   my($self, $http_method, $relative_path, $body, $params) = @_;
172              
173 0           my $path = "/" . $self->{api_version} . "/" . $relative_path;
174              
175 0           my $json_body = {};
176              
177 0           my $url =
178             $self->build_path_with_authentication_params($http_method, $path,
179             $params, "");
180              
181 0 0         if (!$url) {
182 0           croak "No url generated for request in send_request";
183             }
184              
185 0           my $base_url;
186 0 0         if ($http_method ne 'GET') {
187 0           $base_url = $self->{base_url};
188             } else {
189 0           $base_url = $self->{cache_base_url};
190             }
191              
192 0           my $resp;
193 0 0         if ($http_method eq 'GET') {
194 0           my $full_url = "https://" . $base_url . $url;
195 0           $resp = $self->{agent}->get($full_url);
196              
197 0 0         unless ($resp->is_success) {
198 0           croak "Failed to GET $full_url - " . $resp->status_line;
199             }
200              
201 0 0         if ($resp->is_success) {
202 0           return decode_json($resp->decoded_content);
203             }
204             } else {
205 0           croak
206             "Trying to call a method that is not implemented in send_request";
207             }
208             }
209              
210             =head2 generate_signature
211              
212             Generates signature needed to send API request based on payload
213              
214             $ooyala->($http_method, $path, $params, $body);
215              
216             =cut
217              
218             sub generate_signature {
219 0     0 1   my($self, $http_method, $path, $params, $body) = @_;
220 0   0       $body ||= '';
221              
222 0           my $signature = $self->{secret_key} . uc($http_method) . $path;
223              
224 0           foreach my $key (sort keys %$params) {
225 0           $signature .= $key . "=" . $params->{$key};
226             }
227              
228 0           $signature = sha256_base64($signature);
229 0           return $signature;
230             }
231              
232             =head2 build_path
233              
234             Builds path up with parameters
235              
236             $ooyala->build_path($path, $params(
237              
238             =cut
239              
240             sub build_path {
241 0     0 1   my($self, $path, $params) = @_;
242 0           my $url = $path . '?';
243 0           foreach (keys %$params) {
244 0           $url .= "&$_=" . uri_escape($params->{$_});
245             }
246 0           return $url;
247             }
248              
249             =head2 build_path_with_authentication_params
250              
251             Builds path with authentication and expiration parameters
252              
253             $ooyala->build_path_with_authentication_params($http_method, $path,
254             $params, $body);
255              
256             =cut
257              
258             sub build_path_with_authentication_params {
259 0     0 1   my($self, $http_method, $path, $params, $body) = @_;
260              
261 0   0       $params ||= {};
262 0           my $authentication_params = {%$params};
263 0           $authentication_params->{api_key} = $self->{api_key};
264 0           $authentication_params->{expires} = $self->expires();
265             $authentication_params->{signature} =
266 0           $self->generate_signature($http_method, $path,
267             $authentication_params, $body);
268 0           return $self->build_path($path, $authentication_params);
269              
270             }
271              
272             =head2 get_api_key
273              
274             Gets current ooyala api_key
275              
276             my $api_key = $ooylaa->get_api_key();
277              
278             =cut
279              
280             sub get_api_key {
281 0     0 1   my $self = shift;
282 0           return $self->{api_key};
283             }
284              
285             =head2 set_api_key
286              
287             Sets current ooyala api_key
288              
289             $ooylaa->set_api_key($api_key);
290              
291             =cut
292              
293             sub set_api_key {
294 0     0 1   my($self, $api_key) = @_;
295 0           $self->{api_key} = $api_key;
296             }
297              
298             =head2 get_secret_key
299              
300             Gets current ooyala secret_key
301              
302             my $secret_key = $ooyala->get_secret_key()
303              
304             =cut
305              
306             sub get_secret_key {
307 0     0 1   my $self = shift;
308 0           return $self->{secret_key};
309             }
310              
311             =head2 set_secret_key
312              
313             Sets current ooyala secret_key
314              
315             $ooyala->set_secret_key($secret_key)
316              
317             =cut
318              
319             sub set_secret_key {
320 0     0 1   my($self, $secret_key) = @_;
321 0           $self->{secret_key} = $secret_key;
322             }
323              
324             =head2 get_base_url
325              
326             Gets current base_url
327              
328             my $base_url = $ooyala->get_base_url();
329              
330             =cut
331              
332             sub get_base_url {
333 0     0 1   my $self = shift;
334 0           return $self->{base_url};
335             }
336              
337             =head2 set_base_url
338              
339             Sets current base_url
340              
341             $ooyala->set_base_url($base_url)
342              
343             =cut
344              
345             sub set_base_url {
346 0     0 1   my($self, $base_url) = @_;
347 0           $self->{base_url} = $base_url;
348             }
349              
350             =head2 get_cache_base_url
351              
352             Gets current cache_base_url
353              
354             my $cache_base_url = $ooyala->get_cache_base_url();
355              
356             =cut
357              
358             sub get_cache_base_url {
359 0     0 1   my $self = shift;
360 0           return $self->{cache_base_url};
361             }
362              
363             =head2 set_cache_base_url
364              
365             Sets current cache_base_url
366              
367             $ooyala->set_cache_base_url($cache_base_url)
368              
369             =cut
370              
371             sub set_cache_base_url {
372 0     0 1   my($self, $cache_base_url) = @_;
373 0           $self->{cache_base_url} = $cache_base_url;
374             }
375              
376             =head2 get_expiration
377              
378             Gets current expiration in seconds
379              
380             my $expiration = $ooyala->get_expiration();
381              
382             =cut
383              
384             sub get_expiration {
385 0     0 1   my $self = shift;
386 0           return $self->{expiration};
387             }
388              
389             =head2 set_expiration
390              
391             Sets current expiration in seconds
392              
393             $ooyala->set_expiration($expiration);
394              
395             =cut
396              
397             sub set_expiration {
398 0     0 1   my($self, $expiration) = @_;
399 0           $self->{expiration} = $expiration;
400             }
401              
402             =head2 del_expiration
403              
404             Sets current expiration time to 0
405              
406             $ooyala->del_expiration()
407              
408             =cut
409              
410             sub del_expiration {
411 0     0 1   my $self = shift;
412 0           $self->{expiration} = 0;
413             }
414              
415             =head1 SEE ALSO
416              
417             Code here is a port of L
418              
419             L
420              
421             =head1 ACKNOWLEDGEMENTS
422              
423             Thanks to Slashdot for support in release of this module -- early code for
424             this was developed as part of the Slashdot code base
425              
426             =head1 AUTHOR
427              
428             Tim Vroom, C<< >>
429              
430             =head1 BUGS
431              
432             Please report any bugs or feature requests to C, or through
433             the web interface at L. I will be notified, and then you'll
434             automatically be notified of progress on your bug as I make changes.
435              
436              
437              
438              
439             =head1 SUPPORT
440              
441             You can find documentation for this module with the perldoc command.
442              
443             perldoc WebService::Ooyala
444              
445              
446             You can also look for information at:
447              
448             =over 4
449              
450             =item * RT: CPAN's request tracker (report bugs here)
451              
452             L
453              
454             =item * AnnoCPAN: Annotated CPAN documentation
455              
456             L
457              
458             =item * CPAN Ratings
459              
460             L
461              
462             =item * Search CPAN
463              
464             L
465              
466             =back
467              
468              
469             =head1 LICENSE AND COPYRIGHT
470              
471             Copyright 2016 Tim Vroom.
472              
473             This program is free software; you can redistribute it and/or modify it
474             under the terms of the the Artistic License (2.0). You may obtain a
475             copy of the full license at:
476              
477             L
478              
479             Any use, modification, and distribution of the Standard or Modified
480             Versions is governed by this Artistic License. By using, modifying or
481             distributing the Package, you accept this license. Do not use, modify,
482             or distribute the Package, if you do not accept this license.
483              
484             If your Modified Version has been derived from a Modified Version made
485             by someone other than you, you are nevertheless required to ensure that
486             your Modified Version complies with the requirements of this license.
487              
488             This license does not grant you the right to use any trademark, service
489             mark, tradename, or logo of the Copyright Holder.
490              
491             This license includes the non-exclusive, worldwide, free-of-charge
492             patent license to make, have made, use, offer to sell, sell, import and
493             otherwise transfer the Package with respect to any patent claims
494             licensable by the Copyright Holder that are necessarily infringed by the
495             Package. If you institute patent litigation (including a cross-claim or
496             counterclaim) against any party alleging that the Package constitutes
497             direct or contributory patent infringement, then this Artistic License
498             to you shall terminate on the date that such litigation is filed.
499              
500             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
501             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
502             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
503             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
504             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
505             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
506             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
507             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
508              
509              
510             =cut
511              
512             1; # End of WebService::Ooyala