Client/http.pm

filter: Statementstmt 96.5% 84 / 87 Branchbran 84.6% 22 / 26 Conditioncond 62.0% 18 / 29 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 83.2% 154 / 185 CC 25 SCAR 16.5
CC
25
Cov
87%
execute
12.2
ua
5.7
__ANON__
3.0
CRAP 5.2
« Client/CLI.pm ↑ Summary Client.pm »
1 package GraphQL::Client::http;
2 # ABSTRACT: GraphQL over HTTP
3  
4 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use HTTP::AnyUA::Util qw(www_form_urlencode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::AnyUA;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $VERSION = '0.605'; # VERSION
13  
14 3 ▶sub _croak { require Carp; goto &Carp::croak }
Subroutine _croak: called CC=1 SCAR=0.0
15  
16 9 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
17 9 my $class = shift;
18 9 ▶ my $self = @_ % 2 == 0 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 9 0 9
19 9 bless $self, $class;
20 }
21  
22 13 ▶sub execute {
Subroutine execute: called CC=12 SCAR=25.0
Pod documented
23 13 my $self = shift;
24 13 my ($request, $options) = @_;
25  
26 13 ▶ my $url = delete $options->{url} || $self->url;
Condition100%3/3
delete $options->{'url'} || $self->url
l!l&&r!l&&!r
193
MC/DC100%2/2
delete $options->{'url'} || $self->url
delete $options->{'url'} $self->url
Truth table100%3/3
A || B
A= delete $options->{'url'}B= $self->url
ABresult
1X1
011
000
27 13 ▶ my $method = delete $options->{method} || $self->method;
Condition66%2/3
delete $options->{'method'} || $self->method
l!l&&r!l&&!r
2110
MC/DC0%0/2
delete $options->{'method'} || $self->method
delete $options->{'method'} $self->method
Truth table66%2/3
A || B
A= delete $options->{'method'}B= $self->method
ABresult
1X1
011
000
28  
29 13 ▶ $request && ref($request) eq 'HASH' or _croak q{Usage: $http->execute(\%request)};
Branch: 1 branch
BranchTrueFalseTotal
$request and ref $request eq "HASH" or _croak("Usage: \$http->execute(\\%request)") 12 1 13
Condition66%2/3
$request and ref $request eq "HASH"
!ll&&!rl&&r
0112
MC/DC50%1/2
$request and ref $request eq "HASH"
$request ref $request eq "HASH"
Truth table66%2/3
A and B
A= $requestB= ref $request eq "HASH"
ABresult
0X0
100
111
30 12 ▶ $request->{query} or _croak q{Request must have a query};
Branch: 1 branch
BranchTrueFalseTotal
$request->{"query"} or _croak("Request must have a query") 11 1 12
31 11 ▶ $url or _croak q{URL must be provided};
Branch: 1 branch
BranchTrueFalseTotal
$url or _croak("URL must be provided") 10 1 11
32  
33 10 my $data = {%$request};
34  
35 10 ▶ if ($method eq 'GET' || $method eq 'HEAD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'GET' or $method eq 'HEAD') { } 2 8 10
Condition66%2/3
$method eq "GET" or $method eq "HEAD"
l!l&&r!l&&!r
208
MC/DC50%1/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET" $method eq "HEAD"
Truth table66%2/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
ABresult
1X1
011
000
36 2 ▶ $data->{variables} = $self->json->encode($data->{variables}) if $data->{variables};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"variables"} 0 2 2
37 2 my $params = www_form_urlencode($data);
38 2 ▶ my $sep = $url =~ /^[^#]+\?/ ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /^[^#]+\?/ ? : 1 1 2
39 2 ▶ $url =~ s/#/${sep}${params}#/ or $url .= "${sep}${params}";
Branch: 1 branch
BranchTrueFalseTotal
$url =~ s/#/$sep$params#/ or $url .= "$sep$params" 0 2 2
40 }
41 else {
42 8 my $encoded_data = $self->json->encode($data);
43 8 $options->{content} = $encoded_data;
44 8 $options->{headers}{'content-length'} = length $encoded_data;
45 8 $options->{headers}{'content-type'} = 'application/json;charset=UTF-8';
46 }
47  
48 10 return $self->_handle_response($self->any_ua->request($method, $url, $options));
49 }
50  
51 10 ▶sub _handle_response {
Subroutine _handle_response: called CC=3 SCAR=11.0
52 10 my $self = shift;
53 10 my ($resp) = @_;
54  
55 10 ▶ if (eval { $resp->isa('Future') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $resp->isa('Future') } }) { } 3 7 10
56 3 ▶ return $resp->followed_by(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
57 3 my $f = shift;
58  
59 3 ▶ if (my ($exception, $category, @other) = $f->failure) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $f->failure) 1 2 3
60 1 ▶ if (ref $exception eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exception eq "HASH") 1 0 1
61 1 my $resp = $exception;
62 1 return Future->done($self->_handle_error($resp));
63 }
64  
65 0 return Future->done({
66 error => $exception,
67 response => undef,
68 details => {
69 exception_details => [$category, @other],
70 },
71 });
72 }
73  
74 2 my $resp = $f->get;
75 2 return Future->done($self->_handle_success($resp));
76 3 });
77 }
78 else {
79 7 ▶ return $self->_handle_error($resp) if !$resp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->{"success"} 4 3 7
80 3 return $self->_handle_success($resp);
81 }
82 }
83  
84 5 ▶sub _handle_error {
Subroutine _handle_error: called CC=3 SCAR=11.1
85 5 my $self = shift;
86 5 my ($resp) = @_;
87  
88 5 my $data = eval { $self->json->decode($resp->{content}) };
89 5 ▶ my $content = $resp->{content} // 'No content';
Condition50%1/2
$resp->{"content"} // "No content"
l!l
50
MC/DC0%0/1
$resp->{"content"} // "No content"
$resp->{"content"}
Truth table50%1/2
A
A= $resp->{"content"}
Aresult
11
00
90 5 ▶ my $reason = $resp->{reason} // '';
Condition50%1/2
$resp->{"reason"} // ""
l!l
50
MC/DC0%0/1
$resp->{"reason"} // ""
$resp->{"reason"}
Truth table50%1/2
A
A= $resp->{"reason"}
Aresult
11
00
91 5 my $message = "HTTP transport returned $resp->{status} ($reason): $content";
92  
93 5 chomp $message;
94  
95 return {
96 5 error => $message,
97 response => $data,
98 details => {
99 http_response => $resp,
100 },
101 };
102 }
103  
104 5 ▶sub _handle_success {
Subroutine _handle_success: called CC=2 SCAR=6.9
105 5 my $self = shift;
106 5 my ($resp) = @_;
107  
108 5 my $data = eval { $self->json->decode($resp->{content}) };
109 5 ▶ if (my $exception = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exception = $@) 1 4 5
110 return {
111 1 error => "HTTP transport failed to decode response: $exception",
112 response => undef,
113 details => {
114 http_response => $resp,
115 },
116 };
117 }
118  
119 return {
120 4 response => $data,
121 details => {
122 http_response => $resp,
123 },
124 };
125 }
126  
127 5 ▶sub ua {
Subroutine ua: called CC=3 SCAR=17.3
Pod documented
128 5 my $self = shift;
129 5 ▶ $self->{ua} //= do {
Condition33%1/3
$self->{"ua"} //= do { require HTTP::Tiny; "HTTP::Tiny"->new("agent", $ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"} // "perl-graphql-client/$VERSION") }
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->{"ua"} //= do { require HTTP::Tiny; "HTTP::Tiny"->new("agent", $ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"} // "perl-graphql-client/$VERSION") }
$self->{"ua"} do { require HTTP::Tiny; "HTTP::Tiny"->new("agent", $ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"} // "perl-graphql-client/$VERSION") }
Truth table33%1/3
A //= B
A= $self->{"ua"}B= do { require HTTP::Tiny; "HTTP::Tiny"->new("agent", $ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"} // "perl-graphql-client/$VERSION") }
ABresult
1X1
011
000
130 0 require HTTP::Tiny;
131 HTTP::Tiny->new(
132 0 ▶ agent => $ENV{GRAPHQL_CLIENT_HTTP_USER_AGENT} // "perl-graphql-client/$VERSION",
Condition0%0/2
$ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"} // "perl-graphql-client/$VERSION"
l!l
00
MC/DC0%0/1
$ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"} // "perl-graphql-client/$VERSION"
$ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"}
Truth table0%0/2
A
A= $ENV{"GRAPHQL_CLIENT_HTTP_USER_AGENT"}
Aresult
11
00
133 );
134 };
135 }
136  
137 15 ▶sub any_ua {
Subroutine any_ua: called CC=2 SCAR=7.1
Pod documented
138 15 my $self = shift;
139 15 ▶ $self->{any_ua} //= HTTP::AnyUA->new(ua => $self->ua);
Condition66%2/3
$self->{"any_ua"} //= "HTTP::AnyUA"->new("ua", $self->ua)
l!l&&r!l&&!r
1050
MC/DC0%0/2
$self->{"any_ua"} //= "HTTP::AnyUA"->new("ua", $self->ua)
$self->{"any_ua"} "HTTP::AnyUA"->new("ua", $self->ua)
Truth table66%2/3
A //= B
A= $self->{"any_ua"}B= "HTTP::AnyUA"->new("ua", $self->ua)
ABresult
1X1
011
000
140 }
141  
142 14 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod documented
143 14 my $self = shift;
144 14 $self->{url};
145 }
146  
147 13 ▶sub method {
Subroutine method: called CC=2 SCAR=6.9
Pod documented
148 13 my $self = shift;
149 13 ▶ $self->{method} // 'POST';
Condition100%2/2
$self->{"method"} // "POST"
l!l
112
MC/DC100%1/1
$self->{"method"} // "POST"
$self->{"method"}
Truth table100%2/2
A
A= $self->{"method"}
Aresult
11
00
150 }
151  
152 18 ▶sub json {
Subroutine json: called CC=2 SCAR=7.0
Pod documented
153 18 my $self = shift;
154 18 ▶ $self->{json} //= do {
Condition66%2/3
$self->{"json"} //= do { require JSON::MaybeXS; "JSON::MaybeXS"->new("utf8", 1) }
l!l&&r!l&&!r
1440
MC/DC0%0/2
$self->{"json"} //= do { require JSON::MaybeXS; "JSON::MaybeXS"->new("utf8", 1) }
$self->{"json"} do { require JSON::MaybeXS; "JSON::MaybeXS"->new("utf8", 1) }
Truth table66%2/3
A //= B
A= $self->{"json"}B= do { require JSON::MaybeXS; "JSON::MaybeXS"->new("utf8", 1) }
ABresult
1X1
011
000
155 4 require JSON::MaybeXS;
156 4 JSON::MaybeXS->new(utf8 => 1);
157 };
158 }
159  
160 5 1;
161  
162 __END__
« Client/CLI.pm ↑ Summary Client.pm »