line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
376216
|
use utf8; |
|
3
|
|
|
|
|
97
|
|
|
3
|
|
|
|
|
19
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package WebService::Postex; |
4
|
|
|
|
|
|
|
our $VERSION = '0.003'; |
5
|
3
|
|
|
3
|
|
2111
|
use Moose; |
|
3
|
|
|
|
|
1431283
|
|
|
3
|
|
|
|
|
17
|
|
6
|
3
|
|
|
3
|
|
24325
|
use namespace::autoclean; |
|
3
|
|
|
|
|
27512
|
|
|
3
|
|
|
|
|
12
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
2491
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
128089
|
|
|
3
|
|
|
|
|
109
|
|
9
|
3
|
|
|
3
|
|
1632
|
use HTTP::Request::Common; |
|
3
|
|
|
|
|
7047
|
|
|
3
|
|
|
|
|
217
|
|
10
|
3
|
|
|
3
|
|
1467
|
use MooseX::Types::URI qw(Uri); |
|
3
|
|
|
|
|
343680
|
|
|
3
|
|
|
|
|
22
|
|
11
|
3
|
|
|
3
|
|
5472
|
use Carp qw(croak); |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
163
|
|
12
|
3
|
|
|
3
|
|
2500
|
use JSON::XS; |
|
3
|
|
|
|
|
16830
|
|
|
3
|
|
|
|
|
3256
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# ABSTRACT: A Postex WebService implemenation in Perl |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has base_uri => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
isa => Uri, |
19
|
|
|
|
|
|
|
coerce => 1, |
20
|
|
|
|
|
|
|
required => 1, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has generator_id => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
isa => 'Str', |
26
|
|
|
|
|
|
|
required => 1, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has secret => ( |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
isa => 'Str', |
32
|
|
|
|
|
|
|
required => 1, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has ua => ( |
36
|
|
|
|
|
|
|
is => 'ro', |
37
|
|
|
|
|
|
|
isa => 'LWP::UserAgent', |
38
|
|
|
|
|
|
|
lazy => 1, |
39
|
|
|
|
|
|
|
builder => '_build_ua', |
40
|
|
|
|
|
|
|
trigger => \&_set_ua_defaults, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _set_ua_defaults { |
45
|
3
|
|
|
3
|
|
12
|
my ($self, $ua) = @_; |
46
|
3
|
|
|
|
|
16
|
$ua->default_header(Accept => 'application/json'); |
47
|
3
|
|
|
|
|
332
|
$ua->default_header(Authorization => "Bearer " . $self->secret); |
48
|
3
|
|
|
|
|
244
|
return $ua; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _build_ua { |
53
|
2
|
|
|
2
|
|
5
|
my $self = shift; |
54
|
2
|
|
|
|
|
31
|
my $ua = LWP::UserAgent->new( |
55
|
|
|
|
|
|
|
agent => sprintf('%s/%s', __PACKAGE__, $VERSION), |
56
|
|
|
|
|
|
|
timeout => 30, |
57
|
|
|
|
|
|
|
); |
58
|
2
|
|
|
|
|
6702
|
return $self->_set_ua_defaults($ua); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _call { |
62
|
3
|
|
|
3
|
|
11
|
my ($self, $req) = @_; |
63
|
|
|
|
|
|
|
|
64
|
3
|
|
|
|
|
111
|
my $res = $self->ua->request($req); |
65
|
|
|
|
|
|
|
|
66
|
3
|
50
|
|
|
|
2083
|
unless ($res->is_success) { |
67
|
0
|
|
|
|
|
0
|
my $uri = $req->uri . ""; |
68
|
0
|
|
|
|
|
0
|
die "Unsuccesful request to $uri: " . $res->status_line, $/; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
3
|
|
|
|
|
51
|
my $json = decode_json($res->decoded_content); |
72
|
3
|
50
|
|
|
|
562
|
if ($json->{status} eq 'error') { |
73
|
0
|
|
|
|
|
0
|
die "Error occurred calling Postex", $/; |
74
|
|
|
|
|
|
|
} |
75
|
3
|
|
|
|
|
45
|
return $json; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub generation_rest_upload { |
80
|
2
|
|
|
2
|
1
|
2831
|
my ($self, %payload) = @_; |
81
|
|
|
|
|
|
|
|
82
|
2
|
|
|
|
|
81
|
my $uri = $self->_build_uri(qw(generation raw), $self->generator_id); |
83
|
2
|
|
|
|
|
13
|
my $req = $self->_prepare_post($uri, %payload); |
84
|
2
|
|
|
|
|
1057
|
return $self->_call($req); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub generation_rest_upload_check { |
88
|
0
|
|
|
0
|
1
|
0
|
my ($self, %payload) = @_; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
0
|
my $uri = $self->_build_uri(qw(generation raw), $self->generator_id); |
91
|
0
|
|
|
|
|
0
|
my $req = $self->_prepare_get($uri, %payload); |
92
|
0
|
|
|
|
|
0
|
return $self->_call($req); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub generation_file_upload { |
96
|
1
|
|
|
1
|
1
|
17001
|
my ($self, %payload) = @_; |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
40
|
my $uri = $self->_build_uri(qw(generation upload), $self->generator_id); |
99
|
1
|
|
|
|
|
6
|
my $req = $self->_prepare_post($uri, %payload); |
100
|
1
|
|
|
|
|
19054
|
return $self->_call($req); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub generation_file_upload_check { |
104
|
0
|
|
|
0
|
1
|
0
|
my ($self, %payload) = @_; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
0
|
my $uri = $self->_build_uri(qw(generation upload), $self->generator_id); |
107
|
0
|
|
|
|
|
0
|
my $req = $self->_prepare_get($uri, %payload); |
108
|
0
|
|
|
|
|
0
|
return $self->_call($req); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub generation_session_status { |
112
|
0
|
|
|
0
|
1
|
0
|
my ($self, $session_id) = @_; |
113
|
0
|
|
|
|
|
0
|
my $uri = $self->_build_uri(qw(generation session), $session_id); |
114
|
0
|
|
|
|
|
0
|
my $req = $self->_prepare_get($uri); |
115
|
0
|
|
|
|
|
0
|
return $self->_call($req); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub profile_file_upload { |
119
|
0
|
|
|
0
|
1
|
0
|
my ($self, $recipient_id, %payload) = @_; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
0
|
my $uri = $self->_build_uri(qw(recipients upload), $recipient_id); |
122
|
0
|
|
|
|
|
0
|
my $req = $self->_prepare_post($uri, %payload); |
123
|
0
|
|
|
|
|
0
|
return $self->_call($req); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _build_uri { |
127
|
3
|
|
|
3
|
|
11
|
my ($self, $type, $call, $id) = @_; |
128
|
|
|
|
|
|
|
|
129
|
3
|
|
|
|
|
87
|
my $uri = $self->base_uri->clone; |
130
|
3
|
|
|
|
|
168
|
my @segments = $uri->path_segments; |
131
|
3
|
|
|
|
|
134
|
$uri->path_segments(@segments, qw(rest data v1), $type, $call, $id); |
132
|
3
|
|
|
|
|
308
|
return $uri; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub _prepare_request { |
136
|
3
|
|
|
3
|
|
10
|
my ($self, $uri, %payload) = @_; |
137
|
|
|
|
|
|
|
|
138
|
3
|
100
|
|
|
|
13
|
if (my $file = delete $payload{file}) { |
139
|
1
|
|
|
|
|
4
|
$payload{file} = [$file, delete $payload{filename}]; |
140
|
|
|
|
|
|
|
return ( |
141
|
1
|
|
|
|
|
8
|
$uri, |
142
|
|
|
|
|
|
|
Content => [%payload], |
143
|
|
|
|
|
|
|
'Content-Type' => 'form-data', |
144
|
|
|
|
|
|
|
); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
return ( |
148
|
2
|
50
|
|
|
|
36
|
$uri, |
149
|
|
|
|
|
|
|
%payload |
150
|
|
|
|
|
|
|
? ( |
151
|
|
|
|
|
|
|
Content => encode_json(\%payload), |
152
|
|
|
|
|
|
|
'Content-Type' => 'application/json', |
153
|
|
|
|
|
|
|
) |
154
|
|
|
|
|
|
|
: (), |
155
|
|
|
|
|
|
|
); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub _prepare_post { |
160
|
3
|
|
|
3
|
|
12
|
my ($self, $uri, %payload) = @_; |
161
|
3
|
|
|
|
|
13
|
return POST($self->_prepare_request($uri, %payload)); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub _prepare_get { |
165
|
0
|
|
|
0
|
|
|
my ($self, $uri, %payload) = @_; |
166
|
0
|
|
|
|
|
|
return GET($self->_prepare_request($uri, %payload)); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__END__ |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=pod |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=encoding UTF-8 |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 NAME |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
WebService::Postex - A Postex WebService implemenation in Perl |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 VERSION |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
version 0.003 |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 SYNOPSIS |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
use WebService::Postex; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
my $postex = WebService::Postex->new( |
191
|
|
|
|
|
|
|
base_uri => 'https://demo.postex.com', |
192
|
|
|
|
|
|
|
generator_id => 1234, |
193
|
|
|
|
|
|
|
secret => 'yoursecret', |
194
|
|
|
|
|
|
|
); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
my %args = (); |
197
|
|
|
|
|
|
|
$postex->generation_file_upload(%args); |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 DESCRIPTION |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
A Perl API for connecting with the Postex REST API |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 base_uri |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Required. The endpoint to which to talk to |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 generator_id |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Required. The generator ID you get from Postex |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 secret |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Required. The secret for the authorization token. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 METHODS |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 generation_file_upload |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 generation_file_upload_check |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 generation_rest_upload |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 generation_rest_upload_check |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head2 generation_session_status |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 profile_file_upload |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 SEE ALSO |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=over |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item L<Postex|https://www.postex.com> |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=back |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 AUTHOR |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Wesley Schwengle <waterkip@cpan.org> |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
This software is Copyright (c) 2020 by Wesley Schwengle. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
This is free software, licensed under: |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
The (three-clause) BSD License |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=cut |