File Coverage

blib/lib/Paws/Net/MultiplexCaller.pm
Criterion Covered Total %
statement 13 14 92.8
branch 5 6 83.3
condition n/a
subroutine 4 5 80.0
pod 0 4 0.0
total 22 29 75.8


line stmt bran cond sub pod time code
1             package Paws::Net::MultiplexCaller;
2 4     4   4544639 use Moose;
  4         425136  
  4         34  
3             with 'Paws::Net::CallerRole';
4              
5             our $VERSION = '0.03';
6              
7             # TODO: HashRef of things that do Paws::Net::CallerRole
8             has caller_for => (is => 'ro', isa => 'HashRef', required => 1);
9             # TODO: thing that does Paws::Net::CallerRole or Undef
10             has default_caller => (is => 'ro', isa => 'Object');
11              
12             sub get_implementation {
13 22     22 0 94 my ($self, $service) = @_;
14 22 100       1254 return $self->caller_for->{ $service } if (defined $self->caller_for->{ $service });
15 3 100       106 return $self->default_caller if (defined $self->default_caller);
16 1         19 die "Can't find a caller for $service";
17             }
18              
19             sub do_call {
20 22     22 0 2094415 my ($self, $service, $call_object) = @_;
21 22         130 return $self->get_implementation($self->service_from_callobject($call_object))
22             ->do_call($service, $call_object);
23             }
24              
25             sub caller_to_response {
26             #my ($self, $service, $call_object, $status, $content, $headers) = @_;
27 0     0 0 0 die "Die caller_to_response is not needed on the Multiplex caller";
28             }
29              
30             sub service_from_callobject {
31 22     22 0 93 my ($self, $call_object) = @_;
32 22         147 my ($svc_name) = ($call_object->meta->name =~ m/^Paws::(\w+)::/);
33 22 50       851 die "$call_object doesn't seem to be a Paws::SERVICE::CALL" if (not defined $svc_name);
34 22         136 return $svc_name;
35             }
36              
37             1;
38             ### main pod documentation begin ###
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             Paws::Net::MultiplexCaller - Control routing of services to Paws callers
45              
46             =head1 SYNOPSIS
47              
48             use Paws::Net::MultiplexCaller;
49             use Paws::Net::LWPCaller;
50             use Paws::Net::MockCaller;
51              
52             my $paws = Paws->new(
53             config => {
54             caller => Paws::Net::MultiplexCaller->new(
55             caller_for => {
56             SQS => Paws::Net::LWPCaller->new(),
57             EC2 => Paws::Net::MockCaller->new(...),
58             },
59             default_caller => Paws::Net::Caller->new
60             )
61             }
62             );
63              
64             # SQS methods will be called with LWPCaller
65             # $paws->service('SQS', region => 'eu-west-1')->CreateQueue
66             # EC2 with the MockCaller
67             # $paws->service('EC2', region => 'us-east-1')->RunInstances
68             # others will be called with the default Paws::Net::Caller
69             # $paws->service('DynamoDB', region => 'us-east-1')->CreateTable
70              
71             =head1 DESCRIPTION
72              
73             By default, Paws routes all calls to service methods (RunInstances for EC2 and CreateQueue for SQS, for example) to the configured caller (that normally will do HTTP requests to the backing services). All calls go to the one and only caller.
74              
75             Paws::Net::MultiplexCaller is one of Paws' pluggable callers whose only purpose is to let you route requests to different callers. So you can do special things like:
76              
77             =over
78              
79             =item Use a special caller for just one service
80              
81             =item Emulate services without doing HTTP calls
82              
83             =back
84              
85             =head1 ATTRIBUTES
86              
87             Attributes are initialized in the constructor
88              
89             =head2 caller_for
90              
91             Is a Hashref which keys are the names of the services to route for. It's values are instances of objects that can handle Paws calls (it's pluggable callers). Note that you can pass the same object for different services
92              
93             my $caller2 = Paws::Net::LWPCaller->new;
94             my $paws = Paws->new(
95             config => {
96             caller => Paws::Net::MultiplexCaller->new(
97             caller_for => {
98             SQS => $caller2,
99             EC2 => $caller2,
100             },
101             )
102             }
103             );
104              
105             As opposed to
106              
107             my $paws = Paws->new(
108             config => {
109             caller => Paws::Net::MultiplexCaller->new(
110             caller_for => {
111             SQS => Paws::Net::LWPCaller->new,
112             EC2 => Paws::Net::LWPCaller->new,
113             },
114             )
115             }
116             );
117              
118             Where there would be two independant instances of LWPCaller (consuming double memory), or leading
119             to unexpected results (should the callers track some sort of state, like L<Paw::Net::MockCaller>)
120              
121             =head2 default_caller
122              
123             If not specified, any call to a service that is not in C<caller_for> will fail to complete, raising
124             an exception.
125              
126             If specified, Paws will route any service that is not in C<caller_for> to this caller, that should
127             be initialized to an instance of any of Paws' pluggable callers.
128              
129             =head1 Practical use
130              
131             On CPAN you can find L<Paws::Kinesis::MemoryCaller>, that emulates the AWS Kinesis service. Using
132             that caller will not let you call other AWS services. With C<Paws::Net::MultiplexCaller> we can
133             solve that:
134              
135             my $paws = Paws->new(
136             config => {
137             caller => Paws::Net::MultiplexCaller->new(
138             caller_for => {
139             Kinesis => Paws::Kinesis::MemoryCaller->new(),
140             },
141             default_caller => Paws::Net::Caller->new
142             )
143             }
144             );
145              
146             You can also combine the multiplex caller with L<PawsX::FakeImplementation::Instance> to easily
147             fake some AWS services for your testing purposes.
148              
149             =head1 AUTHOR
150              
151             Jose Luis Martinez
152             CPAN ID: JLMARTIN
153             CAPSiDE
154             jlmartinez@capside.com
155              
156             =head1 SEE ALSO
157              
158             L<Paws>
159              
160             L<Paws::Kinesis::MemoryCaller>
161              
162             L<PawsX::FakeImplementation::Instance>
163              
164             =head1 BUGS and SOURCE
165              
166             The source code is located here: L<https://github.com/pplu/paws-net-multiplexcaller>
167              
168             Please report bugs to: L<https://github.com/pplu/paws-net-multiplexcaller/issues>
169              
170             =head1 COPYRIGHT and LICENSE
171              
172             Copyright (c) 2017 by CAPSiDE
173              
174             This code is distributed under the Apache 2 License. The full text of the license can be found in the LICENSE file included with this module.
175              
176             =cut