| 1 | package WWW::Plivo::API; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | WWW::Plivo - Plivo interface to WWW | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Version 0.01 | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Plivo interface - copied from WWW::Twilio and tweaked for Plivo access - see Twilio version for details. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | our $Debug = 0; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use LWP::UserAgent (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use URI::Escape qw(uri_escape uri_escape_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use List::Util '1.29', 'pairs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub API_URL { 'https://api.plivo.com' } | ||||||||||||||||||||||||||
|
Subroutine
API_URL: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub API_VERSION { 'v1' } | ||||||||||||||||||||||||||
|
Subroutine
API_VERSION: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ## NOTE: This is an inside-out object; remove members in | ||||||||||||||||||||||||||||
| 37 | ## NOTE: the DESTROY() sub if you add additional members. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | my %account_authid = (); | |||||||||||||||||||||||||||
| 40 | 1 | my %auth_token = (); | |||||||||||||||||||||||||||
| 41 | 1 | my %api_version = (); | |||||||||||||||||||||||||||
| 42 | 1 | my %lwp_callback = (); | |||||||||||||||||||||||||||
| 43 | 1 | my %utf8 = (); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub new { | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | my $self = bless \(my $ref), $class; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $account_authid {$self} = $args{AccountAuthid} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'AccountAuthid'} || ''
MC/DC0%0/1
$args{'AccountAuthid'} || ''
$args{'AccountAuthid'}
Truth table0%0/2
A
A= $args{'AccountAuthid'}
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $auth_token {$self} = $args{AuthToken} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'AuthToken'} || ''
MC/DC0%0/1
$args{'AuthToken'} || ''
$args{'AuthToken'}
Truth table0%0/2
A
A= $args{'AuthToken'}
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $api_version {$self} = $args{API_VERSION} || API_VERSION(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{'API_VERSION'} || API_VERSION()
MC/DC0%0/2
$args{'API_VERSION'} || API_VERSION()
$args{'API_VERSION'}
API_VERSION()
Truth table0%0/3
A || B
A= $args{'API_VERSION'}B= API_VERSION()
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $lwp_callback {$self} = $args{LWP_Callback} || undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'LWP_Callback'} || undef
MC/DC0%0/1
$args{'LWP_Callback'} || undef
$args{'LWP_Callback'}
Truth table0%0/2
A
A= $args{'LWP_Callback'}
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $utf8 {$self} = $args{utf8} || undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'utf8'} || undef
MC/DC0%0/1
$args{'utf8'} || undef
$args{'utf8'}
Truth table0%0/2
A
A= $args{'utf8'}
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | return $self; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub GET { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | _do_request(shift, METHOD => 'GET', API => shift, @_); | ||||||||||||||||||||||||||
|
Subroutine
GET: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub HEAD { | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | _do_request(shift, METHOD => 'HEAD', API => shift, @_); | ||||||||||||||||||||||||||
|
Subroutine
HEAD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub POST { | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | _do_request(shift, METHOD => 'POST', API => shift, @_); | ||||||||||||||||||||||||||
|
Subroutine
POST: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub PUT { | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | _do_request(shift, METHOD => 'PUT', API => shift, @_); | ||||||||||||||||||||||||||
|
Subroutine
PUT: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub DELETE { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | _do_request(shift, METHOD => 'DELETE', API => shift, @_); | ||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ## METHOD => GET|POST|PUT|DELETE | ||||||||||||||||||||||||||||
| 81 | ## API => Calls|Accounts|OutgoingCallerIds|IncomingPhoneNumbers| | ||||||||||||||||||||||||||||
| 82 | ## Recordings|Notifications|etc. | ||||||||||||||||||||||||||||
| 83 | sub _do_request { | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_do_request: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 85 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $lwp = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 88 | $lwp_callback{$self}->($lwp) | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ref($lwp_callback{$self}) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $lwp->agent("perl-WWW-Plivo-API/$VERSION"); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my $method = delete $args{METHOD}; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $url = API_URL() . '/' . $api_version{$self}; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | my $api = delete $args{API} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'API'} || ''
MC/DC0%0/1
delete $args{'API'} || ''
delete $args{'API'}
Truth table0%0/2
A
A= delete $args{'API'}
| |||||||||||||||||||||||||||||
| 96 | 0 | $url .= "/Account/" . $account_authid{$self}."/Message/"; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | my $content = ''; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | if( keys %args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $content = $self->_build_content( %args ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if( $method eq 'GET' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $url .= '?' . $content; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my $req = HTTP::Request->new( $method => $url ); | |||||||||||||||||||||||||||
| 108 | 0 | $req->authorization_basic( $account_authid{$self}, $auth_token{$self} ); | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if( $content and $method ne 'GET' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$content and $method ne "GET"
MC/DC0%0/2
$content and $method ne "GET"
$content
$method ne "GET"
Truth table0%0/3
A and B
A= $contentB= $method ne "GET"
| |||||||||||||||||||||||||||||
| 110 | 0 | $req->content_type( 'application/json' ); | |||||||||||||||||||||||||||
| 111 | 0 | $req->content( $content ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | local $ENV{HTTPS_DEBUG} = $Debug; | |||||||||||||||||||||||||||
| 115 | 0 | my $res = $lwp->request($req); | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | print STDERR "Request sent: " . $req->as_string . "\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return { code => $res->code, | |||||||||||||||||||||||||||
| 119 | message => $res->message, | ||||||||||||||||||||||||||||
| 120 | content => $res->content }; | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | ## builds a string suitable for LWP's content() method | ||||||||||||||||||||||||||||
| 124 | sub _build_content { | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_content: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my $escape_method = $utf8{$self} ? \&uri_escape_utf8 : \&uri_escape; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | my %Subst = ( to => 'dst', from => 'src', body => 'text' ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my %Content; | |||||||||||||||||||||||||||
| 130 | 0 | for my $pair (pairs @_) { | |||||||||||||||||||||||||||
| 131 | 0 | my ($key, $val) = @$pair; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (exists($Subst{lc($key)}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $key = $Subst{lc($key)}; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | $Content{$key} = $val; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 0 | my $json = JSON->new(); | |||||||||||||||||||||||||||
| 139 | 0 | my $json_args = $json->encode(\%Content); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | return $json_args; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | sub DESTROY { | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | delete $account_authid {$self}; | |||||||||||||||||||||||||||
| 148 | 0 | delete $auth_token {$self}; | |||||||||||||||||||||||||||
| 149 | 0 | delete $api_version {$self}; | |||||||||||||||||||||||||||
| 150 | 0 | delete $lwp_callback{$self}; | |||||||||||||||||||||||||||
| 151 | 0 | delete $utf8 {$self}; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | my $super = $self->can("SUPER::DESTROY"); | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | goto &$super if $super; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | 1; | |||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||