| 1 | package WebService::MyGengo::Response; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | extends 'WebService::MyGengo::Base'; | ||
| 7 | |||
| 8 | use HTTP::Response; | ||
| 9 | use JSON; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | WebService::MyGengo::Response - An response from the myGengo API | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Wraps a L<HTTP::Response> received from the API with extra functionality. | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | # Responses usually created for you by WebService::MyGengo::Client | ||
| 22 | my $http_res = $user_agent->request( $some_request ); | ||
| 23 | my $res = WebService::MyGengo::Response->new( $http_res ); | ||
| 24 | |||
| 25 | # Some deserialized data structure from the API | ||
| 26 | my $struct = $res->response_struct; # ref($struct) eq 'HASH' | ||
| 27 | |||
| 28 | =head1 ATTRIBUTES | ||
| 29 | |||
| 30 | =head2 response_struct | ||
| 31 | |||
| 32 | Returns the deserialized response structure for successful API calls as a | ||
| 33 | hashref. | ||
| 34 | |||
| 35 | If the API response is not a hash it will be wrapped into a hash as | ||
| 36 | `{ elements => \@elements }` for consistency. | ||
| 37 | |||
| 38 | If the response was a failure, returns undef. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | has response_struct => ( | ||
| 42 | is => 'ro' | ||
| 43 | , isa => 'Maybe[HashRef]' | ||
| 44 | , lazy => 1 | ||
| 45 | , builder => '_build_response_struct' | ||
| 46 | ); | ||
| 47 | sub _build_response_struct { | ||
| 48 | my ( $self ) = ( shift ); | ||
| 49 | |||
| 50 | $self->is_error and return undef; | ||
| 51 | |||
| 52 | my $struct = $self->_deserialized->{response}; | ||
| 53 | |||
| 54 | ref($struct) ne 'HASH' and return { elements => $struct }; | ||
| 55 | |||
| 56 | return $struct; | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 error_code|code | ||
| 60 | |||
| 61 | The API-specific error code from the Response. | ||
| 62 | |||
| 63 | If the API request was successful, returns undef. | ||
| 64 | |||
| 65 | If the API returned an error, returns the API error code. | ||
| 66 | |||
| 67 | If the L<HTTP::Response> is_error, or no API payload is available, returns 0. | ||
| 68 | |||
| 69 | The raw HTTP response is available via `$response->_raw_response`. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | #todo Relationship between this object and L<HTTP::Response> needs a rethink. | ||
| 73 | sub code { shift->error_code(); } | ||
| 74 | has error_code => ( | ||
| 75 | is => 'ro' | ||
| 76 | , isa => 'Maybe[WebService::MyGengo::ErrorCode]' | ||
| 77 | , lazy => 1 | ||
| 78 | , builder => '_build_error_code' | ||
| 79 | ); | ||
| 80 | sub _build_error_code { | ||
| 81 | my ( $self ) = ( shift ); | ||
| 82 | |||
| 83 | my $raw_res = $self->_raw_response; | ||
| 84 | my $struct; | ||
| 85 | |||
| 86 | ( $raw_res->is_error || !( $struct = $self->_deserialized ) ) | ||
| 87 | and return 0; | ||
| 88 | |||
| 89 | $struct->{opstat} eq 'error' | ||
| 90 | and return $struct->{err}->{code}; | ||
| 91 | |||
| 92 | return undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 message | ||
| 96 | |||
| 97 | The API-specific error message from the Response. | ||
| 98 | |||
| 99 | If the API request was successful, returns "OK". | ||
| 100 | |||
| 101 | If the API returned an error response, returns the API error message. | ||
| 102 | |||
| 103 | If the L<HTTP::Response> is_error, or no API payload is available, returns | ||
| 104 | the status_line from the raw response. | ||
| 105 | |||
| 106 | The raw HTTP response is available via `$response->_raw_response`. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | sub message { shift->error_message(); } | ||
| 110 | has error_message => ( | ||
| 111 | is => 'ro' | ||
| 112 | , isa => 'Maybe[Str]' | ||
| 113 | , lazy => 1 | ||
| 114 | , builder => '_build_error_message' | ||
| 115 | ); | ||
| 116 | sub _build_error_message { | ||
| 117 | my ( $self ) = ( shift ); | ||
| 118 | |||
| 119 | my $raw_res = $self->_raw_response; | ||
| 120 | my $struct; | ||
| 121 | |||
| 122 | ( $raw_res->is_error || !( $struct = $self->_deserialized ) ) | ||
| 123 | and return $raw_res->status_line; | ||
| 124 | |||
| 125 | $struct->{opstat} eq 'error' | ||
| 126 | and return $struct->{err}->{msg}; | ||
| 127 | |||
| 128 | return "OK"; | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 _raw_response | ||
| 132 | |||
| 133 | The original HTTP::Response object. | ||
| 134 | |||
| 135 | =cut | ||
| 136 | has _raw_response => ( | ||
| 137 | is => 'ro' | ||
| 138 | , isa => 'HTTP::Response' | ||
| 139 | , required => 1 | ||
| 140 | # , handles => [ qw/code message status_line | ||
| 141 | # header content decoded_content request previous | ||
| 142 | # base filename as_string is_info | ||
| 143 | # is_redirect error_as_HTML redirects current_age | ||
| 144 | # freshness_lifetime is_fresh fresh_until | ||
| 145 | # / ] | ||
| 146 | ); | ||
| 147 | |||
| 148 | #=head2 _serializer | ||
| 149 | # | ||
| 150 | #A JSON object. | ||
| 151 | # | ||
| 152 | #B<Note:> The API also supports XML. We do not. :) | ||
| 153 | # | ||
| 154 | #=cut | ||
| 155 | has _serializer => ( | ||
| 156 | is => 'ro' | ||
| 157 | , isa => 'JSON' | ||
| 158 | , init_arg => undef | ||
| 159 | , lazy => 1 | ||
| 160 | , builder => '_build__serializer' | ||
| 161 | ); | ||
| 162 | sub _build__serializer { | ||
| 163 | return new JSON; | ||
| 164 | } | ||
| 165 | |||
| 166 | #=head2 _deserialized | ||
| 167 | # | ||
| 168 | #The deserialized response body. | ||
| 169 | # | ||
| 170 | #Returns undef if the body could not be deserialized. | ||
| 171 | # | ||
| 172 | #See L<response_struct> | ||
| 173 | # | ||
| 174 | #=cut | ||
| 175 | has _deserialized => ( | ||
| 176 | is => 'ro' | ||
| 177 | , isa => 'Maybe[HashRef]' | ||
| 178 | , init_arg => undef | ||
| 179 | , lazy => 1 | ||
| 180 | , builder => '_build__deserialized' | ||
| 181 | ); | ||
| 182 | sub _build__deserialized { | ||
| 183 | my ( $self ) = ( shift ); | ||
| 184 | |||
| 185 | my $raw = $self->_raw_response; | ||
| 186 | my $struct = eval { | ||
| 187 | return $self->_serializer->allow_nonref->utf8->relaxed | ||
| 188 | ->decode( $raw->content ); | ||
| 189 | }; | ||
| 190 | |||
| 191 | # This will never be called outside of testing | ||
| 192 | $@ and Carp::cluck("Could not deserialize response: " . $raw->status_line ); | ||
| 193 | |||
| 194 | return $struct; | ||
| 195 | } | ||
| 196 | |||
| 197 | =head1 METHODS | ||
| 198 | =cut | ||
| 199 | |||
| 200 | #=head2 BUILDARGS | ||
| 201 | # | ||
| 202 | #Accept arguments as a list. | ||
| 203 | # | ||
| 204 | #=cut | ||
| 205 | around BUILDARGS => sub { | ||
| 206 | my ( $orig, $class, $args ) = ( shift, shift, @_ ); | ||
| 207 | |||
| 208 | return { _raw_response => shift }; | ||
| 209 | }; | ||
| 210 | |||
| 211 | =head2 is_success | ||
| 212 | |||
| 213 | If the original response is_success, then also check for errors in the | ||
| 214 | response payload. | ||
| 215 | |||
| 216 | Otherwise, return false. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | sub is_success { | ||
| 220 | my ( $self ) = ( shift ); | ||
| 221 | |||
| 222 | $self->_raw_response->is_error and return 0; | ||
| 223 | |||
| 224 | return !$self->error_code; | ||
| 225 | } | ||
| 226 | |||
| 227 | =head2 is_error | ||
| 228 | |||
| 229 | Returns the opposite of L<is_success> | ||
| 230 | |||
| 231 | =cut | ||
| 232 | sub is_error { return !shift->is_success; } | ||
| 233 | |||
| 234 | |||
| 235 | __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | ||
| 236 | 1; | ||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Nathaniel Heinrichs | ||
| 241 | |||
| 242 | =head1 LICENSE | ||
| 243 | |||
| 244 | Copyright (c) 2011, Nathaniel Heinrichs <nheinric-at-cpan.org>. | ||
| 245 | All rights reserved. | ||
| 246 | |||
| 247 | This library is free software. You can redistribute it and/or modify | ||
| 248 | it under the same terms as Perl itself. | ||
| 249 | |||
| 250 | =cut |