line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Druid; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
53821
|
use Moo; |
|
1
|
|
|
|
|
9332
|
|
|
1
|
|
|
|
|
3
|
|
5
|
1
|
|
|
1
|
|
1572
|
use JSON::MaybeXS qw(encode_json decode_json); |
|
1
|
|
|
|
|
6125
|
|
|
1
|
|
|
|
|
45
|
|
6
|
1
|
|
|
1
|
|
550
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
36581
|
|
|
1
|
|
|
|
|
28
|
|
7
|
1
|
|
|
1
|
|
6
|
use HTTP::Request; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
8
|
1
|
|
|
1
|
|
322
|
use Druid::Util qw(iso8601_yyyy_mm_dd_hh_mm_ss); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
346
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.004'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has 'api_url' => ( |
13
|
|
|
|
|
|
|
'is' => 'ro', |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has 'ua' => ( |
17
|
|
|
|
|
|
|
'is' => 'ro', |
18
|
|
|
|
|
|
|
'default' => sub { |
19
|
|
|
|
|
|
|
my $self = shift; |
20
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
21
|
|
|
|
|
|
|
$ua->ssl_opts( 'verify_hostname' => 0 ); |
22
|
|
|
|
|
|
|
return $ua; |
23
|
|
|
|
|
|
|
}, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has 'req' => ( |
27
|
|
|
|
|
|
|
'is' => 'ro', |
28
|
|
|
|
|
|
|
'lazy' => 1, |
29
|
|
|
|
|
|
|
'default' => sub { |
30
|
|
|
|
|
|
|
my $self = shift; |
31
|
|
|
|
|
|
|
my $req = HTTP::Request->new( 'POST' => $self->api_url ); |
32
|
|
|
|
|
|
|
$req->header( 'Content-Type' => 'application/json' ); |
33
|
|
|
|
|
|
|
return $req; |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub send { |
38
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
39
|
0
|
|
|
|
|
|
my $query = shift; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
$self->{error} = undef; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $response; |
44
|
0
|
|
|
|
|
|
my $request_hash = $query->gen_query; |
45
|
0
|
|
|
|
|
|
$self->req->content( encode_json( $request_hash ) ); |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $res = $self->ua->request( $self->req ); |
48
|
0
|
0
|
|
|
|
|
if ($res->is_success) { |
49
|
|
|
|
|
|
|
eval { |
50
|
0
|
0
|
|
|
|
|
$response = decode_json($res->content) if $res->content ne ""; |
51
|
|
|
|
|
|
|
$_->{'timestamp'} = iso8601_yyyy_mm_dd_hh_mm_ss( $_->{'timestamp'} ) |
52
|
0
|
|
|
|
|
|
for @{$response}; |
|
0
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
1; |
54
|
0
|
0
|
|
|
|
|
} or do { |
55
|
0
|
|
|
|
|
|
$self->handle_error("500", "Parsing of the reponse failed"); |
56
|
0
|
|
|
|
|
|
warn "error: $@"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
}else{ |
59
|
0
|
|
|
|
|
|
$self->handle_error($res->code, $res->message, $res->content); |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
return $response; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub handle_error { |
65
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
66
|
0
|
|
|
|
|
|
my ($code, $message, $content) = @_; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
$self->{error} = { |
69
|
0
|
|
|
|
|
|
"code" => $code, |
70
|
|
|
|
|
|
|
"message" => $message, |
71
|
|
|
|
|
|
|
"content" => $content |
72
|
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
__END__ |