1 package Catalyst::View::Component::SubInclude::HTTP;
4 use namespace::clean -except => 'meta';
6 use List::MoreUtils 'firstval';
10 $VERSION = eval $VERSION;
13 isa => 'Str', is => 'ro', default => 'GET',
17 isa => 'Int', is => 'ro', default => 10,
21 isa => 'Str', is => 'ro', required => 0,
25 isa => 'HashRef', is => 'ro', required => 0,
29 is => 'ro', lazy => 1, builder => '_build_user_agent',
32 sub _build_user_agent {
34 return LWP::UserAgent->new(
36 timeout => $self->ua_timeout,
40 sub generate_subinclude {
41 my ($self, $c, $path, $args) = @_;
42 my $error_msg_prefix = "SubInclude for $path failed: ";
43 my $base_uri = $self->base_uri || $c->req->base;
44 my $uri_map = $self->uri_map || { q{/} => $base_uri };
45 $base_uri = $uri_map->{ firstval { $path =~ s/^$_// } keys %$uri_map };
47 my $uri = URI->new(join(q{/}, $base_uri, $path));
48 my $req_method = q{_} . lc $self->http_method . '_request';
51 if ( $self->can($req_method) ) {
52 $response = $self->$req_method($uri, $args);
55 confess $self->http_method . ' not supported';
57 if ($response->is_success) {
58 return $response->content;
61 $c->log->info($error_msg_prefix . $response->status_line);
67 my ( $self, $uri, $args) = @_;
68 $uri->query_form($args);
69 return $self->user_agent->get($uri);
73 my ( $self, $uri, $args ) = @_;
74 return $self->user_agent->post($uri, $args);
77 __PACKAGE__->meta->make_immutable;
85 Catalyst::View::Component::SubInclude::HTTP - HTTP plugin for C::V::Component::SubInclude
91 package MyApp::View::TT;
94 extends 'Catalyst::View::TT';
95 with 'Catalyst::View::Component::SubInclude';
98 subinclude_plugin => 'HTTP::GET',
102 http_method => 'GET',
105 '/my/' => 'http://localhost:5000/',
110 http_method => 'POST',
113 '/foo/' => 'http://www.foo.com/',
119 Then, somewhere in your templates:
121 [% subinclude('/my/widget') %]
123 [% subinclude_using('HTTP::POST', '/foo/path', { foo => 1 }) %]
127 C<Catalyst::View::Component::SubInclude::HTTP> does HTTP requests (currently
128 using L<LWP::UserAgent>) and uses the responses to render subinclude contents.
132 The configuration is passed in the C<subinclude> key based on your plugin name
133 which can be arbitrary.
139 Required just in case your plugin name differs from C<HTTP>.
143 Accepts C<GET> and C<POST> as values. The default one is C<GET>.
147 This lazily builds a L<LWP::UserAgent> obj, however you can pass a different
148 user agent obj that implements the required API.
152 User Agent's timeout config param. Defaults to 10 seconds.
156 This expects a HashRef in order to map paths to different URLs.
160 Used only if C<uri_map> is C<undef> and defaults to C<< $c->request->base >>.
166 =head2 C<generate_subinclude( $c, $path, $args )>
168 Note that C<$path> should be the relative path.
172 L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>
176 Wallace Reis C<< <wreis@cpan.org> >>
180 Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
182 =head1 COPYRIGHT & LICENSE
184 Copyright (c) 2010 Wallace Reis.
186 This program is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.