line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::CA::API::Request; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
6
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
2
|
use Moo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
176
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
701
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
29549
|
|
|
1
|
|
|
|
|
30
|
|
12
|
1
|
|
|
1
|
|
7
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
10
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
534
|
use Catmandu::CA::API::Login; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
516
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has url => (is => 'ro', required => 1); |
17
|
|
|
|
|
|
|
has url_query => (is => 'ro', required => 1); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has username => (is => 'ro', required => 1); |
20
|
|
|
|
|
|
|
has password => (is => 'ro', required => 1); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has lang => (is => 'ro', default => 'nl_NL'); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has token => (is => 'lazy'); |
25
|
|
|
|
|
|
|
has ua => (is => 'lazy'); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _build_token { |
28
|
0
|
|
|
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
|
|
|
|
|
|
|
sub _build_ua { |
34
|
0
|
|
|
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
|
|
|
|
|
|
|
sub get { |
42
|
0
|
|
|
0
|
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
|
0
|
|
|
|
|
if ($self->url_query =~ /\?[^\/]/) { |
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
|
0
|
|
|
|
|
if ($response->is_success) { |
|
|
0
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub put { |
78
|
0
|
|
|
0
|
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
|
0
|
|
|
|
|
if (!$response->is_success) { |
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
|
|
|
|
|
|
|
sub delete { |
100
|
0
|
|
|
0
|
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
|
0
|
|
|
|
|
if (!$response->is_success) { |
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; |
121
|
|
|
|
|
|
|
__END__ |