blib/lib/WWW/GoodData.pm

filter: Statementstmt 6.9% 18 / 261 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 12.2% 5 / 41 Podpod 89.6% 26 / 29 totaltotal 9.4% 49 / 518 SCAR 85.9
CC
78
Cov
4%
get_canonical_links
380.0
create_report_definition
56.0
upload
42.0
CRAP 5404.2
↑ Summary blib/lib/WWW/GoodData/Agent.pm »
1 package WWW::GoodData;
2  
3 =head1 NAME
4  
5 WWW::GoodData - Client library for GoodData REST-ful API
6  
7 =head1 SYNOPSIS
8  
9 use WWW::GoodData;
10 my $gdc = new WWW::GoodData;
11 print $gdc->get_uri ('md', { title => 'My Project' });
12  
13 =head1 DESCRIPTION
14  
15 B<WWW::GoodData> is the client for GoodData JSON-based API
16 built atop L<WWW::GoodData::Agent> client agent, with focus
17 on usefullness and correctness of implementation.
18  
19 It provides code for navigating the REST-ful API structure as well as
20 wrapper funcitons for common actions.
21  
22 =cut
23  
24 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use WWW::GoodData::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 our $root = new URI ('https://secure.gooddata.com/gdc');
32  
33 =head1 METHODS
34  
35 =over 4
36  
37 =item B<new> [PARAMS]
38  
39 Create a new client instance.
40  
41 You can optionally pass a hash reference with properties that would be
42 blessed, otherwise a new one is created. Possible properties include:
43  
44 =over 8
45  
46 =item B<agent>
47  
48 A L<WWW::GoodData::Agent> instance to use.
49  
50 =item B<retries>
51  
52 A number of retries to obtain results of asynchronous tasks, such as
53 report exports or data uploads. See B<poll>.
54  
55 Defaults to 3600 (delay of one hour).
56  
57 =back
58  
59 =cut
60  
61 sub new
62 {
63 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
64 0 ▶ my $self = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
65 0 bless $self, $class;
66 0 ▶ $self->{agent} ||= new WWW::GoodData::Agent ($root);
Condition0%0/3
$self->{"agent"} ||= "WWW::GoodData::Agent"->new($WWW::GoodData::root)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"agent"} ||= "WWW::GoodData::Agent"->new($WWW::GoodData::root)
$self->{"agent"} "WWW::GoodData::Agent"->new($WWW::GoodData::root)
Truth table0%0/3
A ||= B
A= $self->{"agent"}B= "WWW::GoodData::Agent"->new($WWW::GoodData::root)
ABresult
1X1
011
000
67 0 ▶ $self->{retries} ||= 3600;
Condition0%0/2
$self->{"retries"} ||= 3600
l!l
00
MC/DC0%0/1
$self->{"retries"} ||= 3600
$self->{"retries"}
Truth table0%0/2
A
A= $self->{"retries"}
Aresult
11
00
68 0 return $self;
69 }
70  
71 # API hierarchy traversal Cache
72 1 our %links;
73 sub get_canonical_links
74 {
75 0 ▶ my $self = shift;
Subroutine get_canonical_links: not called CC=19 SCAR=59.4
Pod undocumented
76 0 my $root = shift;
77 0 ▶ my @path = map { ref $_ ? $_ : { category => $_ } } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
78 0 my $link = shift @path;
79  
80 0 ▶ unless ($links{$root}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($WWW::GoodData::links{$root}) 0 0 0
81 0 my $response = $self->{agent}->get ($root);
82 # Various ways to get the links
83 0 ▶ if (exists $response->{about}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $response->{'about'}) { } 0 0 0
elsif (exists $response->{'query'} and exists $response->{'query'}{'entries'}) { } 0 0 0
elsif (scalar keys %$response == 1) { } 0 0 0
84 # Ordinary structure with about section
85 0 $links{$root} = $response->{about}{links};
86 ▶ } elsif (exists $response->{query} and exists $response->{query}{entries}) {
Condition0%0/3
exists $response->{"query"} and exists $response->{"query"}{"entries"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $response->{"query"} and exists $response->{"query"}{"entries"}
exists $response->{"query"} exists $response->{"query"}{"entries"}
Truth table0%0/3
A and B
A= exists $response->{"query"}B= exists $response->{"query"}{"entries"}
ABresult
0X0
100
111
87 # Inconsistent query entries
88 0 $links{$root} = $response->{query}{entries};
89 } elsif (scalar keys %$response == 1) {
90 0 my @elements = ($response);
91 0 my ($structure) = keys %$response;
92  
93 # Aggregated resources (/gdc/account/profile/666/projects)
94 0 @elements = @{$response->{$structure}}
95 0 ▶ if ref $response->{$structure} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $response->{$structure} eq "ARRAY" 0 0 0
96  
97 0 $links{$root} = [];
98 0 foreach my $element (@elements) {
99 0 my $root = $root;
100 0 my ($type) = keys %$element;
101  
102 # Metadata with interesting information outside "links"
103 0 ▶ if (exists $element->{$type}{links}{self}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $element->{$type}{"links"}{"self"} and exists $element->{$type}{"meta"}) 0 0 0
Condition0%0/3
exists $element->{$type}{"links"}{"self"} and exists $element->{$type}{"meta"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $element->{$type}{"links"}{"self"} and exists $element->{$type}{"meta"}
exists $element->{$type}{"links"}{"self"} exists $element->{$type}{"meta"}
Truth table0%0/3
A and B
A= exists $element->{$type}{"links"}{"self"}B= exists $element->{$type}{"meta"}
ABresult
0X0
100
111
104 and exists $element->{$type}{meta}) {
105 0 my $link = new URI ($element->{$type}{links}{self})->abs ($root);
106 0 push @{$links{$root}}, {
107 0 %{$element->{$type}{meta}},
108 category => $type,
109 structure => $structure,
110 link => $link,
111 };
112 0 $root = $link;
113 }
114  
115 # The links themselves
116 0 foreach my $category (keys %{$element->{$type}{links}}) {
117 0 my $link = new URI ($element->{$type}{links}{$category})->abs ($root);
118 0 push @{$links{$root}}, {
119 structure => $structure,
120 category => $category,
121 type => $type,
122 link => $link,
123 };
124 }
125 }
126  
127 } else {
128 0 die 'No links';
129 }
130 }
131  
132 # Canonicalize the links
133 0 $_->{link} = new URI ($_->{link})->abs ($root) foreach @{$links{$root}};
134  
135 my @matches = grep {
136 0 my $this_link = $_;
137 # Filter out those, who lack any of our keys or
138 # hold a different value for it.
139 0 not map { not exists $link->{$_}
140 or not exists $this_link->{$_}
141 0 ▶ or $link->{$_} ne $this_link->{$_}
Branch: 2 branches
BranchTrueFalseTotal
not exists $link->{$_} && exists $this_link->{$_} or $link->{$_} ne $this_link->{$_} ? 1 : () 0 0 0
$link->{$_} ne $this_link->{$_} ? : 0 0 0
Condition0%0/3
exists $link->{$_} && exists $this_link->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $link->{$_} && exists $this_link->{$_}
exists $link->{$_} exists $this_link->{$_}
Truth table0%0/3
A && B
A= exists $link->{$_}B= exists $this_link->{$_}
ABresult
0X0
100
111
142 ? 1 : () } keys %$link
143 0 } @{$links{$root}};
144  
145 # Fully resolved
146 0 ▶ return @matches unless @path;
Branch: 1 branch
BranchTrueFalseTotal
unless @path 0 0 0
147  
148 0 ▶ die 'Nonexistent component in path' unless @matches;
Branch: 1 branch
BranchTrueFalseTotal
unless @matches 0 0 0
149 0 ▶ die 'Ambigious path' unless scalar @matches == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @matches == 1 0 0 0
150  
151 # Traverse further
152 0 return $self->get_canonical_links ($matches[0]->{link}, @path);
153 }
154  
155 # This is a 'normalized' version, for convenience and compatibility
156 sub get_links
157 {
158 0 ▶ my $self = shift;
Subroutine get_links: not called CC=5 SCAR=34.0
Pod undocumented
159 0 ▶ my $root = (ref $_[0] and ref $_[0] ne 'HASH') ? shift : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] ne 'HASH' ? : 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] ne "HASH"
ref $_[0] ref $_[0] ne "HASH"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] ne "HASH"
ABresult
0X0
100
111
160  
161 # Canonicalize URIs
162 0 $root = new URI ($root)->abs ($self->{agent}{root});
163  
164 # And decanonicalize, ommiting the scheme and authority part if possible
165 0 my @links = $self->get_canonical_links ($root, @_);
166 $_->{link} = $_->{link}->rel ($root)->authority
167 0 ▶ ? $_->{link} : new URI ($_->{link}->path) foreach @links;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'link'}->rel($root)->authority ? : 0 0 0
168  
169 0 return @links;
170 }
171  
172 =item B<links> PATH
173  
174 Traverse the links in resource hierarchy following given PATH,
175 starting from API root (L</gdc> by default).
176  
177 PATH is an array of dictionaries, where each key-value pair
178 matches properties of a link. If a plain string is specified,
179 it is considered to be a match against B<category> property:
180  
181 $gdc->links ('md', { 'category' => 'projects' });
182  
183 The above call returns a list of all projects, with links to
184 their metadata resources.
185  
186 =cut
187  
188 sub links
189 {
190 0 ▶ my @links = get_links @_;
Subroutine links: not called CC=2 SCAR=17.9
Pod documented
191 0 ▶ return @links if @links;
Branch: 1 branch
BranchTrueFalseTotal
if @links 0 0 0
192 0 %links = ();
193 0 return get_links @_;
194 }
195  
196 =item B<get_uri> PATH
197  
198 Follows the same samentics as B<links>() call, but returns an
199 URI of the first matching resource instead of complete link
200 structure.
201  
202 =cut
203  
204 sub get_uri
205 {
206 0 ▶ [links @_]->[0]{link};
Subroutine get_uri: not called CC=1 SCAR=6.9
Pod documented
207 }
208  
209 =item B<login> EMAIL PASSWORD
210  
211 Obtain a SST (login token).
212  
213 =cut
214  
215 sub login
216 {
217 0 ▶ my $self = shift;
Subroutine login: not called CC=1 SCAR=6.9
Pod documented
218 0 my ($login, $password) = @_;
219  
220 0 my $root = new URI ($self->{agent}{root});
221 0 my $staging = $self->get_uri ('uploads')->abs ($root);
222 0 my $netloc = $staging->host.':'.$staging->port;
223  
224 0 $self->{agent}->credentials ($netloc,
225 'GoodData project data staging area', $login => $password);
226  
227 0 $self->{login} = $self->{agent}->post ($self->get_uri ('login'),
228 {postUserLogin => {
229 login => $login,
230 password => $password,
231 remember => 0}});
232 }
233  
234 =item B<logout>
235  
236 Make server invalidate the client session and drop
237 credential tokens.
238  
239 Is called upon destruction of the GoodData client instance.
240  
241 =cut
242  
243 sub logout
244 {
245 0 ▶ my $self = shift;
Subroutine logout: not called CC=2 SCAR=17.9
Pod documented
246  
247 0 ▶ die 'Not logged in' unless defined $self->{login};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"login"} 0 0 0
248  
249 # Forget Basic authentication
250 0 my $root = new URI ($self->{agent}{root});
251 0 my $staging = $self->get_uri ('uploads');
252 0 my $netloc = $staging->host.':'.$staging->port;
253 0 $self->{agent}->credentials ($netloc,
254 'GoodData project data staging area', undef, undef);
255  
256 # The redirect magic does not work for POSTs and we can't really
257 # handle 401s until the API provides reason for them...
258 0 $self->{agent}->get ($self->get_uri ('token'));
259  
260 0 $self->{agent}->delete ($self->{login}{userLogin}{state});
261 0 $self->{login} = undef;
262 }
263  
264 =item B<change_passwd> OLD NEW
265  
266 Change user password given the old and new password.
267  
268 =cut
269  
270 sub change_passwd
271 {
272 0 ▶ my $self = shift;
Subroutine change_passwd: not called CC=4 SCAR=30.0
Pod documented
273 0 ▶ my $old_passwd = shift or die 'No old password given';
Branch: 1 branch
BranchTrueFalseTotal
my $old_passwd = shift() or die "No old password given" 0 0 0
274 0 ▶ my $new_passwd = shift or die 'No new password given';
Branch: 1 branch
BranchTrueFalseTotal
my $new_passwd = shift() or die "No new password given" 0 0 0
275  
276 0 ▶ die 'Not logged in' unless defined $self->{login};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"login"} 0 0 0
277  
278 0 my $profile = $self->{agent}->get ($self->{login}{userLogin}{profile});
279 my $new_profile = {
280 'accountSetting' => {
281 'old_password' => $old_passwd,
282 'password' => $new_passwd,
283 'verifyPassword' => $new_passwd,
284 'firstName' => $profile->{accountSetting}->{firstName},
285 'lastName' => $profile->{accountSetting}->{lastName}
286 }
287 0 };
288  
289 0 $self->{agent}->put ($self->{login}{userLogin}{profile}, $new_profile);
290 }
291  
292 =item B<projects>
293  
294 Return array of links to project resources on metadata server.
295  
296 =cut
297  
298 sub projects
299 {
300 0 ▶ my $self = shift;
Subroutine projects: not called CC=2 SCAR=17.9
Pod documented
301 0 ▶ die 'Not logged in' unless $self->{login};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"login"} 0 0 0
302 0 $self->get_links (new URI ($self->{login}{userLogin}{profile}),
303 qw/projects project/);
304 }
305  
306 =item B<delete_project> IDENTIFIER
307  
308 Delete a project given its identifier.
309  
310 =cut
311  
312 sub delete_project
313 {
314 0 ▶ my $self = shift;
Subroutine delete_project: not called CC=2 SCAR=17.9
Pod documented
315 0 my $project = shift;
316  
317 # Instead of directly DELETE-ing the URI gotten, we check
318 # the existence of a project with such link, as a sanity check
319 0 ▶ my $uri = $self->get_uri (new URI ($project),
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->get_uri("URI"->new($project), {"category", "self", "type", "project"}) or die "No such project: $project" 0 0 0
320 { category => 'self', type => 'project' }) # Validate it's a project
321 or die "No such project: $project";
322 0 $self->{agent}->delete ($uri);
323 }
324  
325 =item B<create_project> TITLE SUMMARY TEMPLATE
326  
327 Create a project given its title and optionally summary and project template,
328 return its identifier.
329  
330 The list of valid project templates is available from the template server:
331 L<https://secure.gooddata.com/projectTemplates/>.
332  
333 =cut
334  
335 sub create_project
336 {
337 0 ▶ my $self = shift;
Subroutine create_project: not called CC=4 SCAR=30.0
Pod documented
338 0 ▶ my $title = shift or die 'No title given';
Branch: 1 branch
BranchTrueFalseTotal
my $title = shift() or die "No title given" 0 0 0
339 0 ▶ my $summary = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
340 0 my $template = shift;
341  
342 # The redirect magic does not work for POSTs and we can't really
343 # handle 401s until the API provides reason for them...
344 0 $self->{agent}->get ($self->get_uri ('token'));
345  
346 return $self->{agent}->post ($self->get_uri ('projects'), {
347 project => {
348 # No hook to override this; use web UI
349 content => { guidedNavigation => 1 },
350 meta => {
351 summary => $summary,
352 title => $title,
353 ($template ? (projectTemplate => $template) : ()),
354 }
355 0 ▶ }})->{uri};
Branch: 1 branch
BranchTrueFalseTotal
$template ? : 0 0 0
356 }
357  
358 =item B<wait_project_enabled> PROJECT_URI
359  
360 Wait until project identified by its uri is in enabled state,
361 return its identifier.
362  
363 =cut
364  
365 sub wait_project_enabled
366 {
367 0 ▶ my $self = shift;
Subroutine wait_project_enabled: not called CC=4 SCAR=30.0
Pod documented
368 0 ▶ my $project_uri = shift || die 'Project uri was not specified.';
Condition0%0/2
shift() || die('Project uri was not specified.')
l!l
00
MC/DC0%0/1
shift() || die('Project uri was not specified.')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
369  
370 0 my $state;
371 my $exported = $self->poll (
372 0 ▶ sub { $self->{agent}->get ($project_uri) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
373 sub { $_[0] and exists $_[0]->{project} and exists $_[0]->{project}{content} and exists $_[0]->{project}{content}{state} and
374 0 ▶ (($state = $_[0]->{project}{content}{state}) !~ /^(PREPARING|PREPARED|LOADING)$/)
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
$_[0] and exists $_[0]{"project"} and exists $_[0]{"project"}{"content"} and exists $_[0]{"project"}{"content"}{"state"} and not +($state = $_[0]{"project"}{"content"}{"state"}) =~ /^(PREPARING|PREPARED|LOADING)$/ 0 0 0
Condition0%0/9
$_[0] and exists $_[0]{"project"} and exists $_[0]{"project"}{"content"} and exists $_[0]{"project"}{"content"}{"state"}
!ll&&!rl&&r
000
$_[0] and exists $_[0]{"project"} and exists $_[0]{"project"}{"content"}
!ll&&!rl&&r
000
$_[0] and exists $_[0]{"project"}
!ll&&!rl&&r
000
MC/DC0%0/4
$_[0] and exists $_[0]{"project"} and exists $_[0]{"project"}{"content"} and exists $_[0]{"project"}{"content"}{"state"}
$_[0] exists $_[0]{"project"} exists $_[0]{"project"}{"content"} exists $_[0]{"project"}{"content"}{"state"}
Truth table0%0/5
A and B and C and D
A= $_[0]B= exists $_[0]{"project"}C= exists $_[0]{"project"}{"content"}D= exists $_[0]{"project"}{"content"}{"state"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
375 }
376 0 ▶ ) or die 'Timed out waiting for project preparation';
Branch: 1 branch
BranchTrueFalseTotal
my $exported = $self->poll(sub { $self->{"agent"}->get($project_uri); } , sub { $_[0] and exists $_[0]{"project"} and exists $_[0]{"project"}{"content"} and exists $_[0]{"project"}{"content"}{"state"} and not +($state = $_[0]{"project"}{"content"}{"state"}) =~ /^(PREPARING|PREPARED|LOADING)$/; } ) or die "Timed out waiting for project preparation" 0 0 0
377 0 ▶ ($state eq 'ENABLED') or die "Unable to enable project";
Branch: 1 branch
BranchTrueFalseTotal
$state eq "ENABLED" or die "Unable to enable project" 0 0 0
378 }
379  
380 =item B<create_user> LOGIN PASSWORD FIRST_NAME LAST_NAME PHONE COMPANY
381  
382 Create a user given its login, password, first name, surname, phone and optionally company,
383 return his identifier.
384  
385 =cut
386  
387 sub create_user
388 {
389 0 ▶ my $self = shift;
Subroutine create_user: not called CC=2 SCAR=17.9
Pod documented
390 0 my $login = shift;
391 0 my $passwd = shift;
392 0 my $firstname = shift;
393 0 my $lastname = shift;
394 0 my $phone = shift;
395 0 ▶ my $company = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
396  
397 return $self->{agent}->post ('/gdc/account/domains/default/users', { #TODO links does not exists
398 accountSetting => {
399 login => $login,
400 password => $passwd,
401 verifyPassword => $passwd,
402 firstName => $firstname,
403 lastName => $lastname,
404 phoneNumber => $phone,
405 companyName => $company
406 0 }})->{uri};
407 }
408  
409 =item B<get_roles>
410  
411 Gets project roles. Project is identified by its id.
412 return array of project roles.
413  
414 =cut
415  
416 sub get_roles
417 {
418 0 ▶ my $self = shift;
Subroutine get_roles: not called CC=1 SCAR=6.9
Pod documented
419 0 my $project = shift;
420  
421 return $self->{agent}->get (
422 0 $self->get_uri (new URI($project), 'roles'))->{projectRoles}{roles};
423 }
424  
425 =item B<get_roles_by_id>
426  
427 Gets project roles. Project is identified by its id.
428 return hash map role id => role uri.
429  
430 =cut
431  
432 sub get_roles_by_id
433 {
434 0 ▶ my $self = shift;
Subroutine get_roles_by_id: not called CC=2 SCAR=17.9
Pod documented
435 0 my $project = shift;
436 0 my $rolesUris = $self->get_roles ($project);
437  
438 0 my %roles;
439  
440 0 foreach my $roleUri (@$rolesUris) {
441 0 my $role = $self->{agent}-> get ($roleUri);
442 0 my $roleId = $role->{projectRole}{meta}{identifier};
443 0 $roles{$roleId} = $roleUri;
444 }
445 0 return %roles;
446 }
447  
448 =item B<assign_user> USER PROJECT ROLE
449  
450 Assign user to project.
451 return his identifier.
452  
453 =cut
454  
455 sub assign_user
456 {
457 0 ▶ my $self = shift;
Subroutine assign_user: not called CC=1 SCAR=6.9
Pod documented
458 0 my $user = shift;
459 0 my $project = shift;
460 0 my $role = shift;
461  
462 0 my @userRoles = ($role);
463  
464 0 return $self->{agent}->post ($self->get_uri (new URI($project),'users'), {
465 user => {
466 content => {
467 status => "ENABLED",
468 userRoles => \@userRoles
469 },
470 links => {
471 self => $user
472 }
473 }
474 });
475 }
476  
477 =item B<schedule> PROJECT_URI CRON PARAMS HIDDEN_PARAMS
478  
479 Create a schedule given its project, type, cron expression and optionally
480 parameters and hidden parameters, return created schedule object.
481  
482 =cut
483  
484 sub schedule {
485 0 ▶ my $self = shift;
Subroutine schedule: not called CC=3 SCAR=24.8
Pod documented
486 0 my $project_uri = shift;
487 0 my $type = shift;
488 0 my $cron = shift;
489 0 ▶ my $params = shift || { };
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
490 0 ▶ my $hidden_params = shift || { };
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
491  
492 0 return $self->{agent}->post ($project_uri.'/schedules', {schedule => { #TODO no link to schedules does not exists
493 type => $type,
494 params => $params,
495 hiddenParams => $hidden_params,
496 cron => $cron
497 }});
498 }
499  
500 =item B<schedule_msetl_graph> PROJECT_URI TRANSFORMATION_ID GRAPH_NAME CRON PARAMS HIDDEN_PARAMS
501  
502 Create a MSETL schedule given its project, clover transformation id,
503 clover graph to schedule, cron expression and optionally
504 parameters and hidden parameters, return created schedule object.
505  
506 =cut
507  
508 sub schedule_msetl_graph {
509 0 ▶ my $self = shift;
Subroutine schedule_msetl_graph: not called CC=3 SCAR=24.8
Pod documented
510 0 my $project_uri = shift;
511 0 my $trans_id = shift;
512 0 my $graph = shift;
513 0 my $cron = shift;
514 0 ▶ my $params = shift || { };
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
515 0 ▶ my $hidden_params = shift || { };
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
516  
517 0 my $type = "MSETL";
518  
519 0 $params->{"TRANSFORMATION_ID"} = $trans_id;
520 0 $params->{"CLOVER_GRAPH"} = $graph;
521  
522 0 return $self->schedule (
523 $project_uri, $type, $cron, $params, $hidden_params);
524 }
525  
526 =item B<create_clover_transformation> PROJECT_URI TEMPLATE TRANSFORMATION_ID NAME
527  
528 Create a clover transformation given its project uri, template, clover
529 transformation id in template and optionaly name, return created transformation
530 object.
531  
532 =cut
533  
534 sub create_clover_transformation
535 {
536 0 ▶ my $self = shift;
Subroutine create_clover_transformation: not called CC=2 SCAR=17.9
Pod documented
537 0 my $projectUri = shift;
538 0 my $template = shift;
539 0 my $transformation = shift;
540 0 ▶ my $name = shift || $transformation;
Condition0%0/3
shift() || $transformation
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $transformation
shift() $transformation
Truth table0%0/3
A || B
A= shift()B= $transformation
ABresult
1X1
011
000
541  
542 0 my $file = $transformation.'.zip';
543 0 my $path = '/uploads/'.$file;
544  
545 # download clover transformation zip file from project template
546 0 my $content = $self->{agent}->get ($template.'/'.$file);
547  
548 # upload clover transformation zip file
549 0 my $uploads = new URI ($self->get_uri ('uploads'));
550 0 $uploads->path_segments ($uploads->path_segments, $file);
551 $self->{agent}->request (new HTTP::Request (PUT => $uploads,
552 0 ['Content-Type' => 'application/zip'], $content->{raw}));
553  
554 # create transformation
555 0 return $self->{agent}->post ($projectUri."/etl/clover/transformations", { #TODO links does not exists
556 cloverTransformation => {
557 name => $name,
558 path => $path
559 }
560 });
561 }
562  
563 =item B<reports> PROJECT
564  
565 Return array of links to repoort resources on metadata server.
566  
567 =cut
568  
569 sub reports
570 {
571 0 ▶ my $self = shift;
Subroutine reports: not called CC=2 SCAR=17.9
Pod documented
572 0 my $project = shift;
573  
574 0 ▶ die 'Not logged in' unless $self->{login};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"login"} 0 0 0
575 0 $self->get_links (new URI ($project),
576 { category => 'self', type => 'project' }, # Validate it's a project
577 qw/metadata query reports/, {});
578 }
579  
580 =item B<compute_report> REPORT
581  
582 Trigger a report computation and return the URI of the result resource.
583  
584 =cut
585  
586 sub compute_report
587 {
588 0 ▶ my $self = shift;
Subroutine compute_report: not called CC=1 SCAR=6.9
Pod documented
589 0 my $report = shift;
590  
591 return $self->{agent}->post (
592 $self->get_uri (qw/xtab xtab-executor3/),
593 { report_req => { report => $report }}
594 0 )->{reportResult2}{meta}{uri};
595 }
596  
597 =item B<export_report> REPORT FORMAT
598  
599 Submit an exporter task for a computed report (see B<compute_report>),
600 wait for completion and return raw data in desired format.
601  
602 =cut
603  
604 sub export_report
605 {
606 0 ▶ my $self = shift;
Subroutine export_report: not called CC=5 SCAR=34.0
Pod documented
607 0 my $report = shift;
608 0 my $format = shift;
609  
610 # Compute the report
611 my $result = $self->{agent}->post (
612 0 $self->get_uri (qw/report-exporter exporter-executor/),
613 { result_req => { format => $format,
614 report => $self->compute_report ($report) }}
615 );
616  
617 # This is for new release, where location is finally set correctly;
618 0 ▶ $result = $result->{uri} if ref $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" 0 0 0
619  
620 # Trigger the export
621 my $exported = $self->poll (
622 0 ▶ sub { $self->{agent}->get ($result) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
623 0 ▶ sub { $_[0] and exists $_[0]->{raw} and $_[0]->{raw} ne 'null' }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_[0] and exists $_[0]{"raw"} and $_[0]{"raw"} ne "null" 0 0 0
Condition0%0/3
$_[0] and exists $_[0]{"raw"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and exists $_[0]{"raw"}
$_[0] exists $_[0]{"raw"}
Truth table0%0/3
A and B
A= $_[0]B= exists $_[0]{"raw"}
ABresult
0X0
100
111
624 0 ▶ ) or die 'Timed out waiting for report to export';
Branch: 1 branch
BranchTrueFalseTotal
my $exported = $self->poll(sub { $self->{"agent"}->get($result); } , sub { $_[0] and exists $_[0]{"raw"} and $_[0]{"raw"} ne "null"; } ) or die "Timed out waiting for report to export" 0 0 0
625  
626 # Follow the link
627 0 ▶ $exported = $self->{agent}->get ($exported->{uri}) if exists $exported->{uri};
Branch: 1 branch
BranchTrueFalseTotal
if exists $exported->{"uri"} 0 0 0
628  
629 # Gotten the correctly coded result?
630 return $exported->{raw} if $exported->{type} eq {
631 png => 'image/png',
632 pdf => 'application/pdf',
633 xls => 'application/vnd.ms-excel',
634 0 ▶ }->{$format};
Branch: 1 branch
BranchTrueFalseTotal
if $exported->{"type"} eq {"png", "image/png", "pdf", "application/pdf", "xls", "application/vnd.ms-excel"}->{$format} 0 0 0
635  
636 0 die 'Wrong type of content returned';
637 }
638  
639 =item B<ldm_picture> PROJECT
640  
641 Return picture of Logical Data Model (LDM) in PNG format.
642  
643 =cut
644  
645 sub ldm_picture
646 {
647 0 ▶ my $self = shift;
Subroutine ldm_picture: not called CC=2 SCAR=17.9
Pod documented
648 0 my $project = shift;
649  
650 my $model = $self->{agent}->get ($self->{agent}->get (
651 $self->get_uri (new URI ($project),
652 0 { category => 'ldm' }))->{uri});
653 0 ▶ die 'Expected PNG image' unless $model->{type} eq 'image/png';
Branch: 1 branch
BranchTrueFalseTotal
unless $model->{"type"} eq "image/png" 0 0 0
654  
655 0 return $model->{raw};
656 }
657  
658 =item B<ldm_manage> PROJECT MAQL
659  
660 Execute MAQL statement for a project.
661  
662 =cut
663  
664 sub ldm_manage
665 {
666 0 ▶ my $self = shift;
Subroutine ldm_manage: not called CC=1 SCAR=6.9
Pod documented
667 0 my $project = shift;
668 0 my $maql = shift;
669  
670 0 $maql = "# WWW::GoodData MAQL execution\n$maql";
671 0 chomp $maql;
672  
673 $self->{agent}->post (
674 0 $self->get_uri (new URI ($project), qw/metadata ldm ldm-manage/),
675 { manage => { maql => $maql }});
676 }
677  
678 =item B<upload> PROJECT MANIFEST
679  
680 Upload and integrate a new data load via Single Loading Interface (SLI).
681  
682 =cut
683  
684 sub upload
685 {
686 0 ▶ my $self = shift;
Subroutine upload: not called CC=6 SCAR=37.4
Pod documented
687 0 my $project = shift;
688 0 my $file = shift;
689  
690 # Parse the manifest
691 0 my $upload_info = decode_json (slurp_file ($file));
692 die "$file: not a SLI manifest"
693 0 ▶ unless $upload_info->{dataSetSLIManifest};
Branch: 1 branch
BranchTrueFalseTotal
unless $upload_info->{"dataSetSLIManifest"} 0 0 0
694  
695 # Construct unique URI in staging area to upload to
696 0 my $uploads = new URI ($self->get_uri ('uploads'));
697 $uploads->path_segments ($uploads->path_segments,
698 0 $upload_info->{dataSetSLIManifest}{dataSet}.'-'.time);
699 0 $self->{agent}->request (new HTTP::Request (MKCOL => $uploads));
700  
701 # Upload the manifest
702 0 my $manifest = $uploads->clone;
703 0 $manifest->path_segments ($manifest->path_segments, 'upload_info.json');
704 0 $self->{agent}->request (new HTTP::Request (PUT => $manifest,
705 ['Content-Type' => 'application/json'], encode_json ($upload_info)));
706  
707 # Upload CSV
708 0 my $csv = $uploads->clone;
709 0 $csv->path_segments ($csv->path_segments, $upload_info->{dataSetSLIManifest}{file});
710 $self->{agent}->request (new HTTP::Request (PUT => $csv,
711 ['Content-Type' => 'application/csv'],
712 (slurp_file ($upload_info->{dataSetSLIManifest}{file})
713 0 ▶ || die 'No CSV file specified in SLI manifest')));
Condition0%0/2
slurp_file($upload_info->{'dataSetSLIManifest'}{'file'}) || die('No CSV file specified in SLI manifest')
l!l
00
MC/DC0%0/1
slurp_file($upload_info->{'dataSetSLIManifest'}{'file'}) || die('No CSV file specified in SLI manifest')
slurp_file($upload_info->{'dataSetSLIManifest'}{'file'})
Truth table0%0/2
A
A= slurp_file($upload_info->{'dataSetSLIManifest'}{'file'})
Aresult
11
00
714  
715 # Trigger the integration
716 my $task = $self->{agent}->post (
717 $self->get_uri (new URI ($project),
718 { category => 'self', type => 'project' }, # Validate it's a project
719 qw/metadata etl pull/),
720 { pullIntegration => [$uploads->path_segments]->[-1] }
721 0 )->{pullTask}{uri};
722  
723 # Wait for the task to enter a stable state
724 my $result = $self->poll (
725 0 ▶ sub { $self->{agent}->get ($task) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
726 0 ▶ sub { shift->{taskStatus} !~ /^(RUNNING|PREPARED)$/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
727 0 ▶ ) or die 'Timed out waiting for integration to finish';
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->poll(sub { $self->{"agent"}->get($task); } , sub { not +(shift())->{"taskStatus"} =~ /^(RUNNING|PREPARED)$/; } ) or die "Timed out waiting for integration to finish" 0 0 0
728  
729 0 ▶ return if $result->{taskStatus} eq 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"taskStatus"} eq "OK" 0 0 0
730 0 ▶ warn 'Upload finished with warnings' if $result->{taskStatus} eq 'WARNING';
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"taskStatus"} eq "WARNING" 0 0 0
731 0 die 'Upload finished with '.$result->{taskStatus}.' status';
732 }
733  
734 =item B<poll> BODY CONDITION
735  
736 Should only be used internally.
737  
738 Run BODY passing its return value to call to CONDITION until it
739 evaluates to true or B<retries> (see properties) times out.
740  
741 Returns value is of last iteration of BODY in case
742 CONDITION succeeds, otherwise undefined (in case of timeout).
743  
744 =cut
745  
746 sub poll
747 {
748 0 ▶ my $self = shift;
Subroutine poll: not called CC=3 SCAR=24.8
Pod documented
749 0 my ($body, $cond) = @_;
750 0 my $retries = $self->{retries};
751  
752 0 ▶ while ($retries--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries--) 0 0 0
753 0 my $ret = $body->();
754 0 ▶ return $ret if $cond->($ret);
Branch: 1 branch
BranchTrueFalseTotal
if &$cond($ret) 0 0 0
755 0 sleep 1;
756 }
757  
758 0 return undef;
759 }
760  
761 =item B<create_object_with_expression> PROJECT URI TYPE TITLE SUMMARY EXPRESSION
762  
763 Create a new metadata object of type TYPE with EXPRESSION as the only content.
764  
765 =cut
766  
767 sub create_object_with_expression
768 {
769 0 ▶ my $self = shift;
Subroutine create_object_with_expression: not called CC=6 SCAR=37.4
Pod documented
770 0 my $project = shift;
771 0 my $uri = shift;
772 0 ▶ my $type = shift or die 'No type given';
Branch: 1 branch
BranchTrueFalseTotal
my $type = shift() or die "No type given" 0 0 0
773 0 ▶ my $title = shift or die 'No title given';
Branch: 1 branch
BranchTrueFalseTotal
my $title = shift() or die "No title given" 0 0 0
774 0 ▶ my $summary = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
775 0 ▶ my $expression = shift or die 'No expression given';
Branch: 1 branch
BranchTrueFalseTotal
my $expression = shift() or die "No expression given" 0 0 0
776  
777 0 ▶ if (defined $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri) { } 0 0 0
778 0 $uri = new URI ($uri);
779 } else {
780 0 $uri = $self->get_uri (new URI ($project), qw/metadata obj/);
781 }
782  
783 return $self->{agent}->post (
784 $uri,
785 { $type => {
786 content => {
787 expression => $expression
788 },
789 meta => {
790 summary => $summary,
791 title => $title,
792 }
793 }}
794 0 )->{uri};
795 }
796  
797 =item B<create_report_definition> PROJECT URI TITLE SUMMARY METRICS DIM FILTERS
798  
799 Create a new reportDefinition in metadata.
800  
801 =cut
802  
803 sub create_report_definition
804 {
805 0 ▶ my $self = shift;
Subroutine create_report_definition: not called CC=7 SCAR=40.3
Pod documented
806 0 my $project = shift;
807 0 my $uri = shift;
808 0 ▶ my $title = shift or die 'No title given';
Branch: 1 branch
BranchTrueFalseTotal
my $title = shift() or die "No title given" 0 0 0
809 0 ▶ my $summary = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
810 0 ▶ my $metrics = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
811 0 ▶ my $dim = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
812 0 ▶ my $filters = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
813  
814 0 ▶ if (defined $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri) { } 0 0 0
815 0 $uri = new URI ($uri);
816 } else {
817 0 $uri = $self->get_uri (new URI ($project), qw/metadata obj/);
818 }
819  
820 return $self->{agent}->post (
821 $uri,
822 { reportDefinition => {
823 content => {
824 filters => [ map +{ expression => $_ }, @$filters ],
825 grid => {
826 columns => [ "metricGroup" ],
827 metrics => [ map +{ alias => '', uri => $_ }, @$metrics ],
828 rows => [ map +{ attribute => { alias => '', uri => $_,
829 totals => [[]] } }, @$dim ],
830 sort => {
831 columns => [],
832 rows => [],
833 },
834 columnWidths => []
835 },
836 format => "grid"
837 },
838 meta => {
839 summary => $summary,
840 title => $title,
841 }
842 }}
843 0 )->{uri};
844 }
845  
846 =item B<DESTROY>
847  
848 Log out the session with B<logout> unless not logged in.
849  
850 =cut
851  
852 sub DESTROY
853 {
854 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=2 SCAR=17.9
855 0 ▶ $self->logout if $self->{login};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"login"} 0 0 0
856 }
857  
858 sub slurp_file
859 {
860 0 ▶ my $file = shift;
Subroutine slurp_file: not called CC=2 SCAR=17.9
Pod undocumented
861 0 ▶ open (my $fh, '<', $file) or die "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "${file}: $!" 0 0 0
862 0 return join '', <$fh>;
863 }
864  
865 =back
866  
867 =head1 SEE ALSO
868  
869 =over
870  
871 =item *
872  
873 L<http://developer.gooddata.com/api/> -- API documentation
874  
875 =item *
876  
877 L<https://secure.gooddata.com/gdc/> -- Browsable GoodData API
878  
879 =item *
880  
881 L<WWW::GoodData::Agent> -- GoodData API-aware user agent
882  
883 =back
884  
885 =head1 COPYRIGHT
886  
887 Copyright 2011, 2012 Lubomir Rintel
888  
889 Copyright 2012 Adam Stulpa, Jan Orel, Tomas Janousek
890  
891 This program is free software; you can redistribute it and/or modify it
892 under the same terms as Perl itself.
893  
894 =head1 AUTHORS
895  
896 Lubomir Rintel C<lkundrak@v3.sk>
897  
898 Adam Stulpa C<adam.stulpa@gooddata.com>
899  
900 Jan Orel C<jan.orel@gooddata.com>
901  
902 Tomas Janousek C<tomi@nomi.cz>
903  
904 =cut
905  
906 1 1;
↑ Summary blib/lib/WWW/GoodData/Agent.pm »