Answer.pm (4174B)
1 package OvhApi::Answer; 2 3 use strict; 4 use warnings; 5 6 our $VERSION = 0.2; 7 8 9 use overload ( 10 bool => \&isSuccess, 11 '!' => \&isFailure, 12 fallback => 0, 13 ); 14 15 use Scalar::Util 'blessed'; 16 use Carp qw{ carp croak }; 17 use JSON (); 18 19 20 21 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 22 # Class variables 23 24 my $Json = JSON->new->allow_nonref; 25 26 # End - Class variables 27 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 28 29 30 31 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 32 # Class methods 33 34 sub new 35 { 36 my ($class, %params) = @_; 37 38 unless ($params{'response'}) 39 { 40 croak 'Missing parameter: response'; 41 } 42 43 unless (blessed $params{'response'} and $params{'response'}->isa('HTTP::Response')) 44 { 45 croak 'Invalid parameter: reponse'; 46 } 47 48 bless { response => $params{'response'} }, $class; 49 } 50 51 # End - Class methods 52 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 53 54 55 56 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 57 # Instance methods 58 59 sub isSuccess 60 { 61 my ($self) = @_; 62 63 return $self->{'response'}->is_success; 64 } 65 66 sub isFailure 67 { 68 my ($self) = @_; 69 70 return not $self->isSuccess; 71 } 72 73 74 sub content 75 { 76 my ($self) = @_; 77 78 if ($self->isFailure) 79 { 80 carp 'Fetching content from a failed OvhApi::Response Object'; 81 return; 82 } 83 84 return $self->_generateContent; 85 } 86 87 sub error 88 { 89 my ($self) = @_; 90 91 return $self ? '' : $self->_generateContent->{'message'}; 92 } 93 94 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 95 # private part 96 97 sub _generateContent 98 { 99 my ($self) = @_; 100 101 my $content; 102 103 if ($self->{'response'}->header('Client-Warning') and $self->{'response'}->header('Client-Warning') eq 'Internal response') 104 { 105 return { message => 'Internal LWP::UserAgent error : ' . $self->{'response'}->content }; 106 } 107 108 eval { $content = $Json->decode($self->{'response'}->content); 1; } or do { 109 carp 'Failed to parse JSON content from the answer: ', $self->{'response'}->content; 110 return; 111 }; 112 113 return $content; 114 } 115 116 # End - Instance methods 117 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 118 119 120 return 42; 121 122 __END__ 123 124 =head1 NAME 125 126 OvhApi::Answer - Response to a request run with C<OvhApi>. 127 128 =head1 SYNOPSIS 129 130 my $Answer = $Api->get(path => '/me'); 131 132 if ($Answer) 133 { 134 # Success: can fetch content and process 135 my $content = $Answer->content; 136 } 137 else 138 { 139 # Request failed: stop here and retrieve the error 140 my $error = $Answer->error; 141 } 142 143 =head1 DESCRIPTION 144 145 This module represents a response to a query run with C<OvhApi>. It is build upon a C<HTTP::Request> object. 146 147 =head1 CLASS METHODS 148 149 =head2 Constructor 150 151 There is only one constructor: C<new>. 152 153 Its parameters are: 154 155 Parameter Mandatory Default Usage 156 ------------ ------------ ---------- -------- 157 response Yes - An HTTP::Response object return by LWP::UserAgent 158 159 =head1 INSTANCE METHODS 160 161 =head2 content 162 163 Returns the content of the answer. This method will C<carp> if the answer is an error. 164 165 It takes no parameter. 166 167 =head2 error 168 169 Returns the error message of the answer, or an empty string if the answer is a success. 170 171 It takes no parameter. 172 173 =head2 isSuccess 174 175 Forwards a call to C<HTTP::Response::is_error> in the inner C<HTTP::Response> of the answer. Returns true is the request was a success, false otherwise. 176 177 It takes no parameter. 178 179 This method is used for the C<bool> L<overload|overload>. 180 181 =head2 isFailure 182 183 Helper method which returns the boolean negation of L<isSuccess|/isSuccess>. 184 185 It takes no parameter. 186 187 =head1 SEE ALSO 188 189 The guts of module are using: C<JSON>. 190 191 =head1 COPYRIGHT 192 193 Copyright (c) 2013, OVH SAS. 194 All rights reserved. 195 196 This library is distributed under the terms of C<LICENSE>. 197 198 =cut 199 200