DiaColloDB/Client/http.pm

filter: Statementstmt 25.6% 20 / 78 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 37.5% 6 / 16 Podpod 80.0% 8 / 10 totaltotal 20.6% 34 / 165 CC 20 SCAR 34.7
CC
20
Cov
16%
open_http
72.0
jget
42.0
jpost
42.0
CRAP 32.0
« DiaColloDB/Client/file.pm ↑ Summary DiaColloDB/Compat.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::Client::http.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db: client: remote http server
5  
6 package DiaColloDB::Client::http;
7 2 ▶use DiaColloDB::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Request::Common; ##-- for POST()
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 ##==============================================================================
15 ## Globals & Constants
16  
17 2 our @ISA = qw(DiaColloDB::Client);
18  
19 ##==============================================================================
20 ## Constructors etc.
21  
22 ## $cli = CLASS_OR_OBJECT->new(%args)
23 ## $cli = CLASS_OR_OBJECT->new($url, %args)
24 ## + %args, object structure:
25 ## (
26 ## ##-- DiaColloDB::Client: options
27 ## url => $url, ##-- local url
28 ## ##
29 ## ##-- DiaColloDB::Client::http: options
30 ## user => $user, ##-- for LWP::UserAgent basic authentication
31 ## password => $password, ##-- for LWP::UserAgent basic authentication
32 ## logRequest => $log, ##-- log-level for HTTP requests (default:'debug')
33 ## ##
34 ## ##-- DiaColloDB::Client::http: guts
35 ## ua => $ua, ##-- underlying LWP::UserAgent
36 ## )
37  
38 ## %defaults = $CLASS_OR_OBJ->defaults()
39 ## + called by new()
40 0 ▶sub defaults {
Subroutine defaults: not called CC=1 SCAR=6.9
Pod documented
41 return (
42 0 logRequest=>'debug',
43 );
44 }
45  
46  
47  
48 ##==============================================================================
49 ## I/O: open/close
50  
51 ## $cli_or_undef = $cli->open_http($http_url,%opts)
52 ## $cli_or_undef = $cli->open_http()
53 ## + opens a local file url
54 ## + may re-bless() $cli into an appropriate package
55 ## + OVERRIDE in subclasses supporting file urls
56 0 ▶sub open_http {
Subroutine open_http: not called CC=8 SCAR=42.8
Pod documented
57 0 my ($cli,$url,%opts) = @_;
58 0 ▶ $cli = $cli->new() if (!ref($cli));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cli 0 0 0
59 0 ▶ $cli->close() if ($cli->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->opened 0 0 0
60 0 ▶ $url //= $cli->{url};
Condition0%0/3
$url //= $cli->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$url //= $cli->{"url"}
$url $cli->{"url"}
Truth table0%0/3
A //= B
A= $urlB= $cli->{"url"}
ABresult
1X1
011
000
61 0 my $uri = URI->new($url);
62 0 ▶ if ((my $path=$uri->path) !~ m/profile/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $path = $uri->path) =~ /profile/) 0 0 0
63 0 ▶ $path .= "/" if ($path !~ m{/$});
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[/$] 0 0 0
64 0 $path .= "profile.perl";
65 0 $uri->path($path);
66 }
67 0 ▶ if ($uri->query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query) 0 0 0
68 0 my %qf = $uri->query_form();
69 0 @$cli{keys %qf} = values %qf;
70 }
71 0 $cli->{url} = $uri->as_string;
72 0 ▶ $cli->{ua} //= LWP::UserAgent->new();
Condition0%0/3
$cli->{"ua"} //= "LWP::UserAgent"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$cli->{"ua"} //= "LWP::UserAgent"->new
$cli->{"ua"} "LWP::UserAgent"->new
Truth table0%0/3
A //= B
A= $cli->{"ua"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
73 0 return $cli;
74 }
75  
76 ## $cli_or_undef = $cli->close()
77 ## + default just returns $cli
78 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
79 0 my $cli = shift;
80 0 ▶ $cli->{db}->close() if ($cli->{db});
Branch: 1 branch
BranchTrueFalseTotal
if $cli->{"db"} 0 0 0
81 0 return $cli;
82 }
83  
84 ## $bool = $cli->opened()
85 ## + default just checks for $cli->{url}
86 0 ▶sub opened {
Subroutine opened: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ return ref($_[0]) && $_[0]{ua};
Condition0%0/3
ref $_[0] && $_[0]{'ua'}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] && $_[0]{'ua'}
ref $_[0] $_[0]{'ua'}
Truth table0%0/3
A && B
A= ref $_[0]B= $_[0]{'ua'}
ABresult
0X0
100
111
88 }
89  
90 ##==============================================================================
91 ## Profiling
92  
93 ##--------------------------------------------------------------
94 ## Profiling: Generic: HTTP wrappers
95  
96 ## $obj_or_undef = $cli->jget($url,\%query_form,$class)
97 ## + wrapper for http json GET requests
98 0 ▶sub jget {
Subroutine jget: not called CC=6 SCAR=37.4
Pod undocumented
99 0 my ($cli,$url,$form,$class) = @_;
100 0 ▶ my $uri = URI->new($url // $cli->{url});
Condition0%0/3
$url // $cli->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$url // $cli->{"url"}
$url $cli->{"url"}
Truth table0%0/3
A // B
A= $urlB= $cli->{"url"}
ABresult
1X1
011
000
101 0 ▶ $uri->query_form( {%{$cli->{params}//{}}, %$form} );
Condition0%0/2
$cli->{"params"} // {}
l!l
00
MC/DC0%0/1
$cli->{"params"} // {}
$cli->{"params"}
Truth table0%0/2
A
A= $cli->{"params"}
Aresult
11
00
102 0 my $req = HTTP::Request->new('GET',"$uri");
103 0 ▶ $req->authorization_basic($cli->{user}, $cli->{password}) if (defined($cli->{user}) && defined($cli->{password}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli->{"user"} and defined $cli->{"password"} 0 0 0
Condition0%0/3
defined $cli->{"user"} and defined $cli->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cli->{"user"} and defined $cli->{"password"}
defined $cli->{"user"} defined $cli->{"password"}
Truth table0%0/3
A and B
A= defined $cli->{"user"}B= defined $cli->{"password"}
ABresult
0X0
100
111
104 0 $cli->vlog($cli->{logRequest}, "GET $uri");
105 0 my $rsp = $cli->{ua}->request($req);
106 0 ▶ if (!$rsp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsp->is_success) 0 0 0
107 0 $cli->{error} = $rsp->status_line;
108 0 return undef;
109 }
110 0 my $cref = $rsp->content_ref;
111 0 return $class->loadJsonString($cref,utf8=>!utf8::is_utf8($$cref));
112 }
113  
114 ## $obj_or_undef = $cli->jpost($url,\%query_form,$class)
115 ## + wrapper for json http POST requests
116 0 ▶sub jpost {
Subroutine jpost: not called CC=6 SCAR=37.4
Pod undocumented
117 0 my ($cli,$url,$form,$class) = @_;
118 0 ▶ $url //= $cli->{url};
Condition0%0/3
$url //= $cli->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$url //= $cli->{"url"}
$url $cli->{"url"}
Truth table0%0/3
A //= B
A= $urlB= $cli->{"url"}
ABresult
1X1
011
000
119 0 ▶ my $req = POST($url, Content => {%{$cli->{params}//{}}, %$form});
Condition0%0/2
$cli->{"params"} // {}
l!l
00
MC/DC0%0/1
$cli->{"params"} // {}
$cli->{"params"}
Truth table0%0/2
A
A= $cli->{"params"}
Aresult
11
00
120 0 ▶ $req->authorization_basic($cli->{user}, $cli->{password}) if (defined($cli->{user}) && defined($cli->{password}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli->{"user"} and defined $cli->{"password"} 0 0 0
Condition0%0/3
defined $cli->{"user"} and defined $cli->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cli->{"user"} and defined $cli->{"password"}
defined $cli->{"user"} defined $cli->{"password"}
Truth table0%0/3
A and B
A= defined $cli->{"user"}B= defined $cli->{"password"}
ABresult
0X0
100
111
121 0 $cli->vlog($cli->{logRequest}, "POST $url");
122 #$cli->trace("REQUEST = ", $req->as_string);
123 0 my $rsp = $cli->{ua}->request($req);
124 0 ▶ if (!$rsp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsp->is_success) 0 0 0
125 0 $cli->{error} = $rsp->status_line;
126 0 return undef;
127 }
128 0 my $cref = $rsp->content_ref;
129 0 return $class->loadJsonString($cref,utf8=>!utf8::is_utf8($$cref));
130 }
131  
132 ##--------------------------------------------------------------
133 ## dbinfo
134  
135 ## \%info = $cli->dbinfo()
136 ## + adds 'url' key
137 0 ▶sub dbinfo {
Subroutine dbinfo: not called CC=1 SCAR=6.9
Pod documented
138 0 my $cli = shift;
139 0 (my $url = $cli->{url}) =~ s{/profile.*$}{};
140 0 my $info = $cli->jget("$url/info.perl", {},'DiaColloDB::Persistent');
141 0 $info->{url} = "$url/";
142 0 return $info;
143 }
144  
145 ##--------------------------------------------------------------
146 ## Profiling: Generic
147  
148 ## $mprf_or_undef = $cli->profile($relation, %opts)
149 ## + get a relation profile for selected items as a DiaColloDB::Profile::Multi object
150 ## + %opts: as for DiaColloDB::profile()
151 ## + sets $cli->{error} on error
152 0 ▶sub profile {
Subroutine profile: not called CC=1 SCAR=6.9
Pod documented
153 0 my ($cli,$rel,%opts) = @_;
154 0 delete @opts{qw(alemma adate aslice blemma bdate bslice)};
155 0 return $cli->jget($cli->{url}, {profile=>$rel, %opts, format=>'json'},'DiaColloDB::Profile::Multi');
156 }
157  
158 ##--------------------------------------------------------------
159 ## Profiling: extend (pass-2 for multi-clients)
160  
161 ## $mprf = $cli->extend($relation, %opts)
162 ## + get an extension-profile for selected items as a DiaColloDB::Profile::Multi object
163 ## + %opts: as for DiaColloDB::extend()
164 ## + sets $cli->{error} on error
165 0 ▶sub extend {
Subroutine extend: not called CC=1 SCAR=6.9
Pod documented
166 0 my ($cli,$rel,%opts) = @_;
167 0 delete @opts{(qw(alemma adate aslice blemma bdate bslice),
168 qw(eps score kbest cutoff global),
169 qw(onepass),
170 )};
171 0 return $cli->jpost($cli->{url}, {profile=>"extend-$rel", %opts, format=>'json'},'DiaColloDB::Profile::Multi');
172 }
173  
174 ##--------------------------------------------------------------
175 ## Profiling: Comparison (diff)
176  
177 ## $mprf = $cli->compare($relation, %opts)
178 ## + get a relation comparison profile for selected items as a DiaColloDB::Profile::MultiDiff object
179 ## + %opts: as for DiaColloDB::compare()
180 0 ▶sub compare {
Subroutine compare: not called CC=1 SCAR=6.9
Pod documented
181 0 my ($cli,$rel,%opts) = @_;
182 0 return $cli->jget($cli->{url}, {profile=>"d$rel", %opts, format=>'json'},'DiaColloDB::Profile::MultiDiff');
183 }
184  
185 ##==============================================================================
186 ## Footer
187 2 1;
188  
189 __END__
« DiaColloDB/Client/file.pm ↑ Summary DiaColloDB/Compat.pm »