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