File Coverage

blib/lib/Konfidi/Client.pm
Criterion Covered Total %
statement 70 70 100.0
branch 20 24 83.3
condition 6 6 100.0
subroutine 12 12 100.0
pod 4 4 100.0
total 112 116 96.5


line stmt bran cond sub pod time code
1             # <@LICENSE>
2             # Licensed under the Apache License, Version 2.0 (the "License");
3             # you may not use this file except in compliance with the License.
4             # You may obtain a copy of the License at
5             #
6             # http://www.apache.org/licenses/LICENSE-2.0
7             #
8             # Unless required by applicable law or agreed to in writing, software
9             # distributed under the License is distributed on an "AS IS" BASIS,
10             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11             # See the License for the specific language governing permissions and
12             # limitations under the License.
13             #
14              
15             package Konfidi::Client;
16 3     3   363338 use warnings;
  3         9  
  3         102  
17 3     3   18 use strict;
  3         7  
  3         177  
18              
19             =head1 NAME
20              
21             Konfidi::Client - Interact with a Konfidi TrustServer
22              
23             =head1 DESCRIPTION
24              
25             Konfidi is a trust framework that uses topical trust values from a social network of authenticated people. When you see a document (e.g email, webpage) from someone you do not know, but he/she is in the network, Konfidi will compute an inferred trust value for you. For more information, see L
26              
27             =head1 VERSION
28              
29             Version 1.0.4
30              
31             =cut
32              
33             our $VERSION = '1.0.4';
34              
35             =head1 SYNOPSIS
36              
37             use Konfidi::Client;
38             use Error qw(:try);
39              
40             my $k = Konfidi::Client->new();
41             $k->server('http://test-server.konfidi.org');
42             try {
43             my $response = $k->query($truster_40char_pgp_fingerprint, $trusted_40char_pgp_fingerprint, 'http://www.konfidi.org/ns/topics/0.0#internet-communication');
44             } catch Konfidi::Client::Error with {
45             my $E = shift;
46             die "Couldn't query the trustserver: $E";
47             };
48             ...
49              
50             See L for error handling documentation
51              
52             =head1 METHODS
53              
54             =head2 C
55              
56             Create a new C
57              
58             =cut
59              
60 3     3   19 use Carp;
  3         10  
  3         227  
61 3     3   5428 use LWP::UserAgent;
  3         188866  
  3         117  
62 3     3   37 use URI::Escape;
  3         6  
  3         264  
63 3     3   2381 use Konfidi::Client::Error;
  3         39  
  3         32  
64 3     3   3360 use Konfidi::Response;
  3         9  
  3         3155  
65              
66             sub new {
67 3     3 1 2093 my $this = shift;
68 3   100     17 my $class = ref($this) || $this;
69 3 100       23 croak unless $class;
70 2         4 my $self = {};
71 2         5 bless $self, $class;
72 2         8 $self->_initialize();
73 2         6 return $self;
74             }
75              
76             sub _initialize {
77 2     2   3 my $self = shift;
78 2         33 $self->{server} = undef;
79             #$self->{strategy} = 'Multiplicative2';
80 2         3 return;
81             }
82              
83             =head2 C
84              
85             Get or set the server to use. Required. For example: C
86              
87             =cut
88              
89             sub server {
90 10     10 1 13864 my $self = shift;
91 10 100       45 if (@_) {
92 7         35 $self->{server} = shift;
93 7 100 100     112 if ($self->{server} && substr($self->{server}, -1, 1) eq '/') {
94 5         20 chop $self->{server};
95             }
96             }
97 10         40 return $self->{server};
98             }
99              
100             =head2 C
101              
102             Get or set which trust propogation strategy to use. No default (server decides)
103              
104             =cut
105              
106             sub strategy {
107 2     2 1 4 my $self = shift;
108 2 100       7 if (@_) { $self->{strategy} = shift;}
  1         3  
109 2         7 return $self->{strategy};
110             }
111              
112             =head2 C
113              
114             Query the Konfidi Trustserver, using the default or set C and C values. C<$source> and C<$sink> must be 40 character long OpenPGP fingerprint identifiers. The only topic currently in use is C<'http://www.konfidi.org/ns/topics/0.0#internet-communication'>.
115             Returns a L or throws a L upon error
116              
117             =cut
118              
119             sub query {
120 6     6 1 32000 my $self = shift;
121 6         84 my ($source, $sink, $topic) = @_;
122            
123 6 100       51 throw Konfidi::Client::Error("You must set the 'server'") unless $self->{server};
124            
125 5         2496 my $ua = LWP::UserAgent->new;
126 5         25189 $ua->agent("Perl Konfidi::Client/$VERSION");
127            
128 5         633 my $url = $self->{server} . '/query?';
129 5 50       29 if (defined($self->{'strategy'})) {
130 5         106 $url .= 'strategy=' . uri_escape($self->{'strategy'}) . '&';
131             }
132 5 50       266 if (defined($source)) {
133 5         43 $url .= "source=" . uri_escape($source) . '&';
134             }
135 5 50       119 if (defined($sink)) {
136 5         18 $url .= "sink=" . uri_escape($sink) . '&';
137             }
138 5 50       83 if (defined($topic)) {
139 5         17 $url .= "topic=" . uri_escape($topic) . '&';
140             }
141 5         934 my $req = HTTP::Request->new(GET => $url);
142             #$req->content_type('application/x-www-form-urlencoded');
143             #$req->content('query=libwww-perl&mode=dist');
144            
145 5         42601 my $res = $ua->request($req);
146            
147 5         428900 my $server_version = $res->header('X-Konfidi-Frontend-Version');
148             # TODO compare server_version
149            
150 5 100       319 if ($res->is_success) {
151             # mod_python errors are text/html
152             # and we don't know how to handle anything else anyway
153 4 100       197 if ($res->header('Content-Type') ne 'text/plain') {
154 1         100 throw Konfidi::Client::Error("Server had a bad error:\n " . $res->content);
155             }
156            
157 3         373 my $response = Konfidi::Response->new();
158 3         42 foreach (split(/\n/,$res->content)) {
159 7         107 my ($key, $val) = split /:/;
160 7         35 $val =~ s/^\s+//; # ltrim
161 7         103 $response->{$key} = $val;
162             }
163            
164 3 100       23 if ($response->{'Error'}) {
165 1         53 throw Konfidi::Client::Error("Server had an error: " . $response->{'Error'});
166             } else {
167 2         4882 return $response;
168             }
169             }else {
170 1         16 throw Konfidi::Client::Error("Could not reach server; HTTP status: " . $res->status_line);
171             }
172             }
173              
174             1;
175              
176             __END__