line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::OpenResty::Simple; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
18014
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
29
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp 'croak'; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
38
|
|
7
|
1
|
|
|
1
|
|
1054
|
use JSON::XS (); |
|
1
|
|
|
|
|
36124
|
|
|
1
|
|
|
|
|
34
|
|
8
|
1
|
|
|
1
|
|
16
|
use base 'WWW::OpenResty'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
931
|
|
9
|
1
|
|
|
1
|
|
6
|
use Params::Util qw( _HASH ); |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
769
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
12
|
|
|
|
|
|
|
our $json_xs = JSON::XS->new->utf8->allow_nonref; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub request { |
15
|
5
|
|
|
5
|
0
|
12
|
my $self = shift; |
16
|
5
|
|
|
|
|
8
|
my $data = $_[0]; |
17
|
5
|
|
|
|
|
14
|
my $meth = $_[1]; |
18
|
5
|
|
|
|
|
161
|
my $url = $_[2]; |
19
|
5
|
50
|
33
|
|
|
29
|
if ($data && ref $data) { |
20
|
0
|
|
|
|
|
0
|
$_[0] = $json_xs->encode($data); |
21
|
|
|
|
|
|
|
} |
22
|
5
|
|
50
|
|
|
44
|
my $retries = $self->{retries} || 0; |
23
|
5
|
|
|
|
|
12
|
my $ignore_dup_error = $self->{ignore_dup_error}; |
24
|
5
|
|
|
|
|
7
|
my $i = 0; |
25
|
5
|
|
|
|
|
10
|
while (1) { |
26
|
5
|
|
|
|
|
30
|
my $res = $self->SUPER::request(@_); |
27
|
5
|
50
|
|
|
|
26
|
if ($res->is_success) { |
28
|
0
|
|
|
|
|
0
|
my $json = $res->content; |
29
|
|
|
|
|
|
|
#$json =~ s/\n+$//gs; |
30
|
0
|
|
|
|
|
0
|
my $data = $json_xs->decode($json); |
31
|
0
|
0
|
0
|
|
|
0
|
if (_HASH($data) && defined $data->{success} && $data->{success} == 0) { |
|
|
|
0
|
|
|
|
|
32
|
0
|
0
|
0
|
|
|
0
|
if ($ignore_dup_error && |
33
|
|
|
|
|
|
|
$data->{error} =~ /duplicate key (?:value )?violates unique constraint/) { |
34
|
|
|
|
|
|
|
#print "Ignored |
35
|
0
|
|
|
|
|
0
|
return $data; |
36
|
|
|
|
|
|
|
} |
37
|
0
|
0
|
|
|
|
0
|
if ($i >= $retries) { |
38
|
0
|
|
|
|
|
0
|
croak "$meth $url: $json"; |
39
|
|
|
|
|
|
|
} else { |
40
|
0
|
|
|
|
|
0
|
warn "Retrying...\n"; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} else { |
43
|
0
|
|
|
|
|
0
|
return $data; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} else { |
46
|
5
|
|
|
|
|
65
|
my $status_line = $res->status_line; |
47
|
5
|
50
|
|
|
|
62
|
if ($i >= $retries) { |
48
|
5
|
|
|
|
|
1070
|
croak "$meth $url: $status_line"; |
49
|
|
|
|
|
|
|
} else { |
50
|
0
|
|
|
|
|
|
warn "Retrying...\n"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
|
} continue { $i++ } |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub has_model { |
57
|
0
|
|
|
0
|
1
|
|
my ($self, $model) = @_; |
58
|
0
|
|
|
|
|
|
my $res; |
59
|
0
|
|
|
|
|
|
eval { |
60
|
0
|
|
|
|
|
|
$res = $self->get("/=/model/$model"); |
61
|
|
|
|
|
|
|
}; |
62
|
0
|
0
|
|
|
|
|
if ($@) { |
63
|
0
|
0
|
|
|
|
|
if ($@ =~ /Model .*? not found/i) { |
64
|
0
|
|
|
|
|
|
return undef; |
65
|
|
|
|
|
|
|
} |
66
|
0
|
|
|
|
|
|
die $@; |
67
|
|
|
|
|
|
|
} |
68
|
0
|
|
0
|
|
|
|
return _HASH($res) && $res->{name} eq $model; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub has_view { |
72
|
0
|
|
|
0
|
1
|
|
my ($self, $view) = @_; |
73
|
0
|
|
|
|
|
|
my $res; |
74
|
0
|
|
|
|
|
|
eval { |
75
|
0
|
|
|
|
|
|
$res = $self->get("/=/view/$view"); |
76
|
|
|
|
|
|
|
}; |
77
|
0
|
0
|
|
|
|
|
if ($@) { |
78
|
0
|
0
|
|
|
|
|
if ($@ =~ /View .*? not found/i) { |
79
|
0
|
|
|
|
|
|
return undef; |
80
|
|
|
|
|
|
|
} |
81
|
0
|
|
|
|
|
|
die $@; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
|
0
|
|
|
|
return _HASH($res) && $res->{name} eq $view; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
__END__ |