Tango/API.pm

filter: Statementstmt 71.1% 111 / 156 Branchbran 52.8% 37 / 70 Conditioncond 68.1% 15 / 22 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 87.5% 14 / 16 Podpod n/a totaltotal 66.3% 183 / 276 CC 128 SCAR 36.7
CC
128
Cov
66%
cluster_endpoints
45.4
create_collection
45.4
delete_document
45.4
CRAP 39.2
↑ Summary Tango/Cursor.pm »
1 # ABSTRACT: Internal module with the API specification
2 package Arango::Tango::API;
3 9 $Arango::Tango::API::VERSION = '0.021';
4 #use Arango::Tango::Database;
5 #use Arango::Tango::Collection;
6  
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use MIME::Base64 3.11 'encode_base64url';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use JSON::Schema::Fit 0.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Sub::Install qw(install_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Sub::Name qw(subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21 27 ▶sub _install_methods($$) {
Subroutine _install_methods: called CC=4 SCAR=18.7
22 27 my ($package, $methods) = @_;
23 27 for my $method (keys %$methods) {
24 414 my $value = $methods->{$method};
25  
26 # A signature lists the positional arguments in order; the names
27 # starting with '?' are optional.
28 414 ▶ my @required_names = grep { !/^\?/ } @{ $value->{signature} // [] };
Condition100%2/2
$value->{"signature"} // []
l!l
171243
MC/DC100%1/1
$value->{"signature"} // []
$value->{"signature"}
Truth table100%2/2
A
A= $value->{"signature"}
Aresult
11
00
29 414 ▶ my @optional_names = map { /^\?(.+)$/ } @{ $value->{signature} // [] };
Condition100%2/2
$value->{"signature"} // []
l!l
171243
MC/DC100%1/1
$value->{"signature"} // []
$value->{"signature"}
Truth table100%2/2
A
A= $value->{"signature"}
Aresult
11
00
30  
31 install_sub {
32 into => $package,
33 as => $method,
34 code => subname(
35 "${package}::$method",
36 0 ▶ sub {
Subroutine truncate: not called CC=12 SCAR=38.2
Subroutine list_users: called CC=12 SCAR=38.2
37 0 my $self = shift;
38 0 ▶ my $arango = ref($self) eq "Arango::Tango" ? $self : $self->{arango};
Branch: 2 branches
BranchTrueFalseTotal
ref $self eq 'Arango::Tango' ? : 0 0 0
ref $self eq 'Arango::Tango' ? : 10 6 16
39 0 my %required = ();
40 0 my %optional = ();
41 0 ▶ if (exists($value->{signature})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $value->{"signature"}) 0 0 0
if (exists $value->{"signature"}) 16 0 16
42 0 ▶ if (scalar(@_) < scalar(@required_names)) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ < scalar @required_names) 0 0 0
if (scalar @_ < scalar @required_names) 4 12 16
43 # Required arguments are consumed in order, so the
44 # first missing one is the one right after the
45 # arguments that were supplied.
46 0 $arango->{error} = sprintf("Arango::Tango | %s | Missing %s", $method, $required_names[scalar(@_)]);
47 0 die "-1\n";
48 }
49 0 %required = map { $_ => shift @_ } @required_names;
50 0 for my $name (@optional_names) {
51 0 ▶ next if ref($_[0]);
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] 0 0 0
if ref $_[0] 0 3 3
52 0 $optional{$name} = shift @_;
53 }
54  
55 0 %required = ( %required, %optional );
56 }
57  
58 0 ▶ if (exists($value->{inject_properties})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $value->{"inject_properties"}) 0 0 0
if (exists $value->{"inject_properties"}) 5 7 12
59 0 foreach my $property (@{$value->{inject_properties}}) {
60 0 ▶ if (ref($property) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $property eq 'HASH') { } 0 0 0
if (ref $property eq 'HASH') { } 3 4 7
61 0 ▶ unless (exists $property->{prop}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $property->{"prop"}) 0 0 0
unless (exists $property->{"prop"}) 0 3 3
62 0 $arango->{error} = "Property injection without property" ;
63 0 die "-1\n";
64 }
65 0 ▶ unless (exists $property->{as}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $property->{"as"}) 0 0 0
unless (exists $property->{"as"}) 0 3 3
66 0 $arango->{error} = "Property injection without alias" ;
67 0 die "-1\n";
68 }
69 0 $required{$property->{as}} = $self->{$property->{prop}};
70 }
71 else {
72 0 $required{$property} = $self->{$property};
73 }
74 }
75 }
76 0 ▶ if (scalar(@_) % 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar(@_) % 2) 0 0 0
if (scalar(@_) % 2) 0 12 12
77 0 $arango->{error} = sprintf("Arango::Tango | %s | Odd number of elements on options hash", $method);
78 0 die "-1\n";
79 }
80 0 return $arango->__api( $value, { @_, %required });
81 })
82 414 };
83 }
84 }
85  
86 9 my %API = (
87 'bulk_import_list' => {
88 rest => [ post => '{{database}}_api/import?collection={collection}'],
89 url_schema => {
90 type => { type => 'string', pattern => 'documents|list|auto' },
91 fromPrefix => { type => 'string' },
92 toPrefix => { type => 'string' },
93 overwrite => { type => 'boolean' },
94 waitForSync => { type => 'boolean' },
95 onDuplicate => { type => 'string', pattern => 'error|update|replace|ignore' },
96 complete => { type => 'boolean' },
97 details => { type => 'boolean' }
98 }
99 },
100 'create_document' => {
101 rest => [ post => '{{database}}_api/document/{collection}']
102 },
103 'replace_document' => {
104 rest => [ put => '{{database}}_api/document/{collection}/{key}' ],
105 },
106 'list_collections' => {
107 rest => [ get => '{{database}}_api/collection'],
108 schema => { excludeSystem => { type => 'boolean' } }
109 },
110 'cursor_next' => {
111 rest => [ put => '{{database}}_api/cursor/{id}']
112 },
113 'cursor_delete' => {
114 rest => [ delete => '{{database}}_api/cursor/{id}']
115 },
116 'accessible_databases' => {
117 rest => [ get => '_api/database/user']
118 },
119 'all_keys' => {
120 rest => [ put => '{{database}}_api/simple/all-keys' ],
121 schema => { type => { type => 'string' }, collection => { type => 'string' } },
122 },
123 'create_cursor' => {
124 rest => [ post => '{{database}}_api/cursor' ],
125 schema => {
126 query => { type => 'string' },
127 count => { type => 'boolean' },
128 batchSize => { type => 'integer' },
129 cache => { type => 'boolean' },
130 memoryLimit => { type => 'integer' },
131 ttl => { type => 'integer' },
132 bindVars => { type => 'object', additionalProperties => 1 },
133 options => { type => 'object', additionalProperties => 0, properties => {
134 failOnWarning => { type => 'boolean' },
135 profile => { type => 'integer', maximum => 2, minimum => 0 }, # 0, 1, 2
136 maxTransactionSize => { type => 'integer' },
137 stream => { type => 'boolean' },
138 skipInaccessibleCollections => { type => 'boolean' },
139 maxWarningCount => { type => 'integer' },
140 intermediateCommitCount => { type => 'integer' },
141 satelliteSyncWait => { type => 'integer' },
142 fullCount => { type => 'boolean' },
143 intermediateCommitSize => { type => 'integer' },
144 'optimizer.rules' => { type => 'string' },
145 maxPlans => { type => 'integer' },
146 }
147 },
148 },
149 },
150  
151 create_user => {
152 method => 'post',
153 uri => '_api/user',
154 schema => {
155 password => { type => 'string' },
156 active => { type => 'boolean' },
157 user => { type => 'string' },
158 extra => { type => 'object', additionalProperties => 1 },
159 }
160 },
161 );
162  
163  
164  
165 10 ▶sub _check_options {
Subroutine _check_options: called CC=1 SCAR=0.0
166 10 my ($params, $properties) = @_;
167 10 my $schema = { type => 'object', additionalProperties => 0, properties => $properties };
168 10 my $prepared_data = JSON::Schema::Fit->new(replace_invalid_values => 1)->get_adjusted($params, $schema);
169 10 return $prepared_data;
170 }
171  
172 # Keys of the params hash that are consumed by __api itself and must
173 # never be forwarded as request options.
174 9 my @INTERNAL_KEYS = qw( _body _url_parameters );
175  
176 0 ▶sub _api {
Subroutine _api: not called CC=1 SCAR=6.9
177 0 my ($self, $action, $params) = @_;
178 0 return $self->__api( $API{$action}, $params);
179 }
180  
181 16 ▶sub __api {
Subroutine __api: called CC=26 SCAR=35.8
182 16 my ($self, $conf, $params) = @_;
183  
184 16 my ($method, $uri) = @{$conf->{rest}};
185 16 $method = uc $method;
186  
187 16 my $params_copy = clone $params; ## FIXME: decide if this is relevant
188  
189 16 ▶ $uri =~ s[\{\{database\}\}] [ defined $params->{database} ? "_db/" . uri_escape_utf8($params->{database}) . "/" : "" ]e;
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'database'} ? : 5 0 5
190 16 ▶ $uri =~ s[\{([^}]+)\}] [uri_escape_utf8($params->{$1} // "")]eg;
Condition100%2/2
$params->{$1} // ""
l!l
161
MC/DC100%1/1
$params->{$1} // ""
$params->{$1}
Truth table100%2/2
A
A= $params->{$1}
Aresult
11
00
191  
192 16 my $url = sprintf("%s://%s:%d/%s", $self->{scheme}, $self->{host}, $self->{port}, $uri);
193  
194 16 my $body = undef;
195 16 my $opts = {};
196 16 my $url_opts = {};
197 16 ▶ if (ref($params) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "HASH") 16 0 16
198 16 ▶ $body = $params->{_body} if exists $params->{_body};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"_body"} 1 15 16
199 16 ▶ $url_opts = $params->{_url_parameters} if exists $params->{_url_parameters} and ref($params->{_url_parameters}) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"_url_parameters"} and ref $params->{"_url_parameters"} eq "HASH" 3 13 16
Condition66%2/3
exists $params->{"_url_parameters"} and ref $params->{"_url_parameters"} eq "HASH"
!ll&&!rl&&r
1303
MC/DC50%1/2
exists $params->{"_url_parameters"} and ref $params->{"_url_parameters"} eq "HASH"
exists $params->{"_url_parameters"} ref $params->{"_url_parameters"} eq "HASH"
Truth table66%2/3
A and B
A= exists $params->{"_url_parameters"}B= ref $params->{"_url_parameters"} eq "HASH"
ABresult
0X0
100
111
200 16 $opts = $params;
201 16 delete $opts->{$_} for @INTERNAL_KEYS;
202 }
203 16 ▶ $opts = exists($conf->{schema}) ? _check_options($opts, $conf->{schema}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $conf->{'schema'} ? : 9 7 16
204 16 ▶ $url_opts = exists($conf->{url_schema}) ? _check_options($url_opts, $conf->{url_schema}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $conf->{'url_schema'} ? : 1 15 16
205  
206 16 ▶ if (keys %$url_opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$url_opts) 1 15 16
207 my $url_parameters = join("&", map {
208 1 my $val = $url_opts->{$_};
209 1 ▶ if (ref($val) eq "JSON::PP::Boolean") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "JSON::PP::Boolean") 0 1 1
210 0 ▶ $val = $val ? "yes" : "no"
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
211 }
212 1 "$_=$val" } keys %$url_opts);
213 1 ▶ $url .= ($url =~ /\?/ ? "&" : "?") . $url_parameters;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /\?/ ? : 0 1 1
214 }
215  
216 16 ▶ if (exists($conf->{require_document}) && !$body) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf->{"require_document"} and not $body) 0 16 16
Condition33%1/3
exists $conf->{"require_document"} and not $body
!ll&&!rl&&r
1600
MC/DC0%0/2
exists $conf->{"require_document"} and not $body
exists $conf->{"require_document"} not $body
Truth table33%1/3
A and not(B)
A= exists $conf->{"require_document"}B= not $body
ABresult
0X0
100
111
217 0 $self->{error} = "Document missing [ $method => $url ]";
218 0 die "-1\n";
219 }
220  
221 16 ▶ if ($method eq 'GET' && scalar(keys %$opts)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'GET' and scalar keys %$opts) { } 2 14 16
elsif ($body and ref $body eq 'HASH' || ref $body eq 'ARRAY') { } 1 13 14
elsif (defined $body) { } 0 13 13
Condition100%2/2
$method eq "GET" and scalar keys %$opts
!ll
133
MC/DC100%1/1
$method eq "GET" and scalar keys %$opts
$method eq "GET"
Truth table100%2/2
A
A= $method eq "GET"
Aresult
00
11
222 2 ▶ $url .= "?" . join("&", map { "$_=" . uri_escape_utf8($opts->{$_} // "")} keys %$opts);
Condition50%1/2
$opts->{$_} // ""
l!l
20
MC/DC0%0/1
$opts->{$_} // ""
$opts->{$_}
Truth table50%1/2
A
A= $opts->{$_}
Aresult
11
00
223 }
224 ▶ elsif ($body && (ref($body) eq "HASH" || ref($body) eq "ARRAY")) {
Condition50%3/6
$body and ref $body eq "HASH" || ref $body eq "ARRAY"
!ll&&!rl&&r
1301
ref $body eq "HASH" or ref $body eq "ARRAY"
l!l&&r!l&&!r
010
MC/DC33%1/3
$body and ref $body eq "HASH" || ref $body eq "ARRAY"
$body ref $body eq "HASH" ref $body eq "ARRAY"
Truth table50%2/4
A and B or C
A= $bodyB= ref $body eq "HASH"C= ref $body eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
225 1 $opts = { content => encode_json $body }
226 }
227 elsif (defined($body)) { # JSON
228 0 $opts = { content => $body }
229 }
230 else {
231 13 $opts = { content => encode_json $opts }
232 }
233  
234 16 my $response = $self->{http}->request($method, $url, $opts);
235 16 $self->{last_error} = $response->{status};
236  
237 16 ▶ if ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 16 0 16
238 16 my $ans = decode_json($response->{content});
239 16 ▶ if ($ans->{error}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ans->{'error'}) { } 0 16 16
elsif (exists $conf->{'builder'}) { } 3 13 16
240 0 return $ans;
241 } elsif (exists($conf->{builder})) {
242 3 return $conf->{builder}->( $self, %$params_copy );
243 } else {
244 13 return $ans;
245 }
246 }
247 else {
248 0 $self->{error} = "$response->{reason} [ $method => $url ]";
249 0 die "$response->{status}\n";
250 }
251 }
252  
253  
254  
255 9 1;
256  
257 __END__
↑ Summary Tango/Cursor.pm »