line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
2
|
|
|
|
|
|
|
package WebService::NFSN::Object; |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Copyright 2010 Christopher J. Madsen |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Author: Christopher J. Madsen |
7
|
|
|
|
|
|
|
# Created: 3 Apr 2007 |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify |
10
|
|
|
|
|
|
|
# it under the same terms as Perl itself. |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
13
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the |
15
|
|
|
|
|
|
|
# GNU General Public License or the Artistic License for more details. |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# ABSTRACT: Base class for NFSN API objects |
18
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
722
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
21
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
77
|
|
22
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
23
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
24
|
1
|
|
|
1
|
|
890
|
use HTTP::Request::Common qw(GET POST PUT); |
|
1
|
|
|
|
|
2392
|
|
|
1
|
|
|
|
|
132
|
|
25
|
1
|
|
|
1
|
|
7
|
use URI 1.00 (); |
|
1
|
|
|
|
|
29
|
|
|
1
|
|
|
|
|
29
|
|
26
|
1
|
|
|
1
|
|
6
|
use WebService::NFSN 0.10 qw(_eval_or_die); |
|
1
|
|
|
|
|
22
|
|
|
1
|
|
|
|
|
363
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#===================================================================== |
29
|
|
|
|
|
|
|
# Package Global Variables: |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = '1.03'; # VERSION |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
#===================================================================== |
34
|
|
|
|
|
|
|
sub get_converter # ($function) |
35
|
|
|
|
|
|
|
{ |
36
|
26
|
100
|
|
26
|
0
|
241
|
my $convert = ($_[0] =~ s/:JSON$// |
37
|
|
|
|
|
|
|
? 'WebService::NFSN::decode_json' |
38
|
|
|
|
|
|
|
: ''); |
39
|
|
|
|
|
|
|
|
40
|
26
|
|
|
|
|
43
|
return $convert; |
41
|
|
|
|
|
|
|
} # end get_converter |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
44
|
|
|
|
|
|
|
# Generate the code for an API module: |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _define |
47
|
|
|
|
|
|
|
{ |
48
|
5
|
|
|
5
|
|
29
|
my ($class, %p) = @_; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#................................................................... |
51
|
|
|
|
|
|
|
# Create the object_type method for classifying objects: |
52
|
|
|
|
|
|
|
|
53
|
5
|
|
|
|
|
20
|
my $code = "package $class;\nsub object_type { '$p{type}' }\n"; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
#................................................................... |
56
|
|
|
|
|
|
|
# Create an accessor method for each property: |
57
|
|
|
|
|
|
|
|
58
|
5
|
|
|
|
|
13
|
foreach my $propType (qw(rw ro wo)) { |
59
|
15
|
|
|
|
|
33
|
my $properties = $p{$propType}; |
60
|
|
|
|
|
|
|
|
61
|
15
|
100
|
|
|
|
41
|
next unless $properties; |
62
|
|
|
|
|
|
|
|
63
|
5
|
|
|
|
|
12
|
foreach my $property (@$properties) { |
64
|
14
|
|
|
|
|
25
|
my $convert = get_converter($property); |
65
|
|
|
|
|
|
|
|
66
|
14
|
|
|
|
|
53
|
$code .= <<"END PROPERTY"; |
67
|
|
|
|
|
|
|
sub $property |
68
|
|
|
|
|
|
|
{ |
69
|
|
|
|
|
|
|
$convert shift->${propType}_property('$property' => \@_); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
END PROPERTY |
72
|
|
|
|
|
|
|
} # end foreach $property |
73
|
|
|
|
|
|
|
} # end foreach $propType |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
#................................................................... |
76
|
|
|
|
|
|
|
# Create an object method for each API method: |
77
|
|
|
|
|
|
|
|
78
|
5
|
100
|
|
|
|
18
|
if (my $methods = $p{methods}) { |
79
|
4
|
|
|
|
|
21
|
while (my ($method, $params) = each %$methods) { |
80
|
12
|
|
|
|
|
20
|
my $convert = get_converter($method); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Process method prototype: |
83
|
12
|
|
|
|
|
18
|
my (%accepted, @required); |
84
|
12
|
|
|
|
|
21
|
foreach (@$params) { |
85
|
18
|
100
|
|
|
|
59
|
push @required, $_ unless s/\?$//; |
86
|
18
|
|
|
|
|
43
|
$accepted{$_} = 1; |
87
|
|
|
|
|
|
|
} # end foreach parameter declaration |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Store method prototype into package variable: |
90
|
1
|
|
|
1
|
|
7
|
{ no strict 'refs'; ## no critic ProhibitNoStrict |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
828
|
|
|
12
|
|
|
|
|
19
|
|
91
|
12
|
|
|
|
|
19
|
@{ sprintf '%s::_%s_prototype', $class, $method } |
|
12
|
|
|
|
|
100
|
|
92
|
|
|
|
|
|
|
= ($method, \%accepted, \@required) } |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Define the method: |
95
|
12
|
|
|
|
|
74
|
$code .= <<"END METHOD"; |
96
|
|
|
|
|
|
|
our \@_${method}_prototype; |
97
|
|
|
|
|
|
|
sub $method |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
$convert shift->POST_request(\@_${method}_prototype, \@_); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
END METHOD |
102
|
|
|
|
|
|
|
} # end while each method |
103
|
|
|
|
|
|
|
} # end if methods |
104
|
|
|
|
|
|
|
|
105
|
5
|
|
|
|
|
21
|
_eval_or_die $code; |
106
|
|
|
|
|
|
|
} # end _define |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
#===================================================================== |
109
|
|
|
|
|
|
|
sub new |
110
|
|
|
|
|
|
|
{ |
111
|
0
|
|
|
0
|
0
|
|
my ($class, $manager, $id) = @_; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
return bless { manager => $manager, |
114
|
|
|
|
|
|
|
id => $id, |
115
|
|
|
|
|
|
|
}, $class; |
116
|
|
|
|
|
|
|
} # end new |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
119
|
|
|
|
|
|
|
sub GET_request |
120
|
|
|
|
|
|
|
{ |
121
|
0
|
|
|
0
|
0
|
|
my ($self, $property) = @_; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $self->make_request(GET $self->make_uri($property)); |
124
|
|
|
|
|
|
|
} # end GET_request |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
127
|
|
|
|
|
|
|
sub PUT_request |
128
|
|
|
|
|
|
|
{ |
129
|
0
|
|
|
0
|
0
|
|
my ($self, $property, $value) = @_; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
return $self->make_request(PUT $self->make_uri($property), |
132
|
|
|
|
|
|
|
Content => $value); |
133
|
|
|
|
|
|
|
} # end PUT_request |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
136
|
|
|
|
|
|
|
sub POST_request |
137
|
|
|
|
|
|
|
{ |
138
|
0
|
|
|
0
|
0
|
|
my ($self, $method, $accepted, $required, %param) = @_; |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
foreach my $key (@$required) { |
141
|
0
|
0
|
|
|
|
|
croak(qq'Missing required "$key" parameter for $method') |
142
|
|
|
|
|
|
|
unless defined $param{$key}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
foreach my $key (keys %param) { |
146
|
0
|
0
|
|
|
|
|
carp(qq'"$key" is not a parameter of $method') unless $accepted->{$key}; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
return $self->make_request(POST $self->make_uri($method), \%param); |
150
|
|
|
|
|
|
|
} # end POST_request |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
153
|
|
|
|
|
|
|
sub make_request |
154
|
|
|
|
|
|
|
{ |
155
|
0
|
|
|
0
|
0
|
|
my $self = shift @_; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
my $res = $self->{manager}->make_request(@_); |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
return $res->content; |
160
|
|
|
|
|
|
|
} # end make_request |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
163
|
|
|
|
|
|
|
sub make_uri |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
0
|
0
|
|
my ($self, $name) = @_; |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
URI->new(join('/', $self->{manager}->root_url, $self->object_type, |
168
|
|
|
|
|
|
|
$self->{id}, $name)); |
169
|
|
|
|
|
|
|
} # end make_url |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
172
|
|
|
|
|
|
|
sub ro_property |
173
|
|
|
|
|
|
|
{ |
174
|
0
|
|
|
0
|
0
|
|
my ($self, $property) = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
0
|
|
|
|
|
croak "$property is read-only" if @_ > 2; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
return $self->GET_request($property); |
179
|
|
|
|
|
|
|
} # end ro_property |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
182
|
|
|
|
|
|
|
sub rw_property |
183
|
|
|
|
|
|
|
{ |
184
|
0
|
|
|
0
|
0
|
|
my ($self, $property, $value) = @_; |
185
|
|
|
|
|
|
|
|
186
|
0
|
0
|
|
|
|
|
if (@_ > 2) { |
187
|
0
|
|
|
|
|
|
return $self->PUT_request($property, $value); |
188
|
|
|
|
|
|
|
} else { |
189
|
0
|
|
|
|
|
|
return $self->GET_request($property); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} # end rw_property |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
194
|
|
|
|
|
|
|
sub wo_property |
195
|
|
|
|
|
|
|
{ |
196
|
0
|
|
|
0
|
0
|
|
my ($self, $property, $value) = @_; |
197
|
|
|
|
|
|
|
|
198
|
0
|
0
|
|
|
|
|
croak "$property is write-only" if @_ < 3; |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
return $self->PUT_request($property, $value); |
201
|
|
|
|
|
|
|
} # end wo_property |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
#===================================================================== |
204
|
|
|
|
|
|
|
# Package Return Value: |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
__END__ |