line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Wedata::Database; |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
200
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
4
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
178
|
|
5
|
1
|
|
|
1
|
|
478
|
use JSON::XS; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
6
|
1
|
|
|
1
|
|
868
|
use WebService::Wedata::Item; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use base qw/Class::Accessor::Fast/; |
8
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/ua api_key name description resource_url permit_other_keys updated_at created_at created_by/); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
|
|
|
|
|
|
my($class, %params) = @_; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $self = bless { |
14
|
|
|
|
|
|
|
required_keys => [], |
15
|
|
|
|
|
|
|
optional_keys => [], |
16
|
|
|
|
|
|
|
items => [], |
17
|
|
|
|
|
|
|
}, $class; |
18
|
|
|
|
|
|
|
foreach my $k (qw/ua api_key name description resource_url updated_at created_at created_by/) { |
19
|
|
|
|
|
|
|
$self->set($k, $params{$k}) if ($params{$k}); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
foreach my $k (@{ $params{required_keys} }) { |
22
|
|
|
|
|
|
|
$self->add_required_key($k); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
foreach my $k (@{ $params{optional_keys} }) { |
25
|
|
|
|
|
|
|
$self->add_optional_key($k); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
if ($params{permit_other_keys} == 1 || $params{permit_other_keys} eq 'true') { |
28
|
|
|
|
|
|
|
$self->permit_other_keys(1); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
else { |
31
|
|
|
|
|
|
|
$self->permit_other_keys(0); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
$self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub add_required_key { |
37
|
|
|
|
|
|
|
my($self, $key) = @_; |
38
|
|
|
|
|
|
|
push @{ $self->{required_keys} }, $key; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub add_optional_key { |
42
|
|
|
|
|
|
|
my($self, $key) = @_; |
43
|
|
|
|
|
|
|
push @{ $self->{optional_keys} }, $key; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub update { |
47
|
|
|
|
|
|
|
my $self = shift; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $params = { |
50
|
|
|
|
|
|
|
api_key => $self->api_key, |
51
|
|
|
|
|
|
|
name => $self->name, |
52
|
|
|
|
|
|
|
description => $self->description, |
53
|
|
|
|
|
|
|
required_keys => $self->required_keys, |
54
|
|
|
|
|
|
|
optional_keys => $self->optional_keys, |
55
|
|
|
|
|
|
|
resource_url => $self->resource_url, |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
my $req = _make_update_request($params); |
58
|
|
|
|
|
|
|
my $response = $self->ua->request($req); |
59
|
|
|
|
|
|
|
if ($response->is_success) { |
60
|
|
|
|
|
|
|
return; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
else { |
63
|
|
|
|
|
|
|
croak 'Faild to update database:' . $response->status_line; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _make_update_request { |
68
|
|
|
|
|
|
|
my($params) = @_; |
69
|
|
|
|
|
|
|
my $param_required_keys = join '%20', @{$params->{required_keys}}; |
70
|
|
|
|
|
|
|
my $param_optional_keys = join '%20', @{$params->{optional_keys}}; |
71
|
|
|
|
|
|
|
my $param_permit_other_keys = ($params->{permit_other_keys}) ? 'true' : 'false'; |
72
|
|
|
|
|
|
|
my $content = ''; |
73
|
|
|
|
|
|
|
$content = join '&', |
74
|
|
|
|
|
|
|
"api_key=$params->{api_key}", |
75
|
|
|
|
|
|
|
"database[name]=$params->{name}", |
76
|
|
|
|
|
|
|
"database[description]=$params->{description}", |
77
|
|
|
|
|
|
|
"database[required_keys]=$param_required_keys", |
78
|
|
|
|
|
|
|
"database[optional_keys=$param_optional_keys", |
79
|
|
|
|
|
|
|
"database[permit_other_keys]=$param_permit_other_keys" |
80
|
|
|
|
|
|
|
; |
81
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
82
|
|
|
|
|
|
|
PUT => $params->{resource_url}, |
83
|
|
|
|
|
|
|
HTTP::Headers->new( |
84
|
|
|
|
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
85
|
|
|
|
|
|
|
'content-length' => length($content), |
86
|
|
|
|
|
|
|
), |
87
|
|
|
|
|
|
|
$content, |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
$req; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub delete { |
93
|
|
|
|
|
|
|
my($self, @params) = @_; |
94
|
|
|
|
|
|
|
my $params = {@params}; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
$params->{api_key} = $self->api_key; |
97
|
|
|
|
|
|
|
$params->{name} = $self->name; |
98
|
|
|
|
|
|
|
my $req = _make_delete_request($params); |
99
|
|
|
|
|
|
|
my $response = $self->ua->request($req); |
100
|
|
|
|
|
|
|
if ($response->is_success) { |
101
|
|
|
|
|
|
|
# clean up |
102
|
|
|
|
|
|
|
$self->name(''); |
103
|
|
|
|
|
|
|
$self->description(''); |
104
|
|
|
|
|
|
|
$self->{required_keys} = []; |
105
|
|
|
|
|
|
|
$self->{optional_keys} = []; |
106
|
|
|
|
|
|
|
$self->resource_url(''); |
107
|
|
|
|
|
|
|
$self->updated_at(''); |
108
|
|
|
|
|
|
|
$self->created_at(''); |
109
|
|
|
|
|
|
|
$self->created_by(''); |
110
|
|
|
|
|
|
|
$self->{items} = []; |
111
|
|
|
|
|
|
|
return; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
else { |
114
|
|
|
|
|
|
|
croak 'Faild to delete database:' . $response->status_line; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub _make_delete_request { |
119
|
|
|
|
|
|
|
my($params) = @_; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $content = "api_key=$params->{api_key}"; |
122
|
|
|
|
|
|
|
my $url = $WebService::Wedata::URL_BASE . '/databases/' . $params->{name}; |
123
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
124
|
|
|
|
|
|
|
DELETE => $url, |
125
|
|
|
|
|
|
|
HTTP::Headers->new( |
126
|
|
|
|
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
127
|
|
|
|
|
|
|
'content-length' => length($content), |
128
|
|
|
|
|
|
|
), |
129
|
|
|
|
|
|
|
$content, |
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
$req; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub get_items { |
135
|
|
|
|
|
|
|
my($self) = @_; |
136
|
|
|
|
|
|
|
$self->get_item(database_name => $self->name); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub get_item { |
140
|
|
|
|
|
|
|
my($self, @params) = @_; |
141
|
|
|
|
|
|
|
my $params = {@params}; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $page = ($params->{page}) ? $params->{page} : ''; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $parse_response = sub { |
146
|
|
|
|
|
|
|
my($data) = @_; |
147
|
|
|
|
|
|
|
my $item = WebService::Wedata::Item->new( |
148
|
|
|
|
|
|
|
database => $self, |
149
|
|
|
|
|
|
|
name => $data->{name}, |
150
|
|
|
|
|
|
|
data => $data->{data}, |
151
|
|
|
|
|
|
|
resource_url => $data->{resource_url}, |
152
|
|
|
|
|
|
|
updated_at => $data->{updated_at}, |
153
|
|
|
|
|
|
|
created_at => $data->{created_at}, |
154
|
|
|
|
|
|
|
created_by => $data->{created_by}, |
155
|
|
|
|
|
|
|
); |
156
|
|
|
|
|
|
|
$item; |
157
|
|
|
|
|
|
|
}; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
my $url = $WebService::Wedata::URL_BASE; |
160
|
|
|
|
|
|
|
if ($params->{database_name}) { |
161
|
|
|
|
|
|
|
$url .= '/databases/' . $params->{database_name} . '/items.json'; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
elsif ($params->{id}){ |
164
|
|
|
|
|
|
|
$url .= '/items/' . $params->{id} . '.json'; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
else { |
167
|
|
|
|
|
|
|
croak "specify item_id or database_name"; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $response = $self->ua->get($url, page => $page); |
171
|
|
|
|
|
|
|
if ($response->is_success) { |
172
|
|
|
|
|
|
|
my $data = decode_json($response->content); |
173
|
|
|
|
|
|
|
if ($params->{database_name}) { |
174
|
|
|
|
|
|
|
my $result = []; |
175
|
|
|
|
|
|
|
foreach my $item (@$data) { |
176
|
|
|
|
|
|
|
push @$result, $parse_response->($item); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
$result; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
else { |
181
|
|
|
|
|
|
|
$parse_response->($data); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
else { |
185
|
|
|
|
|
|
|
croak 'Faild to get_item:' . $response->status_line; |
186
|
|
|
|
|
|
|
return; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub create_item { |
191
|
|
|
|
|
|
|
my($self, @params) = @_; |
192
|
|
|
|
|
|
|
my $params = {@params}; |
193
|
|
|
|
|
|
|
croak "require name on create_item\n" unless $params->{name}; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
my $url = $WebService::Wedata::URL_BASE . '/databases/' . $self->name . '/items'; |
196
|
|
|
|
|
|
|
my $kv = []; |
197
|
|
|
|
|
|
|
while (my($k, $v) = each(%{ $params->{data} })) { |
198
|
|
|
|
|
|
|
push @$kv, "data[$k]=$v"; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my $content = ''; |
202
|
|
|
|
|
|
|
$content = join '&', |
203
|
|
|
|
|
|
|
"api_key=" . $self->api_key, |
204
|
|
|
|
|
|
|
"name=$params->{name}", |
205
|
|
|
|
|
|
|
@$kv |
206
|
|
|
|
|
|
|
; |
207
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
208
|
|
|
|
|
|
|
POST => $url, |
209
|
|
|
|
|
|
|
HTTP::Headers->new( |
210
|
|
|
|
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
211
|
|
|
|
|
|
|
'content-length' => length($content), |
212
|
|
|
|
|
|
|
), |
213
|
|
|
|
|
|
|
$content, |
214
|
|
|
|
|
|
|
); |
215
|
|
|
|
|
|
|
my $response = $self->ua->request($req); |
216
|
|
|
|
|
|
|
if ($response->is_success) { |
217
|
|
|
|
|
|
|
my $new_item = WebService::Wedata::Item->new( |
218
|
|
|
|
|
|
|
database => $self, |
219
|
|
|
|
|
|
|
name => $params->{name}, |
220
|
|
|
|
|
|
|
data => $params->{data}, |
221
|
|
|
|
|
|
|
resource_url => $response->header('location'), |
222
|
|
|
|
|
|
|
); |
223
|
|
|
|
|
|
|
$new_item; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
else { |
226
|
|
|
|
|
|
|
croak 'Faild to create_item:' . $response->status_line; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub update_item { |
231
|
|
|
|
|
|
|
my($self, @params) = @_; |
232
|
|
|
|
|
|
|
my $params = {@params}; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
%$params = (%$params, %{ { |
235
|
|
|
|
|
|
|
api_key => $self->api_key, |
236
|
|
|
|
|
|
|
database_name => $self->name, |
237
|
|
|
|
|
|
|
} }); |
238
|
|
|
|
|
|
|
my $req = WebService::Wedata::Item::_make_update_request($params); |
239
|
|
|
|
|
|
|
my $response = $self->ua->request($req); |
240
|
|
|
|
|
|
|
if ($response->is_success) { |
241
|
|
|
|
|
|
|
$self->get_item(id => $params->{id}); |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
else { |
244
|
|
|
|
|
|
|
croak 'Faild to update_item:' . $response->status_line; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub delete_item { |
249
|
|
|
|
|
|
|
my($self, @params) = @_; |
250
|
|
|
|
|
|
|
my $params = {@params}; |
251
|
|
|
|
|
|
|
croak "require item_id on delete_item\n" unless $params->{id}; |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
%$params = (%$params, %{ { |
254
|
|
|
|
|
|
|
api_key => $self->api_key, |
255
|
|
|
|
|
|
|
} }); |
256
|
|
|
|
|
|
|
my $req = WebService::Wedata::Item::_make_delete_request($params); |
257
|
|
|
|
|
|
|
my $response = $self->ua->request($req); |
258
|
|
|
|
|
|
|
if ($response->is_success) { |
259
|
|
|
|
|
|
|
return; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
else { |
262
|
|
|
|
|
|
|
croak 'Faild to delete_item:' . $response->status_line; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
1; |
267
|
|
|
|
|
|
|
__END__ |