| 1 | package WebService::ILS::JSON; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 4 | ▶ | use Modern::Perl; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | =encoding utf-8 | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 NAME | ||||||||||||||
| 8 | |||||||||||||||
| 9 | WebService::ILS::JSON - WebService::ILS module for services with JSON API | ||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||
| 12 | |||||||||||||||
| 13 | To be subclassed | ||||||||||||||
| 14 | |||||||||||||||
| 15 | See L<WebService::ILS> | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =cut | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 4 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 4 | ▶ | use HTTP::Request::Common; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 4 | ▶ | use JSON qw(encode_json); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 4 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | |||||||||||||||
| 24 | 4 | ▶ | use parent qw(WebService::ILS); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 0 | ▶ | sub with_get_request { | ||||||||||||
|
Subroutine
with_get_request: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 27 | 0 | my $self = shift; | |||||||||||||
| 28 | 0 | ▶ | my $callback = shift or croak "No callback"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 0 | ▶ | my $url = shift or croak "No url"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 0 | my $get_params = shift; # hash ref | |||||||||||||
| 31 | |||||||||||||||
| 32 | 0 | my $uri = URI->new($url); | |||||||||||||
| 33 | 0 | ▶ | $uri->query_form($get_params) if $get_params; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | 0 | my $request = HTTP::Request::Common::GET( $uri ); | |||||||||||||
| 35 | 0 | my $response = $self->_request_with_auth($request); | |||||||||||||
| 36 | 0 | return $self->process_json_response($response, $callback); | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | ▶ | sub with_delete_request { | ||||||||||||
|
Subroutine
with_delete_request: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||
| 41 | 0 | ▶ | my $callback = shift or croak "No callback"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | 0 | my $error_callback = shift; | |||||||||||||
| 43 | 0 | ▶ | my $url = shift or croak "No url"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | my $request = HTTP::Request::Common::DELETE( $url ); | |||||||||||||
| 46 | 0 | my $response = $self->_request_with_auth($request); | |||||||||||||
| 47 | 0 | ▶ | return $response->content ? $self->process_json_response($response, $callback) : 1 | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | if $response->is_success; | ||||||||||||||
| 49 | |||||||||||||||
| 50 | return $self->_error_result( | ||||||||||||||
| 51 | 0 | ▶ | sub { $self->process_json_error_response($response, $error_callback); }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 52 | 0 | $request, | |||||||||||||
| 53 | $response | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | sub with_post_request { | ||||||||||||
|
Subroutine
with_post_request: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 58 | 0 | my $self = shift; | |||||||||||||
| 59 | 0 | ▶ | my $callback = shift or croak "No callback"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 0 | ▶ | my $url = shift or croak "No url"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 0 | ▶ | my $post_params = shift || {}; # hash ref | ||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | my $request = HTTP::Request::Common::POST( $url, $post_params ); | |||||||||||||
| 64 | 0 | my $response = $self->_request_with_auth($request); | |||||||||||||
| 65 | 0 | return $self->process_json_response($response, $callback); | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | # This will probably not suit everyone | ||||||||||||||
| 69 | 0 | ▶ | sub with_put_request { | ||||||||||||
|
Subroutine
with_put_request: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||
| 71 | 0 | ▶ | my $callback = shift or croak "No callback"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | ▶ | my $url = shift or croak "No url"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 0 | my $put_params = shift; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | my $request = HTTP::Request::Common::PUT( $url ); | |||||||||||||
| 76 | 0 | my $content; | |||||||||||||
| 77 | 0 | ▶ | if ($put_params) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 0 | my $url = URI->new('http:'); | |||||||||||||
| 79 | 0 | ▶ | $url->query_form(ref($put_params) eq "HASH" ? %$put_params : @$put_params); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 0 | $content = $url->query; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | 0 | ▶ | if( $content ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | # HTML/4.01 says that line breaks are represented as "CR LF" pairs (i.e., `%0D%0A') | ||||||||||||||
| 84 | 0 | $content =~ s/(?<!%0D)%0A/%0D%0A/go; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | $request->content_type("application/x-www-form-urlencoded"); | |||||||||||||
| 87 | 0 | $request->content_length(length $content); | |||||||||||||
| 88 | 0 | $request->content($content); | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | else { | ||||||||||||||
| 91 | 0 | $request->content_length(0); | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | my $response = $self->_request_with_auth($request); | |||||||||||||
| 95 | 0 | return $self->process_json_response($response, $callback); | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | ▶ | sub with_json_request { | ||||||||||||
|
Subroutine
with_json_request: not called CC=5 SCAR=30.5
Pod
undocumented
| |||||||||||||||
| 99 | 0 | my $self = shift; | |||||||||||||
| 100 | 0 | ▶ | my $callback = shift or croak "No callback"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 0 | my $error_callback = shift; | |||||||||||||
| 102 | 0 | ▶ | my $url = shift or croak "No url"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | ▶ | my $post_params = shift || {}; # hashref | ||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 104 | 0 | ▶ | my $method = shift || 'post'; | ||||||||||||
|
Condition0%0/2
shift() || 'post'
MC/DC0%0/1
shift() || 'post'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | my $req_builder = "HTTP::Request::Common::".uc( $method ); | |||||||||||||
| 107 | 4 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 108 | 0 | my $request = $req_builder->( $url ); | |||||||||||||
| 109 | 0 | $self->_json_request_content($request, $post_params); | |||||||||||||
| 110 | 0 | my $response = $self->_request_with_auth($request); | |||||||||||||
| 111 | 0 | return $self->process_json_response($response, $callback, $error_callback); | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | ▶ | sub _json_request_content { | ||||||||||||
|
Subroutine
_json_request_content: not called CC=3 SCAR=24.8
| |||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||
| 116 | 0 | ▶ | my $request = shift or croak "No request"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | 0 | ▶ | my $data = shift or croak "No data"; # hashref | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | $request->header( 'Content-Type' => 'application/json; charset=utf-8' ); | |||||||||||||
| 120 | 0 | $request->content( encode_json($data) ); | |||||||||||||
| 121 | 0 | $request->header( 'Content-Length' => bytes::length($request->content)); | |||||||||||||
| 122 | 0 | return $request; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 4 | 1; | |||||||||||||
| 126 | |||||||||||||||
| 127 | __END__ | ||||||||||||||