| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::HTTP::Spore::Role::Request; | 
| 2 |  |  |  |  |  |  | $Net::HTTP::Spore::Role::Request::VERSION = '0.07'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: make HTTP request | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 21 |  |  | 21 |  | 9213 | use Try::Tiny; | 
|  | 21 |  |  |  |  | 52 |  | 
|  | 21 |  |  |  |  | 1099 |  | 
| 6 | 21 |  |  | 21 |  | 125 | use Moose::Role; | 
|  | 21 |  |  |  |  | 43 |  | 
|  | 21 |  |  |  |  | 156 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 21 |  |  | 21 |  | 110407 | use Net::HTTP::Spore::Request; | 
|  | 21 |  |  |  |  | 78 |  | 
|  | 21 |  |  |  |  | 11691 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub http_request { | 
| 11 | 22 |  |  | 22 | 1 | 73 | my ( $self, $env ) = @_; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 22 |  |  |  |  | 59 | my ($request, $response); | 
| 14 | 22 |  |  |  |  | 290 | $request = Net::HTTP::Spore::Request->new($env); | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 22 |  |  |  |  | 15907 | my @middlewares; | 
| 17 | 22 |  |  |  |  | 812 | foreach my $mw ( $self->middlewares ) { | 
| 18 | 39 |  |  |  |  | 73 | my $res; | 
| 19 |  |  |  |  |  |  | try { | 
| 20 | 39 |  |  | 39 |  | 1986 | $res = $mw->($request); | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  | catch { | 
| 23 | 1 |  |  | 1 |  | 37 | $res = $request->new_response( 599, [], { error => $_, } ); | 
| 24 | 39 |  |  |  |  | 357 | }; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 39 | 100 | 100 |  |  | 1871 | if ( ref $res && ref $res eq 'CODE' ) { | 
|  |  | 100 | 66 |  |  |  |  | 
| 27 | 9 |  |  |  |  | 34 | push @middlewares, $res; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | elsif ( ref $res && ref $res eq 'Net::HTTP::Spore::Response' ) { | 
| 30 | 22 | 100 |  |  |  | 88 | return $res if ($res->status == 599); | 
| 31 | 21 |  |  |  |  | 53 | $response = $res; | 
| 32 | 21 |  |  |  |  | 50 | last; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | return | 
| 37 | 21 | 50 |  |  |  | 199 | $self->_execute_middlewares_on_response( $response, @middlewares ) | 
| 38 |  |  |  |  |  |  | if defined $response; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 |  |  |  |  | 0 | $response = $self->_request($request); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  | 0 | return $self->_execute_middlewares_on_response( $response, @middlewares ); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub _execute_middlewares_on_response { | 
| 46 | 21 |  |  | 21 |  | 75 | my ($self, $response, @middlewares) = @_; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 21 |  |  |  |  | 58 | foreach my $mw ( reverse @middlewares ) { | 
| 49 | 9 |  |  |  |  | 17 | my ($res, $error); | 
| 50 |  |  |  |  |  |  | try { | 
| 51 | 9 |  |  | 9 |  | 362 | $res = $mw->($response); | 
| 52 |  |  |  |  |  |  | }catch{ | 
| 53 | 0 |  |  | 0 |  | 0 | $error = 1; | 
| 54 | 0 |  |  |  |  | 0 | $response->code(599); | 
| 55 | 0 |  |  |  |  | 0 | $response->body({error => $_, body=>$response->body}); | 
| 56 | 9 |  |  |  |  | 73 | }; | 
| 57 | 9 | 50 | 66 |  |  | 806 | $response = $res | 
|  |  |  | 33 |  |  |  |  | 
| 58 |  |  |  |  |  |  | if ( defined $res | 
| 59 |  |  |  |  |  |  | && Scalar::Util::blessed($res) | 
| 60 |  |  |  |  |  |  | && $res->isa('Net::HTTP::Spore::Response') ); | 
| 61 | 9 | 50 |  |  |  | 30 | last if $error; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 21 |  |  |  |  | 125 | $response; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub _request { | 
| 68 | 0 |  |  | 0 |  |  | my ($self, $request) = @_; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | my $finalized_request = $request->finalize(); | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | $self->_debug_request($request, $finalized_request); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 |  |  |  |  |  | my $result = $self->request($finalized_request); | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | my $response = $request->new_response( | 
| 77 |  |  |  |  |  |  | $result->code, | 
| 78 |  |  |  |  |  |  | $result->headers, | 
| 79 |  |  |  |  |  |  | $result->content, | 
| 80 |  |  |  |  |  |  | ); | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  |  | $self->_debug_response($response); | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  |  | return $response; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub _debug_request { | 
| 88 | 0 |  |  | 0 |  |  | my ( $self, $request, $finalized_request ) = @_; | 
| 89 | 0 | 0 |  |  |  |  | return unless $self->trace; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  |  | $self->_trace_msg( '> %s %s%s', $request->method, $request->script_name, $request->path ); | 
| 92 | 0 |  |  |  |  |  | $self->_trace_msg( '> Host: %s', $request->host ); | 
| 93 |  |  |  |  |  |  | $self->_trace_msg( '> Query String: %s', $request->env->{QUERY_STRING} ) | 
| 94 | 0 | 0 |  |  |  |  | if defined $request->env->{QUERY_STRING}; | 
| 95 | 0 | 0 |  |  |  |  | $self->_trace_msg( '> Content: %s', $request->content ) | 
| 96 |  |  |  |  |  |  | if defined $request->content; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 |  |  |  |  |  | foreach my $key ( $request->headers->header_field_names ) { | 
| 99 | 0 |  |  |  |  |  | $self->_trace_msg( '> %s: %s', $key, $request->header($key) ); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub _debug_response { | 
| 104 | 0 |  |  | 0 |  |  | my ($self, $response) = @_; | 
| 105 | 0 | 0 |  |  |  |  | return unless $self->trace; | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | foreach my $key ( $response->headers->header_field_names ) { | 
| 108 | 0 |  |  |  |  |  | $self->_trace_msg( '< %s: %s', $key, $response->header($key) ); | 
| 109 |  |  |  |  |  |  | } | 
| 110 | 0 |  |  |  |  |  | $self->_trace_verbose($response->body); | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | 1; | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | __END__ | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =pod | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =encoding UTF-8 | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head1 NAME | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | Net::HTTP::Spore::Role::Request - make HTTP request | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =head1 VERSION | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | version 0.07 | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head2 METHODS | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =over 4 | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =item B<http_request> | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =back | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =head2 ATTRIBUTES | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =over 4 | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =item B<api_base_url> | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =back | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =head1 AUTHORS | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =over 4 | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =item * | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | Franck Cuny <franck.cuny@gmail.com> | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =item * | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Ash Berlin <ash@cpan.org> | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =item * | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Ahmad Fatoum <athreef@cpan.org> | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =back | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | This software is copyright (c) 2012 by Linkfluence. | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 172 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =cut |