File Coverage

blib/lib/WebService/Postex.pm
Criterion Covered Total %
statement 55 76 72.3
branch 5 8 62.5
condition n/a
subroutine 15 20 75.0
pod 6 6 100.0
total 81 110 73.6


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