| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WebService::Async::SmartyStreets; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Access SmartyStreet API |
|
3
|
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
115035
|
use strict; |
|
|
1
|
|
|
|
|
10
|
|
|
|
1
|
|
|
|
|
30
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
45
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.001'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
WebService::Async::SmartyStreets - calls the SmartyStreets API and checks for the validity of the address |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $ss = WebService::Async::SmartyStreets->new( |
|
16
|
|
|
|
|
|
|
# Obtain these from your SmartyStreets account page. |
|
17
|
|
|
|
|
|
|
# These will be used for US lookups |
|
18
|
|
|
|
|
|
|
us_auth_id => '...', |
|
19
|
|
|
|
|
|
|
us_token => '...', |
|
20
|
|
|
|
|
|
|
# For non-US address lookups, you would also need an international token |
|
21
|
|
|
|
|
|
|
international_auth_id => '...', |
|
22
|
|
|
|
|
|
|
international_token => '...', |
|
23
|
|
|
|
|
|
|
); |
|
24
|
|
|
|
|
|
|
IO::Async::Loop->new->add($ss); |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
print $ss->verify( |
|
27
|
|
|
|
|
|
|
city => 'Atlanta', |
|
28
|
|
|
|
|
|
|
country => 'US', |
|
29
|
|
|
|
|
|
|
geocode => 1 |
|
30
|
|
|
|
|
|
|
)->get->status; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
This module provides basic support for the L. |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Note that this module uses L. |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
|
39
|
|
|
|
|
|
|
|
|
40
|
1
|
|
|
1
|
|
400
|
use parent qw(IO::Async::Notifier); |
|
|
1
|
|
|
|
|
275
|
|
|
|
1
|
|
|
|
|
5
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
1
|
|
|
1
|
|
4100
|
use mro; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
6
|
|
|
43
|
1
|
|
|
1
|
|
439
|
no indirect; |
|
|
1
|
|
|
|
|
1042
|
|
|
|
1
|
|
|
|
|
5
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
1
|
|
|
1
|
|
548
|
use URI; |
|
|
1
|
|
|
|
|
4660
|
|
|
|
1
|
|
|
|
|
35
|
|
|
46
|
1
|
|
|
1
|
|
456
|
use URI::QueryParam; |
|
|
1
|
|
|
|
|
809
|
|
|
|
1
|
|
|
|
|
36
|
|
|
47
|
|
|
|
|
|
|
|
|
48
|
1
|
|
|
1
|
|
461
|
use Future::AsyncAwait; |
|
|
1
|
|
|
|
|
3055
|
|
|
|
1
|
|
|
|
|
5
|
|
|
49
|
1
|
|
|
1
|
|
605
|
use Net::Async::HTTP; |
|
|
1
|
|
|
|
|
126969
|
|
|
|
1
|
|
|
|
|
48
|
|
|
50
|
1
|
|
|
1
|
|
435
|
use JSON::MaybeUTF8 qw(:v1); |
|
|
1
|
|
|
|
|
8205
|
|
|
|
1
|
|
|
|
|
135
|
|
|
51
|
1
|
|
|
1
|
|
492
|
use Syntax::Keyword::Try; |
|
|
1
|
|
|
|
|
1028
|
|
|
|
1
|
|
|
|
|
5
|
|
|
52
|
1
|
|
|
1
|
|
80
|
use Scalar::Util qw(blessed); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
57
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
1
|
|
|
1
|
|
524
|
use WebService::Async::SmartyStreets::Address; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
42
|
|
|
55
|
|
|
|
|
|
|
|
|
56
|
1
|
|
|
1
|
|
6
|
use Log::Any qw($log); |
|
|
1
|
|
|
|
|
13
|
|
|
|
1
|
|
|
|
|
11
|
|
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 verify |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Makes connection to SmartyStreets API and parses the response into WebService::Async::SmartyStreets::Address. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $addr = $ss->verify(%address_to_check)->get; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Takes the following named parameters: |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=over 4 |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item * C - country (required) |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item * C - address line 1 |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item * C - address line 2 |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * C - name of organization (usually building names) |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item * C - city |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item * C - state |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item * C - post code |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item * C - true or false |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=back |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Returns a L which should resolve to a valid L instance. |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# keyword 'async' will cause critic test fail. disable it |
|
91
|
|
|
|
|
|
|
## no critic (RequireEndWithOne.pm) |
|
92
|
4
|
|
|
4
|
1
|
196
|
async sub verify { |
|
93
|
4
|
|
|
|
|
18
|
my ($self, %args) = @_; |
|
94
|
|
|
|
|
|
|
|
|
95
|
4
|
|
|
|
|
12
|
my $uri = $self->country_endpoint($args{country})->clone; |
|
96
|
|
|
|
|
|
|
|
|
97
|
4
|
|
|
|
|
8661
|
$uri->query_param($_ => $args{$_}) for keys %args; |
|
98
|
|
|
|
|
|
|
$uri->query_param( |
|
99
|
4
|
|
50
|
|
|
7644
|
'auth-id' => ($self->auth_id($args{country}) // die 'need an auth ID'), |
|
100
|
|
|
|
|
|
|
); |
|
101
|
|
|
|
|
|
|
$uri->query_param( |
|
102
|
4
|
|
50
|
|
|
1539
|
'auth-token' => ($self->token($args{country}) // die 'need an auth token'), |
|
103
|
|
|
|
|
|
|
); |
|
104
|
4
|
|
|
|
|
1717
|
$uri->query_param( |
|
105
|
|
|
|
|
|
|
'input-id' => $self->next_id, |
|
106
|
|
|
|
|
|
|
); |
|
107
|
4
|
|
|
|
|
1757
|
$log->tracef('GET %s', '' . $uri); |
|
108
|
|
|
|
|
|
|
|
|
109
|
4
|
|
|
|
|
424
|
my $decoded = await get_decoded_data($self, $uri); |
|
110
|
|
|
|
|
|
|
|
|
111
|
3
|
|
|
|
|
186
|
$log->tracef('=> %s', $decoded); |
|
112
|
3
|
100
|
|
|
|
8242
|
$decoded = [$decoded] unless ref($decoded) eq 'ARRAY'; |
|
113
|
|
|
|
|
|
|
|
|
114
|
3
|
|
|
|
|
8
|
return map { WebService::Async::SmartyStreets::Address->new(%$_) } @$decoded; |
|
|
3
|
|
|
|
|
17
|
|
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 METHODS - Accessors |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub country_endpoint { |
|
122
|
4
|
|
|
4
|
0
|
12
|
my ($self, $country) = @_; |
|
123
|
4
|
50
|
|
|
|
13
|
return $self->us_endpoint if uc($country) eq 'US'; |
|
124
|
4
|
|
|
|
|
9
|
return $self->international_endpoint; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub us_endpoint { |
|
128
|
0
|
|
0
|
0
|
0
|
0
|
shift->{us_endpoint} //= URI->new('https://us-street.api.smartystreets.com/street-address'); |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub international_endpoint { |
|
132
|
4
|
|
66
|
4
|
0
|
22
|
shift->{international_endpoint} //= URI->new('https://international-street.api.smartystreets.com/verify'); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub auth_id { |
|
136
|
4
|
|
|
4
|
1
|
13
|
my ($self, $country) = @_; |
|
137
|
4
|
50
|
|
|
|
16
|
return $self->{us_auth_id} if uc($country) eq 'US'; |
|
138
|
4
|
|
|
|
|
18
|
return $self->{international_auth_id}; |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub token { |
|
142
|
4
|
|
|
4
|
1
|
16
|
my ($self, $country) = @_; |
|
143
|
4
|
50
|
|
|
|
12
|
return $self->{us_token} if uc($country) eq 'US'; |
|
144
|
4
|
|
|
|
|
24
|
return $self->{international_token}; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 METHODS - Internal |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 get_decoded_data |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Calls the SmartyStreets API then decode and parses the response give by SmartyStreets |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
my $decoded = await get_decoded_data($self, $uri) |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Takes the following parameters: |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=over 4 |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item * C<$uri> - URI for endpoint |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=back |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
More information on the response can be seen in L. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Returns a L which resolves to an arrayref of L instances. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |
|
168
|
|
|
|
|
|
|
|
|
169
|
1
|
|
|
1
|
|
3
|
async sub get_decoded_data { |
|
170
|
1
|
|
|
|
|
3
|
my $self = shift; |
|
171
|
1
|
|
|
|
|
2
|
my $uri = shift; |
|
172
|
|
|
|
|
|
|
|
|
173
|
1
|
|
|
|
|
2
|
my $res; |
|
174
|
|
|
|
|
|
|
try { |
|
175
|
|
|
|
|
|
|
$res = await $self->ua->GET($uri); |
|
176
|
1
|
|
|
|
|
4
|
} catch ($e) { |
|
177
|
|
|
|
|
|
|
if (blessed($e) and $e->isa('Future::Exception')) { |
|
178
|
|
|
|
|
|
|
my ($payload) = $e->details; |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
if (blessed($payload) && $payload->can('content')) { |
|
181
|
|
|
|
|
|
|
my $resp = decode_json_utf8($payload->content); |
|
182
|
|
|
|
|
|
|
my $errors = $resp->{errors} // []; |
|
183
|
|
|
|
|
|
|
my ($error) = $errors->@*; |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
if ($error && $error->{message}) { |
|
186
|
|
|
|
|
|
|
$log->warnf(sprintf("SmartyStreets HTTP status %d error: %s", $payload->code, $error->{message})); |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
} |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
die 'Unable to retrieve response.'; |
|
192
|
|
|
|
|
|
|
}; |
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
0
|
my $response = decode_json_utf8($res->decoded_content); |
|
195
|
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
0
|
return $response->[0]; |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 configure |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Configures the instance. |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Takes the following named parameters: |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=over 4 |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item * C - auth_id obtained from SmartyStreet |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item * C - token obtained from SmartyStreet |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item * C - auth_id obtained from SmartyStreet |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=item * C - token obtained from SmartyStreet |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=back |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Note that you can provide US, international or both API tokens - if an API token |
|
218
|
|
|
|
|
|
|
is not available for a L call, then it will return a failed L. |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub configure { |
|
223
|
2
|
|
|
2
|
1
|
4732
|
my ($self, %args) = @_; |
|
224
|
2
|
|
|
|
|
7
|
for my $k (qw(international_auth_id international_token us_auth_id us_token)) { |
|
225
|
8
|
100
|
|
|
|
31
|
$self->{$k} = delete $args{$k} if exists $args{$k}; |
|
226
|
|
|
|
|
|
|
} |
|
227
|
2
|
|
|
|
|
12
|
$self->next::method(%args); |
|
228
|
|
|
|
|
|
|
} |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub next_id { |
|
231
|
4
|
|
100
|
4
|
0
|
29
|
++(shift->{id} //= 'AA00000000'); |
|
232
|
|
|
|
|
|
|
} |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 ua |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Accessor for the L instance which will be used for SmartyStreets API requests. |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub ua { |
|
241
|
1
|
|
|
1
|
1
|
4
|
my ($self) = @_; |
|
242
|
1
|
|
33
|
|
|
5
|
$self->{ua} //= do { |
|
243
|
1
|
|
|
|
|
12
|
$self->add_child( |
|
244
|
|
|
|
|
|
|
my $ua = Net::Async::HTTP->new( |
|
245
|
|
|
|
|
|
|
fail_on_error => 1, |
|
246
|
|
|
|
|
|
|
decode_content => 1, |
|
247
|
|
|
|
|
|
|
pipeline => 0, |
|
248
|
|
|
|
|
|
|
max_connections_per_host => 4, |
|
249
|
|
|
|
|
|
|
user_agent => |
|
250
|
|
|
|
|
|
|
'Mozilla/4.0 (WebService::Async::SmartyStreets; BINARY@cpan.org; https://metacpan.org/pod/WebService::Async::SmartyStreets)', |
|
251
|
|
|
|
|
|
|
)); |
|
252
|
1
|
|
|
|
|
153
|
$ua; |
|
253
|
|
|
|
|
|
|
} |
|
254
|
|
|
|
|
|
|
} |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
1; |
|
257
|
|
|
|
|
|
|
|