line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Wedata; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
34704
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
33
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
5
|
1
|
|
|
1
|
|
7
|
use Carp; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
104
|
|
6
|
1
|
|
|
1
|
|
1542
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
110726
|
|
|
1
|
|
|
|
|
37
|
|
7
|
1
|
|
|
1
|
|
1321
|
use JSON::XS; |
|
1
|
|
|
|
|
11393
|
|
|
1
|
|
|
|
|
79
|
|
8
|
1
|
|
|
1
|
|
903
|
use WebService::Wedata::Database; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use version; our $VERSION = qv('0.0.4'); |
11
|
|
|
|
|
|
|
our $URL_BASE = 'http://wedata.net'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
|
|
|
|
|
|
my($class, $api_key) = @_; |
15
|
|
|
|
|
|
|
bless { |
16
|
|
|
|
|
|
|
ua => LWP::UserAgent->new, |
17
|
|
|
|
|
|
|
api_key => $api_key, |
18
|
|
|
|
|
|
|
}, $class; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub get_databases { |
22
|
|
|
|
|
|
|
my($self) = @_; |
23
|
|
|
|
|
|
|
$self->get_database; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub get_database { |
27
|
|
|
|
|
|
|
my($self, $dbname, $page) = @_; |
28
|
|
|
|
|
|
|
my $path = ($dbname) ? "/databases/$dbname.json" : '/databases.json'; |
29
|
|
|
|
|
|
|
$page ||= ''; |
30
|
|
|
|
|
|
|
my $url = $URL_BASE . $path; |
31
|
|
|
|
|
|
|
my $response = $self->{ua}->get($url, page => $page); |
32
|
|
|
|
|
|
|
if ($response->is_success) { |
33
|
|
|
|
|
|
|
my $data = decode_json($response->content); |
34
|
|
|
|
|
|
|
my $parse_response = sub { |
35
|
|
|
|
|
|
|
my($data) = @_; |
36
|
|
|
|
|
|
|
my @required_keys = split / /, $data->{required_keys}; |
37
|
|
|
|
|
|
|
my @optional_keys = (defined $data->{optional_keys}) ? split / /, $data->{optional_keys} : (); |
38
|
|
|
|
|
|
|
my $database = WebService::Wedata::Database->new( |
39
|
|
|
|
|
|
|
ua => $self->{ua}, |
40
|
|
|
|
|
|
|
api_key => $self->{api_key}, |
41
|
|
|
|
|
|
|
name => $data->{name}, |
42
|
|
|
|
|
|
|
description => $data->{description}, |
43
|
|
|
|
|
|
|
resource_url => $data->{resource_url}, |
44
|
|
|
|
|
|
|
required_keys => [@required_keys], |
45
|
|
|
|
|
|
|
optional_keys => [@optional_keys], |
46
|
|
|
|
|
|
|
permit_other_keys => $data->{permit_other_keys}, |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
$database; |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
if ($dbname) { |
51
|
|
|
|
|
|
|
$parse_response->($data); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
|
|
|
|
|
|
my $result = []; |
55
|
|
|
|
|
|
|
foreach my $db (@$data) { |
56
|
|
|
|
|
|
|
push @$result, $parse_response->($db); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
$result; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else { |
62
|
|
|
|
|
|
|
croak 'Faild to get_database:' . $response->status_line; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub create_database { |
67
|
|
|
|
|
|
|
my($self, @params) = @_; |
68
|
|
|
|
|
|
|
my $params = {@params}; |
69
|
|
|
|
|
|
|
croak "require name on create_database\n" unless $params->{name}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $param_description = $params->{description} || ''; |
72
|
|
|
|
|
|
|
my $param_required_keys = join '%20', @{$params->{required_keys}}; |
73
|
|
|
|
|
|
|
my $param_optional_keys = join '%20', @{$params->{optional_keys}}; |
74
|
|
|
|
|
|
|
my $param_permit_other_keys = ($params->{permit_other_keys}) ? 'true' : 'false'; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $url = $URL_BASE . '/databases'; |
77
|
|
|
|
|
|
|
my $content = ''; |
78
|
|
|
|
|
|
|
$content = join '&', |
79
|
|
|
|
|
|
|
"api_key=$self->{api_key}", |
80
|
|
|
|
|
|
|
"database[name]=$params->{name}", |
81
|
|
|
|
|
|
|
"database[description]=$param_description", |
82
|
|
|
|
|
|
|
"database[required_keys]=$param_required_keys", |
83
|
|
|
|
|
|
|
"database[optional_keys=$param_optional_keys", |
84
|
|
|
|
|
|
|
"database[permit_other_keys]=$param_permit_other_keys" |
85
|
|
|
|
|
|
|
; |
86
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
87
|
|
|
|
|
|
|
POST => $url, |
88
|
|
|
|
|
|
|
HTTP::Headers->new( |
89
|
|
|
|
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
90
|
|
|
|
|
|
|
'content-length' => length($content), |
91
|
|
|
|
|
|
|
), |
92
|
|
|
|
|
|
|
$content, |
93
|
|
|
|
|
|
|
); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $response = $self->{ua}->request($req); |
96
|
|
|
|
|
|
|
if ($response->is_success) { |
97
|
|
|
|
|
|
|
my $database = WebService::Wedata::Database->new( |
98
|
|
|
|
|
|
|
ua => $self->{ua}, |
99
|
|
|
|
|
|
|
api_key => $self->{api_key}, |
100
|
|
|
|
|
|
|
name => $params->{name}, |
101
|
|
|
|
|
|
|
description => $params->{description}, |
102
|
|
|
|
|
|
|
required_keys => $params->{required_keys}, |
103
|
|
|
|
|
|
|
optional_keys => $params->{optional_keys}, |
104
|
|
|
|
|
|
|
permit_other_keys => ($params->{permit_other_keys} == 1) ? 1 : 0, |
105
|
|
|
|
|
|
|
resource_url => $response->header('location'), |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
$database; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
|
|
|
|
|
|
croak 'Faild to create_database:' . $response->status_line; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub update_database { |
115
|
|
|
|
|
|
|
my($self, @params) = @_; |
116
|
|
|
|
|
|
|
my $params = {@params}; |
117
|
|
|
|
|
|
|
croak "require name on create_database\n" unless $params->{name}; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$params->{api_key} = $self->{api_key}; |
120
|
|
|
|
|
|
|
$params->{resource_url} = $URL_BASE . '/databases/' . $params->{name}; |
121
|
|
|
|
|
|
|
my $req = WebService::Wedata::Database::_make_update_request($params); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $response = $self->{ua}->request($req); |
124
|
|
|
|
|
|
|
if ($response->is_success) { |
125
|
|
|
|
|
|
|
$self->get_database($params->{name}); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
else { |
128
|
|
|
|
|
|
|
croak 'Faild to update_database:' . $response->status_line; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub delete_database { |
133
|
|
|
|
|
|
|
my($self, @params) = @_; |
134
|
|
|
|
|
|
|
my $params = {@params}; |
135
|
|
|
|
|
|
|
croak "require name on create_database\n" unless $params->{name}; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$params->{api_key} = $self->{api_key}; |
138
|
|
|
|
|
|
|
my $req = WebService::Wedata::Database::_make_delete_request($params); |
139
|
|
|
|
|
|
|
my $response = $self->{ua}->request($req); |
140
|
|
|
|
|
|
|
if ($response->is_success) { |
141
|
|
|
|
|
|
|
return; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
else { |
144
|
|
|
|
|
|
|
croak 'Faild to delete_database:' . $response->status_line; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
150
|
|
|
|
|
|
|
__END__ |