| 1 | package WWW::Keycloak::Role::HTTP; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Sending requests to Keycloak and turning failures into exceptions | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use WWW::Keycloak::Error::API; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use WWW::Keycloak::Error::Network; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 152 | ▶ | sub json_codec { JSON::MaybeXS->new( utf8 => 1, canonical => 1, convert_blessed => 1 ) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
json_codec: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 20 | ▶ | sub api_error_class { 'WWW::Keycloak::Error::API' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_error_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub network_error_class { 'WWW::Keycloak::Error::Network' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
network_error_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 156 | ▶ | sub build_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_request: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 156 | my ( $self, $method, $url, %arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | 156 | my $request = HTTP::Request->new( $method => $url ); | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 156 | $request->header( Accept => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 156 | ▶ | $request->header( Authorization => 'Bearer '.$arg{bearer} ) if defined $arg{bearer}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 156 | ▶ | if ( exists $arg{json} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 38 | $request->header( 'Content-Type' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 38 | $request->content( $self->json_codec->encode( $arg{json} ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | elsif ( $arg{form} ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 26 | my $uri = URI->new('http:'); | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 26 | $uri->query_form( map { $_ => $arg{form}{$_} } grep { defined $arg{form}{$_} } sort keys %{ $arg{form} } ); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 26 | $request->header( 'Content-Type' => 'application/x-www-form-urlencoded' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 26 | ▶ | $request->content( $uri->query // '' ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$uri->query // ""
MC/DC0%0/1
$uri->query // ""
$uri->query
Truth table50%1/2
A
A= $uri->query
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 156 | return $request; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 157 | ▶ | sub read_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_response: called CC=12 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 157 | my ( $self, $response, $method, $url, %arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | # decoded_content undoes a Content-Encoding, the raw content does not; with | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # charset => 'none' the bytes stay bytes, which the JSON codec decodes itself | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 157 | ▶ | my $content = $response->decoded_content( charset => 'none' ) // ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->decoded_content("charset", "none") // ""
MC/DC0%0/1
$response->decoded_content("charset", "none") // ""
$response->decoded_content("charset", "none")
Truth table50%1/2
A
A= $response->decoded_content("charset", "none")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 157 | ▶ | my $data = length $content ? eval { $self->json_codec->decode($content) } : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 157 | ▶ | return { status => $response->code, data => $data, location => scalar $response->header('Location') } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | if $response->is_success; | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 16 | my ( $message, $oauth ); | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 16 | ▶ | if ( ref $data eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 16 | ▶ | $message = $data->{errorMessage} // $data->{error}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$data->{"errorMessage"} // $data->{"error"}
MC/DC0%0/2
$data->{"errorMessage"} // $data->{"error"}
$data->{"errorMessage"}
$data->{"error"}
Truth table66%2/3
A // B
A= $data->{"errorMessage"}B= $data->{"error"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 16 | ▶ | if ( defined $data->{error} && defined $data->{error_description} || $arg{form} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $data->{"error"} and defined $data->{"error_description"} or $arg{"form"}
defined $data->{"error"} and defined $data->{"error_description"}
MC/DC66%2/3
defined $data->{"error"} and defined $data->{"error_description"} or $arg{"form"}
defined $data->{"error"}
defined $data->{"error_description"}
$arg{"form"}
Truth table60%3/5
A and B or C
A= defined $data->{"error"}B= defined $data->{"error_description"}C= $arg{"form"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | $oauth = $data->{error}; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | $message = $data->{error}.( defined $data->{error_description} ? ': '.$data->{error_description} : '' ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 6 | ▶ | if defined $data->{error}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | $self->api_error_class->throw( | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 16 | ▶ | message => $method.' '.$url.' failed: '.$response->status_line.( defined $message ? ' - '.$message : '' ), | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | http_status => $response->code, | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | api_message => $message, | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | oauth_error => $oauth | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 156 | ▶ | sub send_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_request: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 156 | my ( $self, $method, $url, %arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | 156 | my $response = $self->ua->request( $self->build_request( $method, $url, %arg ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 156 | ▶ | $self->network_error_class->throw( message => $method.' '.$url.': '.$response->status_line ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$response->code == 500 and ($response->header("Client-Warning") // "") eq "Internal response"
$response->header("Client-Warning") // ""
MC/DC50%1/2
$response->code == 500 and ($response->header("Client-Warning") // "") eq "Internal response"
$response->code == 500
$response->header("Client-Warning")
Truth table66%2/3
A and B
A= $response->code == 500B= $response->header("Client-Warning")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | if $response->code == 500 && ( $response->header('Client-Warning') // '' ) eq 'Internal response'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 155 | return $self->read_response( $response, $method, $url, %arg ); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||