line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Store::Datahub::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
327
|
use Catmandu; |
|
1
|
|
|
|
|
79044
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
195
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
8
|
1
|
|
|
1
|
|
642
|
use JSON; |
|
1
|
|
|
|
|
5473
|
|
|
1
|
|
|
|
|
4
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
100
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
839
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has url => (is => 'ro', required => 1); |
13
|
|
|
|
|
|
|
has client_id => (is => 'ro', required => 1); |
14
|
|
|
|
|
|
|
has client_secret => (is => 'ro', required => 1); |
15
|
|
|
|
|
|
|
has username => (is => 'ro', required => 1); |
16
|
|
|
|
|
|
|
has password => (is => 'ro', required => 1); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has client => (is => 'lazy'); |
19
|
|
|
|
|
|
|
has access_token => ( |
20
|
|
|
|
|
|
|
is => 'lazy', |
21
|
|
|
|
|
|
|
writer => '_set_access_token', |
22
|
|
|
|
|
|
|
builder => '_build_access_token' |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _build_client { |
26
|
0
|
|
|
0
|
|
|
my $self = shift; |
27
|
0
|
|
|
|
|
|
return LWP::UserAgent->new(keep_alive => 1); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _build_access_token { |
31
|
0
|
|
|
0
|
|
|
my $self = shift; |
32
|
0
|
|
|
|
|
|
return $self->generate_token(); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub set_access_token { |
36
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
37
|
|
|
|
|
|
|
# Used to regenerate the token when it becomes invalid |
38
|
0
|
|
|
|
|
|
return $self->_set_access_token($self->generate_token()); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub generate_token { |
42
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
43
|
0
|
|
|
|
|
|
my $oauth = Catmandu::Store::Datahub::OAuth->new(username => $self->username, password => $self->password, client_id => $self->client_id, client_secret => $self->client_secret, url => $self->url); |
44
|
0
|
|
|
|
|
|
return $oauth->token(); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub get { |
48
|
0
|
|
|
0
|
0
|
|
my ($self, $id) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
|
if (not defined $id) { |
51
|
0
|
|
|
|
|
|
Catmandu::BadArg->throw( |
52
|
|
|
|
|
|
|
'message' => "Record could not be retrieved. Missing 'id'." |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $url = sprintf('%s/api/v1/data/%s', $self->url, $id); |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $response = $self->client->get($url, Authorization => sprintf('Bearer %s', $self->access_token)); |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
62
|
|
|
|
|
|
|
} elsif ($response->code == 401) { |
63
|
0
|
|
|
|
|
|
my $error = decode_json($response->decoded_content); |
64
|
0
|
0
|
|
|
|
|
if ($error->{'error_description'} eq 'The access token provided has expired.') { |
65
|
0
|
|
|
|
|
|
$self->set_access_token(); |
66
|
0
|
|
|
|
|
|
return $self->get($id); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} elsif ($response->code == 404) { |
69
|
0
|
|
|
|
|
|
return {}; |
70
|
|
|
|
|
|
|
} else { |
71
|
0
|
|
|
|
|
|
my $message = decode_json($response->decoded_content); |
72
|
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
73
|
|
|
|
|
|
|
code => $response->code, |
74
|
|
|
|
|
|
|
message => $message->{error}->{msg}, |
75
|
0
|
|
|
|
|
|
url => $response->request->uri->as_string, |
76
|
|
|
|
|
|
|
method => $response->request->method, |
77
|
|
|
|
|
|
|
request_headers => [], |
78
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
79
|
|
|
|
|
|
|
response_headers => [], |
80
|
|
|
|
|
|
|
response_body => $response->decoded_content, |
81
|
|
|
|
|
|
|
}); |
82
|
0
|
|
|
|
|
|
return undef; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub add { |
87
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
88
|
0
|
|
|
|
|
|
my $url = sprintf('%s/api/v1/data.lidoxml', $self->url); |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
my $token = $self->access_token; |
91
|
0
|
|
|
|
|
|
my $response; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
$response = $self->client->post($url, Content_Type => 'application/lido+xml', Authorization => sprintf('Bearer %s', $token), Content => $data); |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
|
|
0
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return $response->decoded_content; |
97
|
|
|
|
|
|
|
} elsif ($response->code == 401) { |
98
|
0
|
|
|
|
|
|
my $error = decode_json($response->decoded_content); |
99
|
0
|
0
|
|
|
|
|
if ($error->{'error_description'} eq 'The access token provided has expired.') { |
100
|
0
|
|
|
|
|
|
$self->set_access_token(); |
101
|
0
|
|
|
|
|
|
return $self->add($data); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} else { |
104
|
0
|
|
|
|
|
|
my $message = decode_json($response->decoded_content); |
105
|
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
106
|
|
|
|
|
|
|
code => $response->code, |
107
|
|
|
|
|
|
|
message => $message->{error}->{msg}, |
108
|
0
|
|
|
|
|
|
url => $response->request->uri->as_string, |
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 undef; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub update { |
120
|
0
|
|
|
0
|
0
|
|
my ($self, $id, $data) = @_; |
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
if (not defined $id) { |
123
|
0
|
|
|
|
|
|
Catmandu::BadArg->throw( |
124
|
|
|
|
|
|
|
'message' => "Record could not be updated. Missing 'id'." |
125
|
|
|
|
|
|
|
); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $url = sprintf('%s/api/v1/data/%s', $self->url, $id); |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
my $token = $self->access_token; |
131
|
0
|
|
|
|
|
|
my $response; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$response = $self->client->put($url, Content_Type => 'application/lido+xml', Authorization => sprintf('Bearer %s', $token), Content => $data); |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
|
|
0
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
return $response->decoded_content; |
137
|
|
|
|
|
|
|
} elsif ($response->code == 401) { |
138
|
0
|
|
|
|
|
|
my $error = decode_json($response->decoded_content); |
139
|
0
|
0
|
|
|
|
|
if ($error->{'error_description'} eq 'The access token provided has expired.') { |
140
|
0
|
|
|
|
|
|
$self->set_access_token(); |
141
|
0
|
|
|
|
|
|
return $self->update($id, $data); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} else { |
144
|
0
|
|
|
|
|
|
my $message = decode_json($response->decoded_content); |
145
|
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
146
|
|
|
|
|
|
|
code => $response->code, |
147
|
|
|
|
|
|
|
message => $message->{error}->{msg}, |
148
|
0
|
|
|
|
|
|
url => $response->request->uri->as_string, |
149
|
|
|
|
|
|
|
method => $response->request->method, |
150
|
|
|
|
|
|
|
request_headers => [], |
151
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
152
|
|
|
|
|
|
|
response_headers => [], |
153
|
|
|
|
|
|
|
response_body => $response->decoded_content, |
154
|
|
|
|
|
|
|
}); |
155
|
0
|
|
|
|
|
|
return undef; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub delete { |
160
|
0
|
|
|
0
|
0
|
|
my ($self, $id) = @_; |
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
if (not defined $id) { |
163
|
0
|
|
|
|
|
|
Catmandu::BadArg->throw( |
164
|
|
|
|
|
|
|
'message' => "Record could not be deleted. Missing 'id'." |
165
|
|
|
|
|
|
|
); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
my $url = sprintf('%s/api/v1/data/%s', $self->url, $id); |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $token = $self->access_token; |
171
|
0
|
|
|
|
|
|
my $response; |
172
|
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
|
$response = $self->client->delete($url, Authorization => sprintf('Bearer %s', $token)); |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
|
|
0
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
return $response->decoded_content; |
177
|
|
|
|
|
|
|
} elsif ($response->code == 401) { |
178
|
0
|
|
|
|
|
|
my $error = decode_json($response->decoded_content); |
179
|
0
|
0
|
|
|
|
|
if ($error->{'error_description'} eq 'The access token provided has expired.') { |
180
|
0
|
|
|
|
|
|
$self->set_access_token(); |
181
|
0
|
|
|
|
|
|
return $self->delete($id); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} else { |
184
|
0
|
|
|
|
|
|
my $message = decode_json($response->decoded_content); |
185
|
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
186
|
|
|
|
|
|
|
code => $response->code, |
187
|
|
|
|
|
|
|
message => $message->{error}->{msg}, |
188
|
0
|
|
|
|
|
|
url => $response->request->uri->as_string, |
189
|
|
|
|
|
|
|
method => $response->request->method, |
190
|
|
|
|
|
|
|
request_headers => [], |
191
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
192
|
|
|
|
|
|
|
response_headers => [], |
193
|
|
|
|
|
|
|
response_body => $response->decoded_content, |
194
|
|
|
|
|
|
|
}); |
195
|
0
|
|
|
|
|
|
return undef; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub list { |
200
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
201
|
0
|
|
|
|
|
|
my $url = sprintf('%s/api/v1/data', $self->url); |
202
|
|
|
|
|
|
|
|
203
|
0
|
|
|
|
|
|
my $token = $self->access_token; |
204
|
0
|
|
|
|
|
|
my $response = $self->client->get($url, Authorization => sprintf('Bearer %s', $token)); |
205
|
|
|
|
|
|
|
|
206
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
207
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
208
|
|
|
|
|
|
|
} else { |
209
|
0
|
|
|
|
|
|
my $message = decode_json($response->decoded_content); |
210
|
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
211
|
|
|
|
|
|
|
code => $response->code, |
212
|
|
|
|
|
|
|
message => $message->{error}->{msg}, |
213
|
0
|
|
|
|
|
|
url => $response->request->uri->as_string, |
214
|
|
|
|
|
|
|
method => $response->request->method, |
215
|
|
|
|
|
|
|
request_headers => [], |
216
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
217
|
|
|
|
|
|
|
response_headers => [], |
218
|
|
|
|
|
|
|
response_body => $response->decoded_content, |
219
|
|
|
|
|
|
|
}); |
220
|
0
|
|
|
|
|
|
return undef; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
1; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
__END__ |