| 1 | package Froody::Invoker::Remote; | ||||||||||||||||||||||||||||
| 2 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use base qw(Froody::Invoker); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 21 | __PACKAGE__->mk_accessors(qw( url )); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Froody::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use LWP::Simple (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 21 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 21 | our $VERSION = 0.01; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 21 | ▶ | use Froody::Response::String; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 21 | ▶ | use Froody::Response::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 21 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 21 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 21 | ▶ | use Encode qw(encode_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 21 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub invoke | ||||||||||||||||||||||||||||
| 24 | 13 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
invoke: called CC=13 SCAR=26.1
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 26 | 13 | my $method = shift; | |||||||||||||||||||||||||||
| 27 | 13 | my $params = shift; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # build a URI | ||||||||||||||||||||||||||||
| 30 | 13 | my $uri = URI->new($self->url); | |||||||||||||||||||||||||||
| 31 | 13 | my $multi = undef; | |||||||||||||||||||||||||||
| 32 | 13 | for my $key (keys %$params) { | |||||||||||||||||||||||||||
| 33 | 5 | my $arg_data = $method->arguments->{$key}; | |||||||||||||||||||||||||||
| 34 | 5 | my $value = delete $params->{$key}; | |||||||||||||||||||||||||||
| 35 | # HTTP::Request::Common doesn't like undef | ||||||||||||||||||||||||||||
| 36 | 5 | ▶ | $value = '' unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 21 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 5 | ▶ | if ($arg_data->{type}[0] eq 'multipart') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | if (ref($value) eq 'ARRAY' and ref($value->[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $value eq "ARRAY" and ref $value->[0]
MC/DC0%0/2
ref $value eq "ARRAY" and ref $value->[0]
ref $value eq "ARRAY"
ref $value->[0]
Truth table66%2/3
A and B
A= ref $value eq "ARRAY"B= ref $value->[0]
| |||||||||||||||||||||||||||||
| 40 | 0 | $value = $value->[0]; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | $multi ||= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$multi ||= 1
MC/DC0%0/1
$multi ||= 1
$multi
Truth table50%1/2
A
A= $multi
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | if (UNIVERSAL::isa($value, "Froody::Upload")) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | 1 | $value = [ $value->filename, $value->client_filename ]; | |||||||||||||||||||||||||||
| 45 | } elsif (!ref($value)) { | ||||||||||||||||||||||||||||
| 46 | 0 | $value = [$value]; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | ▶ | } elsif ($arg_data->{type}[0] eq 'csv' and ref($value) eq 'ARRAY') { | |||||||||||||||||||||||||||
|
Condition66%2/3
$arg_data->{"type"}[0] eq "csv" and ref $value eq "ARRAY"
MC/DC50%1/2
$arg_data->{"type"}[0] eq "csv" and ref $value eq "ARRAY"
$arg_data->{"type"}[0] eq "csv"
ref $value eq "ARRAY"
Truth table66%2/3
A and B
A= $arg_data->{"type"}[0] eq "csv"B= ref $value eq "ARRAY"
| |||||||||||||||||||||||||||||
| 49 | 1 | $value = join(",", @$value); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 5 | ▶ | $params->{$key} = ref $value ? $value : encode_utf8($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 13 | $params->{method} = $method->full_name; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Create a request | ||||||||||||||||||||||||||||
| 56 | 13 | my $req = POST $uri, | |||||||||||||||||||||||||||
| 57 | Content_Type => 'form-data', | ||||||||||||||||||||||||||||
| 58 | Content => [ %$params ]; | ||||||||||||||||||||||||||||
| 59 | 13 | my $res = $ua->request($req); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 13 | ▶ | unless ($res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 3 | Froody::Error->throw("froody.invoke.remote", "Bad response from remote server - ". $res->status_line); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # return whatever we got | ||||||||||||||||||||||||||||
| 66 | 10 | my $frs = Froody::Response::String->new(); | |||||||||||||||||||||||||||
| 67 | 10 | $frs->set_bytes($res->content); | |||||||||||||||||||||||||||
| 68 | 10 | $frs->structure($method); | |||||||||||||||||||||||||||
| 69 | 10 | return $frs; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sub source { | ||||||||||||||||||||||||||
|
Subroutine
source: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 1 | shift->url; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 NAME | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Froody::Invoker::Remote - invoker that calls methods remotely | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | use Froody::Invoker::Remote; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my $remote = Froody::Invoker::Remote | ||||||||||||||||||||||||||||
| 85 | ->new() | ||||||||||||||||||||||||||||
| 86 | ->url("http://someserver.com/restendpoint/"); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | my $method = Froody::Method->new() | ||||||||||||||||||||||||||||
| 89 | ->full_name("fred.bar.baz") | ||||||||||||||||||||||||||||
| 90 | ->invoker($remote); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $response = $method->call({ foo => "wibble" }); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | An Invoker that calls a remote server to get the Froody::Response. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 Accessors | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =over | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item url | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | The URL of the REST endpoint. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =item source | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Returns where this invoker originated | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =back | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 BUGS | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | None known. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Please report any bugs you find via the CPAN RT system. | ||||||||||||||||||||||||||||
| 117 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||
| 124 | on this project. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 127 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | L<Froody>, L<Froody::Invoker> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 21 | 1; | |||||||||||||||||||||||||||