line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Moip::V2; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
329246
|
use IO::Socket::SSL; |
|
2
|
|
|
|
|
123953
|
|
|
2
|
|
|
|
|
15
|
|
4
|
2
|
|
|
2
|
|
1541
|
use MIME::Base64; |
|
2
|
|
|
|
|
1029
|
|
|
2
|
|
|
|
|
98
|
|
5
|
2
|
|
|
2
|
|
11
|
use Furl; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
38
|
|
6
|
2
|
|
|
2
|
|
1051
|
use JSON (); |
|
2
|
|
|
|
|
16346
|
|
|
2
|
|
|
|
|
44
|
|
7
|
2
|
|
|
2
|
|
957
|
use Moo; |
|
2
|
|
|
|
|
16949
|
|
|
2
|
|
|
|
|
10
|
|
8
|
2
|
|
|
2
|
|
2949
|
use URI; |
|
2
|
|
|
|
|
6491
|
|
|
2
|
|
|
|
|
53
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
750
|
use Net::Moip::V2::Endpoint; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1169
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $JSON = JSON->new->utf8; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has 'ua', is => 'ro', default => sub { |
17
|
|
|
|
|
|
|
Furl->new( |
18
|
|
|
|
|
|
|
agent => "Net-Moip-V2/$VERSION", |
19
|
|
|
|
|
|
|
timeout => 15, |
20
|
|
|
|
|
|
|
max_redirects => 3, |
21
|
|
|
|
|
|
|
# "SSL Wants a read first" I think is suggesting you |
22
|
|
|
|
|
|
|
# haven't read OpenSSL a bedtime story in too long and perhaps |
23
|
|
|
|
|
|
|
# it's feeling neglected and lonely? |
24
|
|
|
|
|
|
|
# see also: https://metacpan.org/pod/IO::Socket::SSL#SNI-Support |
25
|
|
|
|
|
|
|
# https://metacpan.org/pod/Furl#FAQ |
26
|
|
|
|
|
|
|
# https://rt.cpan.org/Public/Bug/Display.html?id=86684 |
27
|
|
|
|
|
|
|
ssl_opts => { |
28
|
|
|
|
|
|
|
SSL_verify_mode => SSL_VERIFY_PEER(), |
29
|
|
|
|
|
|
|
# forcing version yields better error message: |
30
|
|
|
|
|
|
|
SSL_version => 'TLSv1_2', |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has 'token', is => 'ro', required => 1; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has 'key', is => 'ro', required => 1; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has 'client_id', is => 'ro'; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has 'client_secret', is => 'ro'; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has 'access_token', is => 'rw'; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has 'api_url', ( |
48
|
|
|
|
|
|
|
is => 'ro', |
49
|
|
|
|
|
|
|
writer => '_set_api_url', |
50
|
|
|
|
|
|
|
default => 'https://api.moip.com.br/v2' |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has 'oauth_url', ( |
54
|
|
|
|
|
|
|
is => 'ro', |
55
|
|
|
|
|
|
|
writer => '_set_oauth_url', |
56
|
|
|
|
|
|
|
default => 'https://connect.moip.com.br' |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has 'sandbox', ( |
61
|
|
|
|
|
|
|
is => 'rw', |
62
|
|
|
|
|
|
|
default => 0, |
63
|
|
|
|
|
|
|
trigger => sub { |
64
|
|
|
|
|
|
|
my ($self, $sandbox) = @_; |
65
|
|
|
|
|
|
|
$self->_set_api_url( $sandbox |
66
|
|
|
|
|
|
|
? 'https://sandbox.moip.com.br/v2' |
67
|
|
|
|
|
|
|
: 'https://api.moip.com.br/v2' |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
$self->_set_oauth_url( $sandbox |
70
|
|
|
|
|
|
|
? 'https://connect-sandbox.moip.com.br' |
71
|
|
|
|
|
|
|
: 'https://connect.moip.com.br' |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
has '_basic_auth_token', is => 'ro', lazy => 1, default => sub { |
77
|
|
|
|
|
|
|
my $self = shift; |
78
|
|
|
|
|
|
|
'Basic '.MIME::Base64::encode_base64( $self->token .':'. $self->key, ''); |
79
|
|
|
|
|
|
|
}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub build_authorization_url { |
82
|
1
|
|
|
1
|
1
|
473
|
my ($self, $redirect_uri, $scope) = @_; |
83
|
1
|
50
|
33
|
|
|
8
|
die 'Method signature is: build_authorization_url($redirect_uri, $scope)' |
84
|
|
|
|
|
|
|
unless $redirect_uri && $scope; |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
9
|
my $url = URI->new($self->oauth_url.'/oauth/authorize'); |
87
|
|
|
|
|
|
|
$url->query_form( |
88
|
|
|
|
|
|
|
response_type => 'code', |
89
|
|
|
|
|
|
|
client_id => $self->client_id, |
90
|
|
|
|
|
|
|
redirect_uri => $redirect_uri, |
91
|
1
|
|
|
|
|
5803
|
scope => join(',', map { uc } @$scope) |
|
3
|
|
|
|
|
10
|
|
92
|
|
|
|
|
|
|
); |
93
|
1
|
|
|
|
|
172
|
$url; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub request_access_token { |
97
|
2
|
|
|
2
|
1
|
324
|
my ($self, $redirect_uri, $code) = @_; |
98
|
2
|
50
|
33
|
|
|
11
|
die 'Method signature is: request_access_token($redirect_uri, $code)' |
99
|
|
|
|
|
|
|
unless $redirect_uri && $code; |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
9
|
my $uri = URI->new($self->oauth_url.'/oauth/token'); |
102
|
2
|
|
|
|
|
94
|
$uri->query_form( |
103
|
|
|
|
|
|
|
client_id => $self->client_id, |
104
|
|
|
|
|
|
|
client_secret => $self->client_secret, |
105
|
|
|
|
|
|
|
grant_type => 'authorization_code', |
106
|
|
|
|
|
|
|
redirect_uri => $redirect_uri, |
107
|
|
|
|
|
|
|
code => $code |
108
|
|
|
|
|
|
|
); |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
166
|
my ($url, $body) = $uri->as_string =~ /(.*?)\?(.*)/; |
111
|
2
|
|
|
|
|
49
|
my $res = $self->ua->post($url, [ |
112
|
|
|
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
113
|
|
|
|
|
|
|
'Authentication' => $self->_basic_auth_token, |
114
|
|
|
|
|
|
|
'Cache-Control:' => 'no-cache' |
115
|
|
|
|
|
|
|
], $body); |
116
|
|
|
|
|
|
|
|
117
|
2
|
100
|
|
|
|
1245
|
return { error => $res->status_line } unless $res->is_success; |
118
|
1
|
|
|
|
|
39
|
my $data = $JSON->decode($res->content); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub endpoint { |
125
|
5
|
|
|
5
|
1
|
369
|
my ($self, $path) = @_; |
126
|
5
|
50
|
|
|
|
11
|
die "Syntax: moip->endpoint()" unless $path; |
127
|
|
|
|
|
|
|
Net::Moip::V2::Endpoint->new( |
128
|
|
|
|
|
|
|
path => $path, |
129
|
5
|
|
|
|
|
9
|
map { $_ => $self->$_ } qw/ ua api_url token key client_id client_secret / |
|
30
|
|
|
|
|
124
|
|
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub get { |
134
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
135
|
0
|
|
|
|
|
|
my $endpoint = shift; |
136
|
0
|
|
|
|
|
|
$self->endpoint($endpoint)->get(@_); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub post { |
140
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
141
|
0
|
|
|
|
|
|
my $endpoint = shift; |
142
|
0
|
|
|
|
|
|
$self->endpoint($endpoint)->post(@_); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub decode_json { |
147
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
148
|
0
|
|
|
|
|
|
$JSON->decode($_[0]); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
__END__ |