blib/lib/POE/Component/Metabase/Client/Submit.pm

filter: Statementstmt 100% 24 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 32 / 32 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package POE::Component::Metabase::Client::Submit;
2 $POE::Component::Metabase::Client::Submit::VERSION = '0.14';
3 #ABSTRACT: a POE client that submits to Metabase servers
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTTP::Status qw[:constants];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTTP::Message 5.814 (); # for HTTP::Message::decodable() support
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use POE qw[Component::Client::HTTP Component::Client::Keepalive];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use URI;
14  
15 my @valid_args;
16 BEGIN {
17 @valid_args = qw(profile secret uri fact event session http_alias context resolver compress);
18  
19 for my $arg (@valid_args) {
20 no strict 'refs';
21 *$arg = sub { $_[0]->{$arg}; }
22 }
23 }
24  
25 sub submit {
26 my ($class,%opts) = @_;
27 $opts{lc $_} = delete $opts{$_} for keys %opts;
28 my $options = delete $opts{options};
29 my $args = $class->__validate_args(
30 [ %opts ],
31 {
32 ( map { $_ => 0 } @valid_args ),
33 ( map { $_ => 1 } qw(profile secret uri event fact) )
34 } # hehe
35 );
36  
37 my $self = bless $args, $class;
38  
39 Carp::confess( "'profile' argument for $class must be a Metabase::User::Profile" )
40 unless $self->profile->isa('Metabase::User::Profile');
41 Carp::confess( "'secret' argument for $class must be a Metabase::User::secret" )
42 unless $self->secret->isa('Metabase::User::Secret');
43 Carp::confess( "'secret' argument for $class must be a Metabase::Fact" )
44 unless $self->secret->isa('Metabase::Fact');
45  
46 $self->{session_id} = POE::Session->create(
47 object_states => [
48 $self => [ qw(_start _dispatch _submit _response _register _guid_exists _http_req) ],
49 ],
50 heap => $self,
51 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
52 )->ID();
53 return $self;
54 }
55  
56 sub _start {
57 my ($kernel,$session,$sender,$self) = @_[KERNEL,SESSION,SENDER,OBJECT];
58 $self->{session_id} = $session->ID();
59 if ( $kernel == $sender and !$self->{session} ) {
60 Carp::confess "Not called from another POE session and 'session' wasn't set\n";
61 }
62 my $sender_id;
63 if ( $self->{session} ) {
64 if ( my $ref = $kernel->alias_resolve( $self->{session} ) ) {
65 $sender_id = $ref->ID();
66 }
67 else {
68 Carp::confess "Could not resolve 'session' to a valid POE session\n";
69 }
70 }
71 else {
72 $sender_id = $sender->ID();
73 }
74 $kernel->refcount_increment( $sender_id, __PACKAGE__ );
75 $kernel->detach_myself;
76 $self->{sender_id} = $sender_id;
77 if ( $self->{http_alias} ) {
78 my $http_ref = $kernel->alias_resolve( $self->{http_alias} );
79 $self->{http_id} = $http_ref->ID() if $http_ref;
80 }
81 unless ( $self->{http_id} ) {
82 $self->{http_id} = 'metabaseclient' . $$ . $self->{session_id};
83 my $ka;
84 if ( $self->resolver ) {
85 $ka = POE::Component::Client::Keepalive->new(
86 resolver => $self->resolver,
87 );
88 }
89 POE::Component::Client::HTTP->spawn(
90 Alias => $self->{http_id},
91 FollowRedirects => 2,
92 Timeout => 120,
93 Agent => 'Mozilla/5.0 (X11; U; Linux i686; en-US; '
94 . 'rv:1.1) Gecko/20020913 Debian/1.1-1',
95 ( defined $ka ? ( ConnectionManager => $ka ) : () ),
96 );
97 $self->{my_httpc} = 1;
98 }
99 $kernel->yield( '_submit' );
100 return;
101 }
102  
103 sub _submit {
104 my ($kernel,$self) = @_[KERNEL,OBJECT];
105 my $fact = $self->fact;
106 my $path = sprintf 'submit/%s', $fact->type;
107  
108 $fact->set_creator($self->profile->resource)
109 unless $fact->creator;
110  
111 my $req_uri = $self->_abs_uri($path);
112 my $can_decode = HTTP::Message::decodable;
113  
114 my $req = HTTP::Request::Common::POST(
115 $req_uri,
116 ( length $can_decode ? ('Accept-Encoding' => $can_decode) : () ),
117 Content_Type => 'application/json',
118 Accept => 'application/json',
119 Content => JSON->new->encode($fact->as_struct),
120 );
121 $req->authorization_basic($self->profile->resource->guid, $self->secret->content);
122  
123 # Compress it?
124 if ( defined $self->compress and $self->compress ne 'none' ) {
125 my $err;
126 eval { $req->encode( $self->compress ) };
127 if ( $@ ) {
128 $self->{_error} = "Compression error: $@";
129 $self->{success} = 0;
130 $kernel->yield( '_dispatch' );
131 return;
132 }
133 }
134  
135 $kernel->yield( '_http_req', $req, 'submit' );
136 return;
137 }
138  
139 sub _guid_exists {
140 my ($kernel,$self) = @_[KERNEL,OBJECT];
141 my $path = sprintf 'guid/%s', $self->profile->guid;
142 my $req_uri = $self->_abs_uri($path);
143 my $req = HTTP::Request::Common::HEAD( $req_uri );
144 $kernel->yield( '_http_req', $req, 'guid' );
145 return;
146 }
147  
148 sub _register {
149 my ($kernel,$self) = @_[KERNEL,OBJECT];
150  
151 for my $type ( qw/profile secret/ ) {
152 $self->$type->set_creator( $self->$type->resource )
153 unless $self->$type->creator;
154 }
155  
156 my $req_uri = $self->_abs_uri('register');
157 my $can_decode = HTTP::Message::decodable;
158  
159 my $req = HTTP::Request::Common::POST(
160 $req_uri,
161 ( length $can_decode ? ('Accept-Encoding' => $can_decode) : () ),
162 Content_Type => 'application/json',
163 Accept => 'application/json',
164 Content => JSON->new->encode([
165 $self->profile->as_struct, $self->secret->as_struct
166 ]),
167 );
168  
169 $kernel->yield( '_http_req', $req, 'register' );
170 return;
171 }
172  
173 sub _http_req {
174 my ($self,$req,$id) = @_[OBJECT,ARG0,ARG1];
175 $poe_kernel->post(
176 $self->{http_id},
177 'request',
178 '_response',
179 $req,
180 $id,
181 );
182 return;
183 }
184  
185 sub _response {
186 my ($kernel,$self,$request_packet,$response_packet) = @_[KERNEL,OBJECT,ARG0,ARG1];
187 my $tag = $request_packet->[1];
188 my $res = $response_packet->[0];
189 # and punt an event back to the requesting session
190 if ( $tag eq 'submit' and $res->code == HTTP_UNAUTHORIZED ) {
191 $kernel->yield( '_guid_exists' );
192 return;
193 }
194 if ( $tag eq 'guid' ) {
195 if ( $res->is_success ) {
196 $self->{_error} = 'authentication failed';
197 $self->{content} = $res->content;
198 $kernel->yield( '_dispatch' );
199 return;
200 }
201 $kernel->yield( '_register' );
202 return;
203 }
204 if ( $tag eq 'register' ) {
205 unless ( $res->is_success ) {
206 $self->{_error} = 'registration failed';
207 $self->{content} = $res->content;
208 $kernel->yield( '_dispatch' );
209 return;
210 }
211 $kernel->yield( '_submit' );
212 return;
213 }
214 unless ( $res->is_success ) {
215 $self->{_error} = "fact submission failed";
216 }
217 else {
218 $self->{success} = 1;
219 }
220  
221 # decode the content if we requested it
222 if ( defined $request_packet->[0]->header( 'Accept-Encoding' ) ) {
223 eval { $self->{content} = $res->decoded_content( 'charset' => 'none' ) };
224 if ( $@ ) {
225 $self->{_error} = "unable to decode content: $@";
226 $self->{success} = 0;
227 }
228 } else {
229 $self->{content} = $res->content;
230 }
231 $kernel->yield( '_dispatch' );
232 return;
233 }
234  
235 sub _dispatch {
236 my ($kernel,$self) = @_[KERNEL,OBJECT];
237 $kernel->post( $self->{http_id}, 'shutdown' )
238 if $self->{my_httpc};
239 my $ref = {};
240 for ( qw(_error success context content) ) {
241 $ref->{$_} = $self->{$_} if $self->{$_};
242 }
243 $ref->{error} = delete $ref->{_error} if $ref->{_error};
244 $kernel->post( $self->{sender_id}, $self->event, $ref );
245 $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
246 return;
247 }
248  
249 #--------------------------------------------------------------------------#
250 # private methods
251 #--------------------------------------------------------------------------#
252  
253 # Stolen from ::Fact.
254 # XXX: Should refactor this into something in Fact, which we can then rely on.
255 # -- rjbs, 2009-03-30
256 sub __validate_args {
257 my ($self, $args, $spec) = @_;
258 my $hash = (@$args == 1 and ref $args->[0]) ? { %{ $args->[0] } }
259 : (@$args == 0) ? { }
260 : { @$args };
261  
262 my @errors;
263  
264 for my $key (keys %$hash) {
265 push @errors, qq{unknown argument "$key" when constructing $self}
266 unless exists $spec->{ $key };
267 }
268  
269 for my $key (grep { $spec->{ $_ } } keys %$spec) {
270 push @errors, qq{missing required argument "$key" when constructing $self}
271 unless defined $hash->{ $key };
272 }
273  
274 Carp::confess(join qq{\n}, @errors) if @errors;
275  
276 return $hash;
277 }
278  
279 sub _abs_uri {
280 my ($self, $str) = @_;
281 my $req_uri = URI->new($str)->abs($self->uri);
282 }
283  
284 sub _error {
285 my ($self, $res, $prefix) = @_;
286 $prefix ||= "unrecognized error";
287 if ( ref($res) && $res->header('Content-Type') eq 'application/json') {
288 my $entity = JSON->new->decode($res->content);
289 return "$prefix\: $entity->{error}";
290 }
291 else {
292 return "$prefix\: " . $res->message;
293 }
294 }
295  
296 'Submit this';
297  
298 __END__
↑ Summary