| 1 | package Zenoss::Response; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use JSON qw{}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | with 'Zenoss::Error'; | |||||||||
| 7 | |||||||||||
| 8 | #************************************************************************** | ||||||||||
| 9 | # Public Attributes | ||||||||||
| 10 | #************************************************************************** | ||||||||||
| 11 | # HTTP::Response from Zenoss::Router | ||||||||||
| 12 | 1 | has handler => ( | |||||||||
| 13 | isa => 'HTTP::Response', | ||||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | handles => { | ||||||||||
| 16 | # Proxy methods to HTTP::Response | ||||||||||
| 17 | json => 'decoded_content', | ||||||||||
| 18 | raw_response => 'as_string', | ||||||||||
| 19 | http_code => 'code', | ||||||||||
| 20 | http_code_description => 'message', | ||||||||||
| 21 | http_status => 'status_line', | ||||||||||
| 22 | is_success => 'is_success', | ||||||||||
| 23 | is_error => 'is_error', | ||||||||||
| 24 | error_as_HTML => 'error_as_HTML', | ||||||||||
| 25 | header => 'header', | ||||||||||
| 26 | request_time => 'current_age', | ||||||||||
| 27 | }, | ||||||||||
| 28 | required => 1, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | # Perl reference format of the JSON return data | ||||||||||
| 32 | 1 | has decoded => ( | |||||||||
| 33 | is => 'ro', | ||||||||||
| 34 | isa => 'Ref', | ||||||||||
| 35 | builder => '_build_decoded', | ||||||||||
| 36 | lazy => 1, | ||||||||||
| 37 | init_arg => undef, | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | # Transaction ID that was received from the Zenoss server | ||||||||||
| 41 | 1 | has received_tid => ( | |||||||||
| 42 | is => 'ro', | ||||||||||
| 43 | isa => 'Num', | ||||||||||
| 44 | builder => '_build_received_tid', | ||||||||||
| 45 | writer => '_set_received_tid', | ||||||||||
| 46 | lazy => 1, | ||||||||||
| 47 | init_arg => undef, | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | # Transaction ID that was sent to the Zenoss server | ||||||||||
| 51 | # This is passed from Zenoss::Router | ||||||||||
| 52 | 1 | has sent_tid => ( | |||||||||
| 53 | is => 'ro', | ||||||||||
| 54 | isa => 'Num', | ||||||||||
| 55 | required => 1, | ||||||||||
| 56 | ); | ||||||||||
| 57 | |||||||||||
| 58 | #************************************************************************** | ||||||||||
| 59 | # Private Attributes | ||||||||||
| 60 | #************************************************************************** | ||||||||||
| 61 | # Method that send the API call off | ||||||||||
| 62 | 1 | has '_caller' => ( | |||||||||
| 63 | is => 'ro', | ||||||||||
| 64 | isa => 'Str', | ||||||||||
| 65 | required => 1, | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | #************************************************************************** | ||||||||||
| 69 | # Private Methods | ||||||||||
| 70 | #************************************************************************** | ||||||||||
| 71 | #====================================================================== | ||||||||||
| 72 | # _build_decoded | ||||||||||
| 73 | #====================================================================== | ||||||||||
| 74 | 0 | ▶ | sub _build_decoded { | ||||||||
|
Subroutine
_build_decoded: not called CC=3 SCAR=24.8
| |||||||||||
| 75 | 0 | my $self = shift; | |||||||||
| 76 | |||||||||||
| 77 | 0 | my $json_decoder = JSON->new->allow_nonref; | |||||||||
| 78 | 0 | my $json_decoded = $json_decoder->decode($self->json); | |||||||||
| 79 | |||||||||||
| 80 | # Set the received_tid if we have it | ||||||||||
| 81 | 0 | ▶ | if (exists($json_decoded->{'tid'})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | $self->_set_received_tid($json_decoded->{'tid'}); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | # Return result set | ||||||||||
| 86 | 0 | ▶ | if (exists($json_decoded->{'result'})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | # result structure should be there, so return this | ||||||||||
| 88 | 0 | return $json_decoded->{'result'}; | |||||||||
| 89 | } else { | ||||||||||
| 90 | # if not return what we have | ||||||||||
| 91 | 0 | return $json_decoded; | |||||||||
| 92 | } | ||||||||||
| 93 | } # END _build_decoded | ||||||||||
| 94 | |||||||||||
| 95 | #====================================================================== | ||||||||||
| 96 | # _build_received_tid | ||||||||||
| 97 | #====================================================================== | ||||||||||
| 98 | 0 | ▶ | sub _build_received_tid { | ||||||||
|
Subroutine
_build_received_tid: not called CC=1 SCAR=6.9
| |||||||||||
| 99 | 0 | my $self = shift; | |||||||||
| 100 | |||||||||||
| 101 | # just call _build_decoded - it does the same thing | ||||||||||
| 102 | # its easier to just convert the JSON to a hash and pull it out | ||||||||||
| 103 | # of the data structure | ||||||||||
| 104 | 0 | $self->_build_decoded; | |||||||||
| 105 | } # END _build_received_tid | ||||||||||
| 106 | |||||||||||
| 107 | #====================================================================== | ||||||||||
| 108 | # _validate_api_method_exists | ||||||||||
| 109 | # | ||||||||||
| 110 | # This method will determine if the API allowed the method to be | ||||||||||
| 111 | # executed on the Zenoss server | ||||||||||
| 112 | #====================================================================== | ||||||||||
| 113 | 0 | ▶ | sub _validate_api_method_exists { | ||||||||
|
Subroutine
_validate_api_method_exists: not called CC=3 SCAR=24.8
| |||||||||||
| 114 | 0 | my $self = shift; | |||||||||
| 115 | |||||||||||
| 116 | # Check to see if we have a 500 HTTP ERROR | ||||||||||
| 117 | 0 | ▶ | if ($self->http_code() == 500) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | # Check to see if Zenoss reported the API call unavailable | ||||||||||
| 119 | 0 | ▶ | if ($self->raw_response() =~ m/is not the name of a method on/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | my $caller = $self->_caller(); | |||||||||
| 121 | 0 | $self->_croak("[$caller] is not an available API call with your version of Zenoss. Upgrade your Zenoss to a later version!"); | |||||||||
| 122 | } | ||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | # All good, return the response object | ||||||||||
| 126 | 0 | return $self; | |||||||||
| 127 | } # END _validate_api_method_exists | ||||||||||
| 128 | |||||||||||
| 129 | #************************************************************************** | ||||||||||
| 130 | # Package end | ||||||||||
| 131 | #************************************************************************** | ||||||||||
| 132 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 133 | 1 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 134 | |||||||||||
| 135 | 1 | 1; | |||||||||
| 136 | |||||||||||
| 137 | __END__ | ||||||||||