| 1 | package UserAgent::Any::JSON; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.036; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use UserAgent::Any::JSON::Response; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use UserAgent::Any::Wrapper 'wrap_get_like_methods', 'wrap_post_like_methods'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 4 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 4 | our $VERSION = 0.05; | |||||||||
| 13 | |||||||||||
| 14 | 4 | extends 'UserAgent::Any'; | |||||||||
| 15 | |||||||||||
| 16 | has _converter => ( | ||||||||||
| 17 | init_arg => 'json', | ||||||||||
| 18 | is => 'ro', | ||||||||||
| 19 | 2 | ▶ | default => sub { require JSON; JSON->new }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 4 | ); | |||||||||
| 21 | |||||||||||
| 22 | # This is not specific to GET and can actually handle any verb that does not | ||||||||||
| 23 | # take a request body. | ||||||||||
| 24 | 4 | ▶ | sub _generate_get_request ($self, $url, @headers) { | ||||||||
|
Subroutine
_generate_get_request: called CC=2 SCAR=6.9
| |||||||||||
| 25 | 4 | ▶ | croak 'Invalid number of arguments, expected an even sized list after the url' if @headers % 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 3 | return _generate_post_request($self, $url, @headers); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | # The only difference with the 'get' version is that this handles the request | ||||||||||
| 30 | # body. | ||||||||||
| 31 | 10 | ▶ | sub _generate_post_request ($self, $url, @args) { | ||||||||
|
Subroutine
_generate_post_request: called CC=3 SCAR=11.0
| |||||||||||
| 32 | 10 | my $body; | |||||||||
| 33 | 10 | ▶ | $body = pop @args if @args % 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | return ( | ||||||||||
| 35 | 10 | ▶ | $url, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 'Accept' => 'application/json', | ||||||||||
| 37 | 'Content-Type' => 'application/json', | ||||||||||
| 38 | @args, | ||||||||||
| 39 | defined $body ? ($self->_converter->encode($body)) : ()); | ||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 10 | ▶ | sub _process_response ($self, $res, @) { | ||||||||
|
Subroutine
_process_response: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 10 | return UserAgent::Any::JSON::Response->new(res => $res, converter => $self->_converter); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 4 | wrap_get_like_methods('UserAgent::Any', \&_generate_get_request, \&_process_response); | |||||||||
| 47 | 4 | wrap_post_like_methods('UserAgent::Any', \&_generate_post_request, \&_process_response); | |||||||||
| 48 | |||||||||||
| 49 | 4 | 1; | |||||||||
| 50 | |||||||||||
| 51 | __END__ | ||||||||||