File Coverage

blib/lib/InfluxDB/Client/Response.pm
Criterion Covered Total %
statement 23 65 35.3
branch 0 28 0.0
condition 0 14 0.0
subroutine 8 15 53.3
pod 7 7 100.0
total 38 129 29.4


line stmt bran cond sub pod time code
1             package InfluxDB::Client::Response;
2             # ABSTRACT: response class of the InfluxDB::Client
3              
4 3     3   18 use strict;
  3         17  
  3         89  
5 3     3   16 use warnings;
  3         6  
  3         76  
6 3     3   31 use v5.10;
  3         10  
7              
8 3     3   17 use Carp;
  3         7  
  3         152  
9 3     3   1187 use JSON;
  3         19895  
  3         12  
10 3     3   293 use HTTP::Status ();
  3         6  
  3         44  
11              
12 3     3   721 use InfluxDB::Client::Response::Status;
  3         6  
  3         68  
13 3     3   716 use InfluxDB::Client::Response::Meta;
  3         7  
  3         1489  
14              
15             our $VERSION = '0.1';
16              
17             sub new {
18 0     0 1   my ( $class, %args ) = @_;
19            
20             my $self = bless {
21 0   0       _status => $args{status} || InfluxDB::Client::Response::Status::OK,
22             _error => undef,
23             _content => {},
24             # we create an empty meta object. this could be filled later
25             # we don't have enough usecases where to fill this in the constructor to handle it here
26             _meta => InfluxDB::Client::Response::Meta->new,
27             } => $class;
28            
29 0 0         if ( $args{http_response} ) {
    0          
30 0           $self->parse_http_response($args{http_response});
31             } elsif ( $args{content} ) {
32 0           $self->content($args{content});
33             }
34            
35 0           return $self;
36             }
37              
38              
39             sub status {
40 0     0 1   my ( $self, $status ) = @_;
41 0 0         if ( $status ) {
42             # TODO: type checking
43 0           $self->{_status} = $status;
44             }
45 0           return $self->{_status};
46             }
47              
48             sub http_code {
49 0     0 1   return $_[0]->{_http_code};
50             }
51              
52             sub content {
53 0     0 1   my ( $self, $content ) = @_;
54 0 0         if ( $content ) {
55 0 0         if ( ref $content ) {
56             # if we got an reference, we just store it
57 0           $self->{_content} = $content;
58             } else {
59             # otherwise we parse it as a JSON string
60 0           my $json = JSON->new();
61 0           eval { $self->{_content} = $json->decode($content); };
  0            
62              
63             # if JSON parsing failed, we set the status to INTFAIL
64 0 0         if ($@) {
65 0           $self->{_error} = $@;
66 0           $self->{_status} = InfluxDB::Client::Response::Status::INTFAIL;
67             } else {
68             # we check if we have an error in our result
69 0 0 0       if(exists $self->{_content}{error} && defined $self->{_content}{error}) {
70 0           $self->{_error} = $self->{_content}{error};
71             # the status code should already be set to error, but we make sure it really is
72             $self->{_status} = InfluxDB::Client::Response::Status::SRVFAIL
73 0 0         if ( $self->{_status} == InfluxDB::Client::Response::Status::OK );
74             }
75            
76             # we strip the "results" from the parsed data
77 0 0         $self->{_content} = $self->{_content}{results} if exists $self->{_content}{results};
78             }
79             }
80             }
81 0           return $self->{_content};
82             }
83              
84             sub error {
85 0     0 1   my ( $self, $error ) = @_;
86 0 0         $self->{_error} = $error if $error;
87 0           return $self->{_error};
88             }
89              
90             sub meta {
91 0     0 1   return $_[0]->{_meta};
92             }
93              
94             sub parse_http_response {
95 0     0 1   my ( $self, $response ) = @_;
96            
97 0 0 0       croak 'invalid response given'
      0        
98             unless( $response && ref $response && ref $response eq 'HTTP::Response' );
99             # set the status according to the response code of the http_response
100 0 0 0       if ( $response->code == HTTP::Status::HTTP_UNAUTHORIZED || $response->code == HTTP::Status::HTTP_FORBIDDEN ) {
    0          
101 0           $self->{_status} = InfluxDB::Client::Response::Status::AUTH;
102             } elsif ( $response->code >= 400 ) {
103 0           $self->{_status} = InfluxDB::Client::Response::Status::SRVFAIL;
104             } else {
105 0           $self->{_status} = InfluxDB::Client::Response::Status::OK;
106             }
107            
108             # parse our content
109             # we leave it up to the calling function to normalize the response data if needed
110 0 0         $self->content($response->content()) if $response->content();
111            
112             # set the http_code to the value from the response
113 0           $self->{_http_code} = $response->code;
114            
115             # we read some meta values and store them in the Meta Object
116 0           $self->meta->influxdb_version($response->header('X-Influxdb-Version'));
117 0           $self->meta->request_id($response->header('Request-Id'));
118 0           $self->meta->server($response->header('Client-Peer'));
119            
120             # return the status, so the caller could check if the parsing worked
121 0           return $self->status;
122             }
123              
124              
125             1;
126              
127             __END__