Paws XIII (The Unlucky one)

I left off in my last post with all my bits and bods to get the proper structure in my generated perl class from the Json data. Now I have to get the 'status' out of the response and then into my class.

By now I know where to start to look to in the code to apply my changes. I started in Paws::Net::RestXMLResponse' class at its 'process' sub which looks at the response 'status' and takes either the error path (>=300) or the happy path (<300). We are interested in the happy path and that takes us to the 'response_to_object' sub.

First I can see that I have the 'status' very soon in the sub;


sub response_to_object {
    my ($self, $call_object, $response) = @_;
    my ($http_status, $content, $headers) = ($response->status, $response->content, $response->headers);;
…

The $response that is coming in is a ''Paws::Net::APIResponse' and I see that all thee of its attributes are passed to local scalars including the one I am interested in 'status'. So far it only is used if there is some sort of error when working with the returned data.


 if ($@){
          return Paws::Exception->new(
            message => $@,
            code => 'InvalidContent',
            request_id => '', #$request_id,
            http_status => $http_status,
          );
        }

First a little explanation on how the sub works may be in order. The sub tries to take all the data coming off the 'response' in its raw or unserialized data values and load them into a hash-ref called '$unserialized_struct'; For example all the header values are added in


    foreach my $key (keys %$headers){
        $unserialized_struct->{lc $key} = $headers->{$key};
    }

it then passes this hash-ref to another sub to be de-serialized and passed into a new instance of a generated class as a set of new params. I first have to get status in that hash-ref like this;


    $unserialized_struct->{ _request_id } = $request_id;
    $unserialized_struct->{ status } = $http_status;

and now I have to go into the 'new_from_result_struct' to set the value;

Looking about in that sub I see that I am iterating over each of the attributes found in the new class that I will be creating but looping over is meta-data.


foreach my $att ($class->meta->get_attribute_list) {
     next if (not my $meta = $class->meta->get_attribute($att));
     my $key = $meta->does('NameInRequest') ? $meta->request_name :
     $meta->does('ParamInHeader') ? lc($meta->header_name) : $att;

What I am going to do is check the to see if the current attribute 'does' that new trait I add into the code and I think this is the best spot for my code change;


-    if ($meta->does('Paws::API::Attribute::Trait::ParamInHeaders')) {
+    if ( $meta->does('ParamInResponse')){
+        $key = $meta->response_name;
+        $args{ $meta->name } = $result->{$key};
+    } elsif ($meta->does('Paws::API::Attribute::Trait::ParamInHeaders')) {

In the above snippet I check the $key from the 'response_name' then and then use that value to add it to the %args has which will be uesed by the sub on this line


$class->new(%args);

which creates a new instance of the passed in class that is then returned;

I then ran my tests and they passed

ok 10209 - Call S3->RestoreObject from t/10_responses/s3-restore-object.1.response ok 10210 - Got _request_id eq 9C4494F17CFA569D from result ok 10211 - Got Status eq 200 from result ok 10212 - Call S3->RestoreObject from t/10_responses/s3-restore-object.2.response ok 10213 - Got _request_id eq 9C4494F17CFA569D from result ok 10214 - Got Status eq 202 from result ok 10215 - Call S3->RestoreObject from t/10_responses/s3-restore-object.3.response ok 10216 - Got request_id eq AC65B3BF8AC3AA34 from result ok 10217 - Got message eq Object restore is already in progress from result ok 10218 - Got code eq RestoreAlreadyInProgress from result ok 10219 - Got http_status eq 409 from result

Now lets see what happens when I put this back into Paws??

codi-dog-shaming-102215-637x796.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations