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

filter: Statementstmt 100% 21 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 28 / 28 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  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::Status qw[:constants];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use POE qw[Component::Client::HTTP Component::Client::Keepalive];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use URI;
11 use vars qw[$VERSION];
12  
13 $VERSION = '0.12';
14  
15 my @valid_args;
16 BEGIN {
17 @valid_args = qw(profile secret uri fact event session http_alias context resolver);
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  
113 my $req = HTTP::Request::Common::POST(
114 $req_uri,
115 Content_Type => 'application/json',
116 Accept => 'application/json',
117 Content => JSON->new->encode($fact->as_struct),
118 );
119 $req->authorization_basic($self->profile->resource->guid, $self->secret->content);
120 $kernel->yield( '_http_req', $req, 'submit' );
121 return;
122 }
123  
124 sub _guid_exists {
125 my ($kernel,$self) = @_[KERNEL,OBJECT];
126 my $path = sprintf 'guid/%s', $self->profile->guid;
127 my $req_uri = $self->_abs_uri($path);
128 my $req = HTTP::Request::Common::HEAD( $req_uri );
129 $kernel->yield( '_http_req', $req, 'guid' );
130 return;
131 }
132  
133 sub _register {
134 my ($kernel,$self) = @_[KERNEL,OBJECT];
135 my $req_uri = $self->_abs_uri('register');
136  
137 for my $type ( qw/profile secret/ ) {
138 $self->$type->set_creator( $self->$type->resource )
139 unless $self->$type->creator;
140 }
141  
142 my $req = HTTP::Request::Common::POST(
143 $req_uri,
144 Content_Type => 'application/json',
145 Accept => 'application/json',
146 Content => JSON->new->encode([
147 $self->profile->as_struct, $self->secret->as_struct
148 ]),
149 );
150  
151 $kernel->yield( '_http_req', $req, 'register' );
152 return;
153 }
154  
155 sub _http_req {
156 my ($self,$req,$id) = @_[OBJECT,ARG0,ARG1];
157 $poe_kernel->post(
158 $self->{http_id},
159 'request',
160 '_response',
161 $req,
162 $id,
163 );
164 return;
165 }
166  
167 sub _response {
168 my ($kernel,$self,$request_packet,$response_packet) = @_[KERNEL,OBJECT,ARG0,ARG1];
169 my $tag = $request_packet->[1];
170 my $res = $response_packet->[0];
171 # and punt an event back to the requesting session
172 if ( $tag eq 'submit' and $res->code == HTTP_UNAUTHORIZED ) {
173 $kernel->yield( '_guid_exists' );
174 return;
175 }
176 if ( $tag eq 'guid' ) {
177 if ( $res->is_success ) {
178 $self->{_error} = 'authentication failed';
179 $self->{content} = $res->content;
180 $kernel->yield( '_dispatch' );
181 return;
182 }
183 $kernel->yield( '_register' );
184 return;
185 }
186 if ( $tag eq 'register' ) {
187 unless ( $res->is_success ) {
188 $self->{_error} = 'registration failed';
189 $self->{content} = $res->content;
190 $kernel->yield( '_dispatch' );
191 return;
192 }
193 $kernel->yield( '_submit' );
194 return;
195 }
196 unless ( $res->is_success ) {
197 $self->{_error} = "fact submission failed";
198 }
199 else {
200 $self->{success} = 1;
201 }
202 $self->{content} = $res->content;
203 $kernel->yield( '_dispatch' );
204 return;
205 }
206  
207 sub _dispatch {
208 my ($kernel,$self) = @_[KERNEL,OBJECT];
209 $kernel->post( $self->{http_id}, 'shutdown' )
210 if $self->{my_httpc};
211 my $ref = {};
212 for ( qw(_error success context content) ) {
213 $ref->{$_} = $self->{$_} if $self->{$_};
214 }
215 $ref->{error} = delete $ref->{_error} if $ref->{_error};
216 $kernel->post( $self->{sender_id}, $self->event, $ref );
217 $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
218 return;
219 }
220  
221 #--------------------------------------------------------------------------#
222 # private methods
223 #--------------------------------------------------------------------------#
224  
225 # Stolen from ::Fact.
226 # XXX: Should refactor this into something in Fact, which we can then rely on.
227 # -- rjbs, 2009-03-30
228 sub __validate_args {
229 my ($self, $args, $spec) = @_;
230 my $hash = (@$args == 1 and ref $args->[0]) ? { %{ $args->[0] } }
231 : (@$args == 0) ? { }
232 : { @$args };
233  
234 my @errors;
235  
236 for my $key (keys %$hash) {
237 push @errors, qq{unknown argument "$key" when constructing $self}
238 unless exists $spec->{ $key };
239 }
240  
241 for my $key (grep { $spec->{ $_ } } keys %$spec) {
242 push @errors, qq{missing required argument "$key" when constructing $self}
243 unless defined $hash->{ $key };
244 }
245  
246 Carp::confess(join qq{\n}, @errors) if @errors;
247  
248 return $hash;
249 }
250  
251 sub _abs_uri {
252 my ($self, $str) = @_;
253 my $req_uri = URI->new($str)->abs($self->uri);
254 }
255  
256 sub _error {
257 my ($self, $res, $prefix) = @_;
258 $prefix ||= "unrecognized error";
259 if ( ref($res) && $res->header('Content-Type') eq 'application/json') {
260 my $entity = JSON->new->decode($res->content);
261 return "$prefix\: $entity->{error}";
262 }
263 else {
264 return "$prefix\: " . $res->message;
265 }
266 }
267  
268 'Submit this';
269 __END__
↑ Summary