| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 27 | 13 | ▶ | my $method = delete $options->{method} || $self->method; | ||||||||||||||||||||||||||
|
Condition66%2/3
delete $options->{'method'} || $self->method
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
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 13 | ▶ | $request && ref($request) eq 'HASH' or _croak q{Usage: $http->execute(\%request)}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$request and ref $request eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 30 | 12 | ▶ | $request->{query} or _croak q{Request must have a query}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 11 | ▶ | $url or _croak q{URL must be provided}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 10 | my $data = {%$request}; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 10 | ▶ | if ($method eq 'GET' || $method eq 'HEAD') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method eq "GET" or $method eq "HEAD"
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"
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | $data->{variables} = $self->json->encode($data->{variables}) if $data->{variables}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 2 | my $params = www_form_urlencode($data); | |||||||||||||||||||||||||||
| 38 | 2 | ▶ | my $sep = $url =~ /^[^#]+\?/ ? '&' : '?'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | $url =~ s/#/${sep}${params}#/ or $url .= "${sep}${params}"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | if (ref $exception eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$resp->{"content"} // "No content"
$resp->{"content"}
Truth table50%1/2
A
A= $resp->{"content"}
| |||||||||||||||||||||||||||||
| 90 | 5 | ▶ | my $reason = $resp->{reason} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$resp->{"reason"} // ""
MC/DC0%0/1
$resp->{"reason"} // ""
$resp->{"reason"}
Truth table50%1/2
A
A= $resp->{"reason"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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")
}
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")
}
| |||||||||||||||||||||||||||||
| 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"
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"}
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$self->{"method"} // "POST"
$self->{"method"}
Truth table100%2/2
A
A= $self->{"method"}
| |||||||||||||||||||||||||||||
| 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)
}
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)
}
| |||||||||||||||||||||||||||||
| 155 | 4 | require JSON::MaybeXS; | |||||||||||||||||||||||||||
| 156 | 4 | JSON::MaybeXS->new(utf8 => 1); | |||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 5 | 1; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||