line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Terse::Plugin::UA; |
2
|
1
|
|
|
1
|
|
69292
|
use 5.006; use strict; use warnings; |
|
1
|
|
|
1
|
|
4
|
|
|
1
|
|
|
1
|
|
6
|
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
35
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
4
|
1
|
|
|
1
|
|
7
|
use base 'Terse::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
648
|
|
5
|
1
|
|
|
1
|
|
118982
|
use LWP::UserAgent; use URI; use Scalar::Util qw/reftype/; |
|
1
|
|
|
1
|
|
46370
|
|
|
1
|
|
|
1
|
|
32
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
23
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
764
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub connect { |
8
|
0
|
|
|
0
|
0
|
|
my ($self, $t) = @_; |
9
|
0
|
|
|
|
|
|
my $connect = $self->SUPER::new(); |
10
|
0
|
|
|
|
|
|
$connect->ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 0}); |
11
|
0
|
|
|
|
|
|
$connect->ua->cookie_jar({}); |
12
|
0
|
|
|
|
|
|
$connect->context = $t; |
13
|
0
|
|
|
|
|
|
return $connect; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub post { |
17
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
18
|
0
|
|
|
|
|
|
$self->_request( |
19
|
|
|
|
|
|
|
method => 'post', |
20
|
|
|
|
|
|
|
'Content-Type' => 'application/json', |
21
|
|
|
|
|
|
|
Accept => 'application/json', |
22
|
|
|
|
|
|
|
@_ |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub get { |
27
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
28
|
0
|
|
|
|
|
|
$self->_request( |
29
|
|
|
|
|
|
|
method => 'get', |
30
|
|
|
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8', |
31
|
|
|
|
|
|
|
Accept => 'application/json', |
32
|
|
|
|
|
|
|
@_ |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _request { |
37
|
0
|
|
|
0
|
|
|
my ($self, %args) = @_; |
38
|
0
|
0
|
|
|
|
|
$self->request_cb(\%args) if $self->can('request_cb'); |
39
|
0
|
|
0
|
|
|
|
my $request_parameters = delete $args{params} // {}; |
40
|
0
|
0
|
0
|
|
|
|
if ($self->context->{session} && $self->context->session->{user}) { |
41
|
0
|
|
|
|
|
|
$request_parameters->{userName} = $self->context->session->user->userName; |
42
|
0
|
|
|
|
|
|
$request_parameters->{hashedUserName} = $self->context->session->user->hashedUserName; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
|
my $url = URI->new(delete $args{path}); |
45
|
0
|
|
|
|
|
|
my $method = delete $args{method}; |
46
|
0
|
|
0
|
|
|
|
my $view = $args{view} && $self->context->view(delete $args{view}); |
47
|
0
|
0
|
|
|
|
|
if ($self->can('params_cb')) { |
|
|
0
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
$self->req_params = $request_parameters = $self->params_cb($request_parameters,\%args, $view); |
49
|
|
|
|
|
|
|
} elsif ($view) { |
50
|
0
|
|
|
|
|
|
$self->req_params = $request_parameters = $view->render($self->context, $request_parameters); |
51
|
|
|
|
|
|
|
} else { |
52
|
0
|
|
|
|
|
|
$self->req_params = $request_parameters; |
53
|
0
|
|
|
|
|
|
$request_parameters = $self->req_params->serialize(); |
54
|
|
|
|
|
|
|
} |
55
|
0
|
0
|
0
|
|
|
|
my @args = ( $method =~ m/get/ ? ($url->query_form(%{$self->req_params}) && ( |
56
|
|
|
|
|
|
|
$url, |
57
|
|
|
|
|
|
|
%args, |
58
|
|
|
|
|
|
|
)) : ( |
59
|
|
|
|
|
|
|
$url, |
60
|
|
|
|
|
|
|
%args, |
61
|
|
|
|
|
|
|
Content => $request_parameters |
62
|
|
|
|
|
|
|
)); |
63
|
0
|
|
|
|
|
|
my $res = $self->ua->$method(@args); |
64
|
0
|
|
|
|
|
|
$res->{uri} = $url; |
65
|
0
|
|
|
|
|
|
$self->_parse_response($res, $view); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _parse_response { |
69
|
0
|
|
|
0
|
|
|
my ($self, $res, $view) = @_; |
70
|
0
|
0
|
|
|
|
|
unless ($res->is_success) { |
71
|
0
|
|
|
|
|
|
my $response = eval { $self->graft('response', $res->decoded_content) }; |
|
0
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
return $self->_return_error($@ ? undef : $response, $res, $res->code()); |
73
|
|
|
|
|
|
|
} |
74
|
0
|
0
|
0
|
|
|
|
if ($self->can('response_cb')) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
return $self->response_cb($res); |
76
|
|
|
|
|
|
|
} elsif ($view && $view->can('parse')) { |
77
|
0
|
|
|
|
|
|
return $view->parse($self->context, $res); |
78
|
|
|
|
|
|
|
} elsif ($res->content_type eq 'application/json') { |
79
|
0
|
|
|
|
|
|
my $response = $self->graft('response', $res->decoded_content); |
80
|
0
|
0
|
|
|
|
|
return $self->_return_error('Invalid JSON content returned', $res) if !$response; |
81
|
0
|
|
|
|
|
|
return $response |
82
|
|
|
|
|
|
|
} |
83
|
0
|
|
|
|
|
|
return $self->_return_error('Invalid UA call', $res); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _return_error { |
87
|
0
|
|
|
0
|
|
|
my ($self, $error, $res, $status) = @_; |
88
|
0
|
|
0
|
|
|
|
$status ||= 500; |
89
|
0
|
0
|
|
|
|
|
if ($self->can('error_cb')) { |
90
|
0
|
|
|
|
|
|
return $self->error_cb($error, $res, $status); |
91
|
|
|
|
|
|
|
} |
92
|
0
|
|
0
|
|
|
|
$self->context->logError($error || 'PAYMENT API call failed', 500); |
93
|
0
|
|
|
|
|
|
return; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
__END__ |