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