File Coverage

blib/lib/WebService/ValidSign/API/DocumentPackage.pm
Criterion Covered Total %
statement 64 86 74.4
branch 12 22 54.5
condition 2 6 33.3
subroutine 14 16 87.5
pod 5 7 71.4
total 97 137 70.8


line stmt bran cond sub pod time code
1             package WebService::ValidSign::API::DocumentPackage;
2             our $VERSION = '0.003';
3 3     3   2277 use Moo;
  3         8  
  3         18  
4 3     3   1021 use namespace::autoclean;
  3         8  
  3         17  
5              
6             # ABSTRACT: A REST API client for ValidSign
7              
8 3     3   1575 use WebService::ValidSign::Object::Sender;
  3         71  
  3         131  
9 3     3   21 use HTTP::Request;
  3         7  
  3         64  
10 3     3   1448 use HTTP::Request::Common;
  3         6658  
  3         254  
11 3     3   39 use Carp qw(croak);
  3         6  
  3         141  
12 3     3   20 use List::Util qw(first);
  3         6  
  3         3076  
13              
14             has action_endpoint => (
15             is => 'ro',
16             default => 'packages'
17             );
18              
19             sub details {
20 0     0 0 0 my ($self, $package) = @_;
21 0         0 my $uri = $self->get_endpoint($self->action_endpoint, $package->id);
22 0         0 my $request = HTTP::Request->new(
23             GET => $uri,
24             [
25             'Content-Type' => 'application/json',
26             Accept => 'application/json',
27             ]
28             );
29              
30 0         0 return $self->call_api($request);
31             }
32              
33             sub create_with_documents {
34 2     2 1 3250 my ($self, $package) = @_;
35              
36 2 100       11 if ($package->has_id) {
37 1         27 croak("Package is already created, it has an ID");
38             }
39              
40 1         11 my $uri = $self->get_endpoint($self->action_endpoint);
41 1         23 my $json = $self->json->encode($package);
42              
43 1         7 my $request = $self->_add_documents($package, $uri, $json);
44              
45 1         604 my $response = $self->call_api($request);
46 1         162 $package->id($response->{id});
47 1         20 return $response->{id};
48             }
49              
50             sub _add_documents {
51 1     1   4 my ($self, $package, $uri, $json) = @_;
52              
53 1         2 my @files;
54              
55 1 50       18 if (!$package->has_documents) {
56 0         0 croak("Unable to add documents, we have none!");
57             }
58              
59 1 50       7 if ($package->count_documents == 1) {
60 1         38 push(@files, ( file => [ $package->documents->[0]->path ] ));
61             }
62             else {
63 0         0 foreach (@{$package->documents}) {
  0         0  
64 0         0 push(@files, ( 'file[]' => [ $_->path ] ))
65             }
66             }
67              
68             # Monkey patch so LWP::MediaTypes can deal with us
69 1     1   15 local *File::Temp::path = sub { return shift->filename };
  1         18945  
70              
71 1 50       9 return POST $uri,
72             'Content_Type' => 'form-data',
73             Accept => 'application/json',
74             Content => [
75             defined $json ? (payload => $json) : (),
76             @files,
77             ];
78             }
79              
80             sub create {
81 2     2 1 3285 my ($self, $package) = @_;
82              
83 2 100       19 if ($package->has_id) {
84 1         23 croak("Package is already created, it has an ID");
85             }
86              
87 1 50       5 if ($package->has_documents) {
88 0         0 return $self->create_with_documents($package);
89             }
90              
91 1         22 my $json = $self->json->encode($package);
92 1         11 my $uri = $self->get_endpoint($self->action_endpoint);
93              
94 1         27 my $request = HTTP::Request->new(
95             POST => $uri,
96             [
97             'Content-Type' => 'application/json',
98             Accept => 'application/json',
99             ],
100             $json
101             );
102              
103 1         209 my $response = $self->call_api($request);
104 1         226 $package->id($response->{id});
105 1         24 return $response->{id};
106             }
107              
108             sub add_document {
109 0     0 0 0 my ($self, $package) = @_;
110              
111 0 0       0 if (!$package->has_id) {
112 0         0 croak("Please create a document package first on the ValidSign endpoint");
113             }
114              
115 0         0 my $json = $self->json->encode($package);
116              
117 0         0 my $uri = $self->get_endpoint(
118             $self->action_endpoint,
119             $package->id,
120             'documents'
121             );
122              
123 0         0 my $req = $self->_add_documents($package, $uri, $json);
124 0         0 my $response = $self->call_api($req);
125 0         0 return $response->{id};
126             }
127              
128             sub find {
129 1     1 1 1592 my ($self, $id) = @_;
130              
131 1         11 my $uri = $self->get_endpoint($self->action_endpoint, $id);
132 1         14 my $request = HTTP::Request->new(
133             GET => $uri,
134             [
135             'Content-Type' => 'application/json',
136             Accept => 'application/json',
137             ]
138             );
139              
140 1         163 my $res = $self->call_api($request);
141 1         167 return WebService::ValidSign::Object::DocumentPackage->new(%$res);
142             }
143              
144             sub download_document {
145 1     1 1 569 my ($self, $package, $document_id) = @_;
146              
147 1 50       8 if (!$package->has_id) {
148 0         0 croak("Please create a document package first on the ValidSign endpoint");
149             }
150              
151 1 50 33     13 if (!$document_id && !$package->count_documents) {
152 0         0 croak( "Unable to download documents, package has none, or you did"
153             . " not supply one!");
154             }
155 1   33     31 $document_id //= $package->documents->[0]->{id};
156              
157 1         15 my $uri = $self->get_endpoint($self->action_endpoint, $package->id,
158             'documents', $document_id, 'pdf');
159              
160 1         7 return $self->download_file($uri);
161              
162 0         0 return;
163             }
164              
165             sub download_documents {
166 1     1 1 1602 my ($self, $package) = @_;
167              
168 1 50       6 if (!$package->has_id) {
169 0         0 croak("Please create a document package first on the ValidSign endpoint");
170             }
171              
172 1 50       19 if ($package->has_documents) {
173 1         6 my $uri = $self->get_endpoint($self->action_endpoint, $package->id,
174             qw(documents zip));
175 1         5 return $self->download_file($uri);
176             }
177 0           return;
178              
179             }
180              
181             with "WebService::ValidSign::API";
182              
183             __PACKAGE__->meta->make_immutable;
184              
185             __END__
186              
187             =pod
188              
189             =encoding UTF-8
190              
191             =head1 NAME
192              
193             WebService::ValidSign::API::DocumentPackage - A REST API client for ValidSign
194              
195             =head1 VERSION
196              
197             version 0.003
198              
199             =head1 SYNOPSIS
200              
201             You should not need to instantiate this object yourself. Use L<WebService::ValidSign> for this.
202              
203             use WebService::ValidSign;
204             my $api = WebService::ValidSign->new(
205             secret => 'foo',
206             endpoint => 'https://some.url/api',
207             );
208              
209             my $pkg_api = $api->package;
210              
211             =head1 ATTRIBUTES
212              
213             =head2 action_endpoints
214              
215             Implement the endpoint as required by L<WebService::ValidSign::API>.
216              
217             =head1 METHODS
218              
219             =head2 find
220              
221             $self->find("someid");
222              
223             Find a document package based on the ID. You get a L<WebService::ValidSign::Object::DocumentPackage> object when we have found the document package.
224              
225             CAVEAT!
226              
227             The object is not full up to spec, as the documents are still an arrayref
228             filled with hashrefs. Later implementations will try to fix this issue.
229              
230             =head2 create
231              
232             $self->create($pkg);
233              
234             Create a document package on the ValidSign side. You need to pass a
235             L<Webservice::ValidSign::Object::DocumentPackage> to the call. It cannot have
236             and ID as you would be able to create two packages with the same ID. You can
237             call this function with, or without documents attached to the document package.
238              
239             =head2 create_with_documents
240              
241             $self->create_with_documents($pkg);
242              
243             Similar to the create call, but this one can only be called when there are documents attached to the document package.
244              
245             =head2 download_document
246              
247             $self->download_document($pkg, $document_id);
248              
249             Download a document from package. When no document id is supplied we will only download the first document. If you supply one, we will use this document id.
250             There is not check to see if the document actually exists in the document package. Callers should check these themselves (via the C<find> command).
251              
252             =head2 download_documents
253              
254             $self->download_documents($pkg);
255              
256             Download all documents from the package. You will get a filehandle to a zip
257             file. Use L<Archive::ZIP> to extract the files.
258              
259             =head1 AUTHOR
260              
261             Wesley Schwengle <waterkip@cpan.org>
262              
263             =head1 COPYRIGHT AND LICENSE
264              
265             This software is Copyright (c) 2019 by Wesley Schwengle.
266              
267             This is free software, licensed under:
268              
269             The (three-clause) BSD License
270              
271             =cut