| 1 | package Document::eSign::Docusign::sendRequest; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use HTTP::Headers; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | Document::eSign::Docusign::sendRequest - Handles communication with DocuSign | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | Version 0.02 | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 functions | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =head2 new($method, $contenttype, $credentials, $uri, $params, $query_params) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | Handles communication with the Docusign API. Behavior adapts according to what is requested by a particular call. | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=25 SCAR=64.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | carp( "Got send request: " . Dumper(@_) ) if $_[1]->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $main = shift; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my $method = shift; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $contenttype = shift; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $credentials = shift; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $uri = shift; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $params = shift; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my $query_params = shift; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $ua = LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||||||
| 41 | default_headers => HTTP::Headers->new( | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 'X-DocuSign-Authentication' => $credentials, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | Accept => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $ua->add_handler( "request_send", sub { shift->dump; return } ) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if $main->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $ua->add_handler( "response_done", sub { shift->dump; return } ) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if $main->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $ua->env_proxy(); | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $ua->default_header( 'Content-Type' => $contenttype ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | if defined $contenttype; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my ( $response, $jsonparams, $multipart ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $json = JSON->new->allow_nonref; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ( defined $params | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $params and defined $contenttype and $contenttype =~ /json|multipart/i
defined $params and defined $contenttype
MC/DC0%0/3
defined $params and defined $contenttype and $contenttype =~ /json|multipart/i
defined $params
defined $contenttype
$contenttype =~ /json|multipart/i
Truth table0%0/4
A and B and C
A= defined $paramsB= defined $contenttypeC= $contenttype =~ /json|multipart/i
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | && defined $contenttype | ||||||||||||||||||||||||||||||||||||||||||
| 63 | && $contenttype =~ /json|multipart/i ) | ||||||||||||||||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $jsonparams = $json->encode($params); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( defined $contenttype && $contenttype =~ /multipart/i ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $contenttype and $contenttype =~ /multipart/i
MC/DC0%0/2
defined $contenttype and $contenttype =~ /multipart/i
defined $contenttype
$contenttype =~ /multipart/i
Truth table0%0/3
A and B
A= defined $contenttypeB= $contenttype =~ /multipart/i
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | { #This shit is ugly, I am ashamed XXX | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $multipart = <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | --snipsnip | ||||||||||||||||||||||||||||||||||||||||||
| 74 | Content-Type: application/json | ||||||||||||||||||||||||||||||||||||||||||
| 75 | Content-Disposition: form-data | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | $jsonparams | ||||||||||||||||||||||||||||||||||||||||||
| 78 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ( defined $params->{documents} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | for my $doc ( @{ $params->{documents} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 81 | open( my $fh, "<", $doc->{name} ) | ||||||||||||||||||||||||||||||||||||||||||
| 82 | or | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | croak( "Unable to open file: " . $doc->{name} . " :: " . $! ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $doc->{name} =~ s/^.*(\\|\/)//; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | local $/ = ''; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my $pdf = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $multipart .= <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||
| 88 | --snipsnip | ||||||||||||||||||||||||||||||||||||||||||
| 89 | Content-Type: application/pdf | ||||||||||||||||||||||||||||||||||||||||||
| 90 | Content-Disposition: file; filename="$doc->{name}"; documentid="$doc->{documentId}" | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $multipart .= <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||
| 97 | --snipsnip-- | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ( $method eq 'GET' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ( defined $query_params ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $uri_with_params = URI->new($uri); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $uri_with_params->query_form($query_params); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $response = $ua->get($uri_with_params); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $response = $ua->get($uri); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | elsif ( $method eq 'POST' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | $response = | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$multipart || $jsonparams || $params
$multipart or $jsonparams
MC/DC0%0/3
$multipart || $jsonparams || $params
$multipart
$jsonparams
$params
Truth table0%0/4
A or B || C
A= $multipartB= $jsonparamsC= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | $ua->post( $uri, Content => $multipart || $jsonparams || $params ); | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | elsif ( $method eq 'PUT' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $response = | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$multipart || $jsonparams || $params
$multipart or $jsonparams
MC/DC0%0/3
$multipart || $jsonparams || $params
$multipart
$jsonparams
$params
Truth table0%0/4
A or B || C
A= $multipartB= $jsonparamsC= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | $ua->put( $uri, Content => $multipart || $jsonparams || $params ); | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | elsif ( $method eq 'DELETE' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $response = $ua->delete($uri); | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return { error => | |||||||||||||||||||||||||||||||||||||||||
| 133 | "An undefined method was used, only use GET, POST, PUT, or DELETE" | ||||||||||||||||||||||||||||||||||||||||||
| 134 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ( $method =~ /GET|POST/ && $response->is_success ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$method =~ /GET|POST/ and $response->is_success
MC/DC0%0/2
$method =~ /GET|POST/ and $response->is_success
$method =~ /GET|POST/
$response->is_success
Truth table0%0/3
A and B
A= $method =~ /GET|POST/B= $response->is_success
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return $json->decode( $response->decoded_content ); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | elsif ( $response->is_success ) | ||||||||||||||||||||||||||||||||||||||||||
| 141 | { #Calls that simply do something and are not expected to return data. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return { Status => $response->status_line }; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | return { error => $response->status_line }; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||