| 1 | package Catmandu::CA::API::Request; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | our $VERSION = '0.06'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | ▶ | use Catmandu::CA::API::Login; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | has url => (is => 'ro', required => 1); | |||||||||||||
| 17 | 1 | has url_query => (is => 'ro', required => 1); | |||||||||||||
| 18 | |||||||||||||||
| 19 | 1 | has username => (is => 'ro', required => 1); | |||||||||||||
| 20 | 1 | has password => (is => 'ro', required => 1); | |||||||||||||
| 21 | |||||||||||||||
| 22 | 1 | has lang => (is => 'ro', default => 'nl_NL'); | |||||||||||||
| 23 | |||||||||||||||
| 24 | 1 | has token => (is => 'lazy'); | |||||||||||||
| 25 | 1 | has ua => (is => 'lazy'); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 0 | ▶ | sub _build_token { | ||||||||||||
|
Subroutine
_build_token: not called CC=1 SCAR=6.9
| |||||||||||||||
| 28 | 0 | my $self = shift; | |||||||||||||
| 29 | 0 | my $login = Catmandu::CA::API::Login->new(username => $self->username, password => $self->password, url => $self->url); | |||||||||||||
| 30 | 0 | return $login->token(); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | ▶ | sub _build_ua { | ||||||||||||
|
Subroutine
_build_ua: not called CC=1 SCAR=6.9
| |||||||||||||||
| 34 | 0 | my $self = shift; | |||||||||||||
| 35 | 0 | my $ua = LWP::UserAgent->new( | |||||||||||||
| 36 | agent => sprintf('catmandu-ca/%s', $VERSION) | ||||||||||||||
| 37 | ); | ||||||||||||||
| 38 | 0 | return $ua; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | ▶ | sub get { | ||||||||||||
|
Subroutine
get: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 42 | 0 | my ($self, $query) = @_; | |||||||||||||
| 43 | 0 | my $url_string = '%s/%s?source=%s&authToken=%s&lang=%s'; | |||||||||||||
| 44 | |||||||||||||||
| 45 | # If $self->url_query matches a '?' with no more '/' behind it | ||||||||||||||
| 46 | 0 | ▶ | if ($self->url_query =~ /\?[^\/]/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 0 | $url_string = '%s/%s&source=%s&authToken=%s&lang=%s'; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | my $url = sprintf($url_string, | |||||||||||||
| 51 | $self->url, | ||||||||||||||
| 52 | $self->url_query, | ||||||||||||||
| 53 | $query, | ||||||||||||||
| 54 | $self->token, | ||||||||||||||
| 55 | $self->lang | ||||||||||||||
| 56 | ); | ||||||||||||||
| 57 | 0 | my $response = $self->ua->get($url); | |||||||||||||
| 58 | 0 | ▶ | if ($response->is_success) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 59 | 0 | return decode_json($response->decoded_content); | |||||||||||||
| 60 | } elsif ($response->code == 404) { | ||||||||||||||
| 61 | 0 | return {}; | |||||||||||||
| 62 | } else { | ||||||||||||||
| 63 | 0 | Catmandu::HTTPError->throw({ | |||||||||||||
| 64 | code => $response->code, | ||||||||||||||
| 65 | message => $response->status_line, | ||||||||||||||
| 66 | url => $response->request->uri, | ||||||||||||||
| 67 | method => $response->request->method, | ||||||||||||||
| 68 | request_headers => [], | ||||||||||||||
| 69 | request_body => $response->request->decoded_content, | ||||||||||||||
| 70 | response_headers => [], | ||||||||||||||
| 71 | response_body => $response->decoded_content, | ||||||||||||||
| 72 | }); | ||||||||||||||
| 73 | 0 | return {}; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | ▶ | sub put { | ||||||||||||
|
Subroutine
put: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 78 | 0 | my ($self, $data) = @_; | |||||||||||||
| 79 | 0 | my $url = sprintf('%s/%s&authToken=%s', $self->url, $self->url_query, $self->token); | |||||||||||||
| 80 | 0 | my $response = $self->ua->put($url, Content => $data, Content_type => 'application/json'); | |||||||||||||
| 81 | |||||||||||||||
| 82 | 0 | ▶ | if (!$response->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 0 | Catmandu::HTTPError->throw({ | |||||||||||||
| 84 | code => $response->code, | ||||||||||||||
| 85 | message => $response->status_line, | ||||||||||||||
| 86 | url => $response->request->uri, | ||||||||||||||
| 87 | method => $response->request->method, | ||||||||||||||
| 88 | request_headers => [], | ||||||||||||||
| 89 | request_body => $response->request->decoded_content, | ||||||||||||||
| 90 | response_headers => [], | ||||||||||||||
| 91 | response_body => $response->decoded_content, | ||||||||||||||
| 92 | }); | ||||||||||||||
| 93 | 0 | return 0; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 0 | return 1; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | ▶ | sub delete { | ||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 100 | 0 | my $self = shift; | |||||||||||||
| 101 | 0 | my $url = sprintf('%s/%s&authToken=%s', $self->url, $self->url_query, $self->token); | |||||||||||||
| 102 | 0 | my $response = $self->ua->delete($url); | |||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | ▶ | if (!$response->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | Catmandu::HTTPError->throw({ | |||||||||||||
| 106 | code => $response->code, | ||||||||||||||
| 107 | message => $response->status_line, | ||||||||||||||
| 108 | url => $response->request->uri, | ||||||||||||||
| 109 | method => $response->request->method, | ||||||||||||||
| 110 | request_headers => [], | ||||||||||||||
| 111 | request_body => $response->request->decoded_content, | ||||||||||||||
| 112 | response_headers => [], | ||||||||||||||
| 113 | response_body => $response->decoded_content, | ||||||||||||||
| 114 | }); | ||||||||||||||
| 115 | 0 | return 0; | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | return 1; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | 1 | 1; | |||||||||||||
| 121 | __END__ | ||||||||||||||