line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Store::Resolver::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
3
|
use Moo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
167
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
5
|
1
|
|
|
1
|
|
665
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
30235
|
|
|
1
|
|
|
|
|
33
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has url => (is => 'ro', required => 1); |
10
|
|
|
|
|
|
|
has username => (is => 'ro', required => 1); |
11
|
|
|
|
|
|
|
has password => (is => 'ro', required => 1); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has ua => (is => 'lazy'); |
14
|
|
|
|
|
|
|
has cookie_jar => (is => 'lazy'); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _build_ua { |
17
|
0
|
|
|
0
|
|
|
my $self = shift; |
18
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
19
|
0
|
|
|
|
|
|
$ua->cookie_jar({}); |
20
|
0
|
|
|
|
|
|
return $ua; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _build_cookie_jar { |
24
|
0
|
|
|
0
|
|
|
my $self = shift; |
25
|
0
|
|
|
|
|
|
return $self->login(); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub login { |
29
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
30
|
0
|
|
|
|
|
|
my $auth_url = '%s/resolver/api/login'; |
31
|
0
|
|
|
|
|
|
my $req_url = sprintf($auth_url, $self->url); |
32
|
0
|
|
|
|
|
|
my $form = { |
33
|
|
|
|
|
|
|
'username' => $self->username, |
34
|
|
|
|
|
|
|
'password' => $self->password |
35
|
|
|
|
|
|
|
}; |
36
|
0
|
|
|
|
|
|
my $response = $self->ua->post($req_url, Content => $form); |
37
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
38
|
0
|
|
|
|
|
|
return $self->ua->cookie_jar; |
39
|
|
|
|
|
|
|
} else { |
40
|
0
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
41
|
|
|
|
|
|
|
code => $response->code, |
42
|
|
|
|
|
|
|
message => $response->status_line, |
43
|
|
|
|
|
|
|
url => $response->request->uri, |
44
|
|
|
|
|
|
|
method => $response->request->method, |
45
|
|
|
|
|
|
|
request_headers => [], |
46
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
47
|
|
|
|
|
|
|
response_headers => [], |
48
|
|
|
|
|
|
|
response_body => $response->decoded_content |
49
|
|
|
|
|
|
|
}); |
50
|
0
|
|
|
|
|
|
return undef; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub logout { |
55
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
56
|
0
|
|
|
|
|
|
my $url = '%s/resolver/api/logout'; |
57
|
0
|
|
|
|
|
|
my $req_url = sprintf($url, $self->url); |
58
|
0
|
|
|
|
|
|
$self->ua->cookie_jar($self->cookie_jar); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $response = $self->ua->get($req_url); |
61
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
62
|
0
|
|
|
|
|
|
return 1; |
63
|
|
|
|
|
|
|
} else { |
64
|
0
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
65
|
|
|
|
|
|
|
code => $response->code, |
66
|
|
|
|
|
|
|
message => $response->status_line, |
67
|
|
|
|
|
|
|
url => $response->request->uri, |
68
|
|
|
|
|
|
|
method => $response->request->method, |
69
|
|
|
|
|
|
|
request_headers => [], |
70
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
71
|
|
|
|
|
|
|
response_headers => [], |
72
|
|
|
|
|
|
|
response_body => $response->decoded_content |
73
|
|
|
|
|
|
|
}); |
74
|
0
|
|
|
|
|
|
return undef; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub get { |
79
|
0
|
|
|
0
|
0
|
|
my ($self, $id) = @_; |
80
|
0
|
|
|
|
|
|
my $url = '%s/resolver/api/entity/original/%s'; |
81
|
0
|
|
|
|
|
|
my $req_url = sprintf($url, $self->url, $id); |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
$self->ua->cookie_jar($self->cookie_jar); |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $response = $self->ua->get($req_url); |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
88
|
|
|
|
|
|
|
# New API |
89
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
90
|
|
|
|
|
|
|
} else { |
91
|
|
|
|
|
|
|
# If it is 404, it could be that the "new" api works, |
92
|
|
|
|
|
|
|
# but that the entity doesn't exist. So we check for |
93
|
|
|
|
|
|
|
# the presence of the error "Entity not found" |
94
|
|
|
|
|
|
|
# Also, the new API returns the application/json content-type |
95
|
|
|
|
|
|
|
# while the old doesn't. |
96
|
0
|
0
|
|
|
|
|
if ($response->code == 404) { |
97
|
0
|
0
|
|
|
|
|
if ($response->content_type eq 'application/json') { |
98
|
|
|
|
|
|
|
return { |
99
|
0
|
|
|
|
|
|
'data' => {} |
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Try with the old API |
105
|
|
|
|
|
|
|
# Please note: this doesn't work with the original object number |
106
|
|
|
|
|
|
|
# so it could say 'does not exist' because the original object number |
107
|
|
|
|
|
|
|
# is not equal to the PID |
108
|
0
|
|
|
|
|
|
$url = '%s/resolver/api/entity/%s'; |
109
|
0
|
|
|
|
|
|
$req_url = sprintf($url, $self->url, $id); |
110
|
0
|
|
|
|
|
|
$response = $self->ua->get($req_url); |
111
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
|
|
0
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
113
|
|
|
|
|
|
|
} elsif ($response->code == 404) { |
114
|
|
|
|
|
|
|
return { |
115
|
0
|
|
|
|
|
|
'data' => {} |
116
|
|
|
|
|
|
|
}; |
117
|
|
|
|
|
|
|
} else { |
118
|
|
|
|
|
|
|
# Give up |
119
|
0
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
120
|
|
|
|
|
|
|
code => $response->code, |
121
|
|
|
|
|
|
|
message => $response->status_line, |
122
|
|
|
|
|
|
|
url => $response->request->uri, |
123
|
|
|
|
|
|
|
method => $response->request->method, |
124
|
|
|
|
|
|
|
request_headers => [], |
125
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
126
|
|
|
|
|
|
|
response_headers => [], |
127
|
|
|
|
|
|
|
response_body => $response->decoded_content |
128
|
|
|
|
|
|
|
}); |
129
|
0
|
|
|
|
|
|
return undef; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub post { |
135
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
136
|
0
|
|
|
|
|
|
my $json_data = encode_json($data); |
137
|
0
|
|
|
|
|
|
my $url = '%s/resolver/api/entity'; |
138
|
0
|
|
|
|
|
|
my $req_url = sprintf($url, $self->url); |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
$self->ua->cookie_jar($self->login()); |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
my $response = $self->ua->post($req_url, Content_Type => 'application/json', Content => $json_data); |
143
|
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
145
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
146
|
|
|
|
|
|
|
} else { |
147
|
0
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
148
|
|
|
|
|
|
|
code => $response->code, |
149
|
|
|
|
|
|
|
message => $response->status_line, |
150
|
|
|
|
|
|
|
url => $response->request->uri, |
151
|
|
|
|
|
|
|
method => $response->request->method, |
152
|
|
|
|
|
|
|
request_headers => [], |
153
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
154
|
|
|
|
|
|
|
response_headers => [], |
155
|
|
|
|
|
|
|
response_body => $response->decoded_content |
156
|
|
|
|
|
|
|
}); |
157
|
0
|
|
|
|
|
|
return undef; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub put { |
163
|
0
|
|
|
0
|
0
|
|
my ($self, $id, $data) = @_; |
164
|
0
|
|
|
|
|
|
my $json_data = encode_json($data); |
165
|
0
|
|
|
|
|
|
my $url = '%s/resolver/api/entity/%s'; |
166
|
0
|
|
|
|
|
|
my $req_url = sprintf($url, $self->url, $id); |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
$self->ua->cookie_jar($self->login()); |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $response = $self->ua->put($req_url, Content_Type => 'application/json', Content => $json_data); |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
173
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
174
|
|
|
|
|
|
|
} else { |
175
|
0
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
176
|
|
|
|
|
|
|
code => $response->code, |
177
|
|
|
|
|
|
|
message => $response->status_line, |
178
|
|
|
|
|
|
|
url => $response->request->uri, |
179
|
|
|
|
|
|
|
method => $response->request->method, |
180
|
|
|
|
|
|
|
request_headers => [], |
181
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
182
|
|
|
|
|
|
|
response_headers => [], |
183
|
|
|
|
|
|
|
response_body => $response->decoded_content |
184
|
|
|
|
|
|
|
}); |
185
|
0
|
|
|
|
|
|
return undef; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub delete { |
190
|
0
|
|
|
0
|
0
|
|
my ($self, $id) = @_; |
191
|
0
|
|
|
|
|
|
my $url = '%s/resolver/api/entity/%s'; |
192
|
0
|
|
|
|
|
|
my $req_url = sprintf($url, $self->url, $id); |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
$self->ua->cookie_jar($self->login()); |
195
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
my $response = $self->ua->delete($req_url); |
197
|
|
|
|
|
|
|
|
198
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
199
|
0
|
|
|
|
|
|
return $response->decoded_content; |
200
|
|
|
|
|
|
|
} else { |
201
|
0
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
202
|
|
|
|
|
|
|
code => $response->code, |
203
|
|
|
|
|
|
|
message => $response->status_line, |
204
|
|
|
|
|
|
|
url => $response->request->uri, |
205
|
|
|
|
|
|
|
method => $response->request->method, |
206
|
|
|
|
|
|
|
request_headers => [], |
207
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
208
|
|
|
|
|
|
|
response_headers => [], |
209
|
|
|
|
|
|
|
response_body => $response->decoded_content |
210
|
|
|
|
|
|
|
}); |
211
|
0
|
|
|
|
|
|
return undef; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
1; |