line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Terse::Plugin::UA; |
2
|
1
|
|
|
1
|
|
69711
|
use 5.006; use strict; use warnings; |
|
1
|
|
|
1
|
|
4
|
|
|
1
|
|
|
1
|
|
6
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
105
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
4
|
1
|
|
|
1
|
|
11
|
use base 'Terse::Plugin'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
614
|
|
5
|
1
|
|
|
1
|
|
111009
|
use LWP::UserAgent; use URI; use Scalar::Util qw/reftype/; |
|
1
|
|
|
1
|
|
46428
|
|
|
1
|
|
|
1
|
|
41
|
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
766
|
|
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
|
|
|
|
|
|
my @args; |
56
|
0
|
0
|
|
|
|
|
if ($method =~ m/get/i) { |
57
|
0
|
|
|
|
|
|
$url->query_form(%{$self->req_params}); |
|
0
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
@args = ( |
59
|
|
|
|
|
|
|
$url, |
60
|
|
|
|
|
|
|
%args, |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
} else { |
63
|
0
|
|
|
|
|
|
@args = ( |
64
|
|
|
|
|
|
|
$url, |
65
|
|
|
|
|
|
|
%args, |
66
|
|
|
|
|
|
|
Content => $request_parameters |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
|
my $res = $self->ua->$method(@args); |
70
|
0
|
|
|
|
|
|
$res->{uri} = $url; |
71
|
0
|
|
|
|
|
|
$self->_parse_response($res, $view); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _parse_response { |
75
|
0
|
|
|
0
|
|
|
my ($self, $res, $view) = @_; |
76
|
0
|
0
|
|
|
|
|
unless ($res->is_success) { |
77
|
0
|
|
|
|
|
|
my $response = eval { $self->graft('response', $res->decoded_content) }; |
|
0
|
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
|
return $self->_return_error($@ ? undef : $response, $res, $res->code()); |
79
|
|
|
|
|
|
|
} |
80
|
0
|
0
|
0
|
|
|
|
if ($self->can('response_cb')) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return $self->response_cb($res); |
82
|
|
|
|
|
|
|
} elsif ($view && $view->can('parse')) { |
83
|
0
|
|
|
|
|
|
return $view->parse($self->context, $res); |
84
|
|
|
|
|
|
|
} elsif ($res->content_type eq 'application/json') { |
85
|
0
|
|
|
|
|
|
my $response = $self->graft('response', $res->decoded_content); |
86
|
0
|
0
|
|
|
|
|
return $self->_return_error('Invalid JSON content returned', $res) if !$response; |
87
|
0
|
|
|
|
|
|
return $response |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
return $self->_return_error('Invalid UA call', $res); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _return_error { |
93
|
0
|
|
|
0
|
|
|
my ($self, $error, $res, $status) = @_; |
94
|
0
|
|
0
|
|
|
|
$status ||= 500; |
95
|
0
|
0
|
|
|
|
|
if ($self->can('error_cb')) { |
96
|
0
|
|
|
|
|
|
return $self->error_cb($error, $res, $status); |
97
|
|
|
|
|
|
|
} |
98
|
0
|
|
0
|
|
|
|
$self->context->logError($error || 'PAYMENT API call failed', 500); |
99
|
0
|
|
|
|
|
|
return; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |