| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||
| 3 | Froody::SimpleClient | ||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||
| 11 | package Froody::SimpleClient; | ||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use base qw( Class::Accessor::Chained::Fast ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | __PACKAGE__->mk_accessors(qw( endpoint )); | |||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use JSON::Syck; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use Encode qw( encode_utf8 ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||
| 24 | =over | ||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||
| 26 | =item endpoint | ||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||
| 28 | =back | ||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||
| 32 | =over | ||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||
| 34 | =item new( endpoint, [ timeout => 30 ] ) | ||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||
| 36 | We take an endpoint here. Optionally, we also take a timeout in seconds. | ||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||
| 42 | 1 | my $endpoint = shift; | |||||||||||||||||||||||||||||||||||||||
| 43 | 1 | my $self = $class->SUPER::new({ endpoint => $endpoint }); | |||||||||||||||||||||||||||||||||||||||
| 44 | 1 | $self->{ua} = LWP::UserAgent->new( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 45 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||
| 48 | =item call( method, arg => value, arg => value, etc... ) | ||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||
| 52 | 11 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 53 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||
| 54 | 11 | my $method = shift; | |||||||||||||||||||||||||||||||||||||||
| 55 | 11 | ▶ | my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||
| 57 | # we want json | ||||||||||||||||||||||||||||||||||||||||
| 58 | 11 | $args{_type} = "json"; | |||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||
| 60 | 11 | my $response = $self->call_raw($method, %args); | |||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||
| 62 | # parse as Frooy/JSON response | ||||||||||||||||||||||||||||||||||||||||
| 63 | 9 | my $data = eval { | |||||||||||||||||||||||||||||||||||||||
| 64 | 9 | local $JSON::Syck::ImplicitUnicode = 1; # bah. | |||||||||||||||||||||||||||||||||||||||
| 65 | 9 | JSON::Syck::Load( $response ); | |||||||||||||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||||||||||||||
| 67 | 9 | ▶ | unless (ref $data eq "HASH") { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | die "Error parsing ".$response.": $@"; | |||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | ▶ | unless ($data->{stat} eq 'ok') { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | my $error = $data->{data}; | |||||||||||||||||||||||||||||||||||||||
| 72 | 3 | Froody::Error->throw(delete $error->{code}, delete $error->{msg}, $error); | |||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | return $data->{data}; | |||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||
| 77 | =item call_raw( method, args ) | ||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||
| 79 | makes a raw call, returns the exact thing returned by the server. | ||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||
| 83 | 11 | ▶ | sub call_raw { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
call_raw: called CC=16 SCAR=35.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 84 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||
| 85 | 11 | my $method = shift; | |||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||
| 88 | 11 | ▶ | die "no endpoint set" unless $self->endpoint; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 89 | 11 | ▶ | die "no UA" unless $self->{ua}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||
| 91 | # fudge args so we can do the Right Thing with lists and uploads. | ||||||||||||||||||||||||||||||||||||||||
| 92 | 11 | my $hash_expansion; | |||||||||||||||||||||||||||||||||||||||
| 93 | 11 | for (keys %args) { | |||||||||||||||||||||||||||||||||||||||
| 94 | 14 | my $value = $args{$_}; | |||||||||||||||||||||||||||||||||||||||
| 95 | 14 | ▶ | if (!defined $value) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | delete $args{$_}; | |||||||||||||||||||||||||||||||||||||||
| 97 | 0 | next; | |||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||
| 100 | 14 | my $ref = ref($value); | |||||||||||||||||||||||||||||||||||||||
| 101 | 14 | ▶ | if ($ref eq 'ARRAY' and ref $value->[0] eq 'Froody::Upload') { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition66%2/3
$ref eq "ARRAY" and ref $value->[0] eq "Froody::Upload"
MC/DC0%0/2
$ref eq "ARRAY" and ref $value->[0] eq "Froody::Upload"
$ref eq "ARRAY"
ref $value->[0] eq "Froody::Upload"
Truth table66%2/3
A and B
A= $ref eq "ARRAY"B= ref $value->[0] eq "Froody::Upload"
| |||||||||||||||||||||||||||||||||||||||||
| 102 | # upload | ||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $args{$_} = [ $value->[0]->filename, $value->[0]->client_filename ]; | |||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | die "too many uploads" if $value->[1]; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||
| 106 | } elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
| 107 | # just CSV | ||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | $args{$_} = join(",", @$value ); | |||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||
| 110 | } elsif ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | die "too many arguments of type 'remaining'; previous was '$hash_expansion' and current is '$_'" | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 112 | if $hash_expansion; | ||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $hash_expansion = $_; | |||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $blah = delete $args{$_}; | |||||||||||||||||||||||||||||||||||||||
| 115 | 0 | %args = ( %args, %$blah ); | |||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||
| 117 | } elsif ($ref) { | ||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | die "can't handle type of argument '$_' (is a ".ref($args{$_}).")"; | |||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||
| 121 | 11 | foreach (keys %args) { | |||||||||||||||||||||||||||||||||||||||
| 122 | 14 | ▶ | unless (ref $args{$_}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 123 | 14 | $args{$_} = encode_utf8($args{$_}); | |||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||
| 127 | # make the request | ||||||||||||||||||||||||||||||||||||||||
| 128 | 11 | my $request = POST( $self->endpoint, | |||||||||||||||||||||||||||||||||||||||
| 129 | Content_Type => 'form-data', | ||||||||||||||||||||||||||||||||||||||||
| 130 | Content => [ method => $method, %args ] ); | ||||||||||||||||||||||||||||||||||||||||
| 131 | 11 | my $response = $self->{ua}->request( $request ); | |||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||
| 133 | 11 | ▶ | Froody::Error->throw('froody.invoke.remote', 'Bad response from server: '. $response->status_line) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 134 | unless $response->is_success; | ||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||
| 136 | 9 | return $response->content; | |||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||
| 139 | =back | ||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||
| 141 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||
| 143 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||
| 145 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||||||||||||||
| 146 | on this project. | ||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||
| 148 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||
| 149 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||