File Coverage

blib/lib/WebService/ValidSign/API.pm
Criterion Covered Total %
statement 47 62 75.8
branch 4 8 50.0
condition 1 3 33.3
subroutine 12 13 92.3
pod 0 4 0.0
total 64 90 71.1


line stmt bran cond sub pod time code
1             package WebService::ValidSign::API;
2              
3             our $VERSION = '0.003';
4 3     3   1701 use Moo::Role;
  3         7  
  3         22  
5 3     3   1206 use namespace::autoclean;
  3         7  
  3         18  
6             with 'WebService::ValidSign::API::Constructor';
7              
8             # ABSTRACT: A REST API client for ValidSign
9              
10 3     3   235 use Carp qw(croak);
  3         8  
  3         174  
11 3     3   20 use HTTP::Request;
  3         7  
  3         121  
12 3     3   19 use JSON qw(decode_json);
  3         6  
  3         30  
13 3     3   387 use URI;
  3         7  
  3         111  
14 3         37 use WebService::ValidSign::Types qw(
15             WebServiceValidSignURI
16             WebServiceValidSignAuthModule
17 3     3   19 );
  3         7  
18              
19             requires qw(action_endpoint);
20              
21             has json => (
22             is => 'ro',
23             builder => 1,
24             lazy => 1,
25             );
26              
27             sub _build_json {
28 1     1   16 require JSON::XS;
29 1         22 return JSON::XS->new->convert_blessed;
30             }
31              
32             has auth => (
33             is => 'ro',
34             required => 1,
35             isa => WebServiceValidSignAuthModule,
36             );
37              
38             sub get_endpoint {
39 6     6 0 44 my ($self, @misc) = @_;
40 6         60 my $uri = $self->endpoint->clone;
41 6         211 my @path = $uri->path_segments;
42 6         283 @path = grep { defined $_ } @path, @misc;
  27         58  
43 6         25 $uri->path_segments(@path);
44 6         524 return $uri;
45             }
46              
47             sub download_file {
48 2     2 0 6 my ($self, $uri) = @_;
49              
50 2         24 my $fh = File::Temp->new();
51 2         835 my $request = HTTP::Request->new(
52             GET => $uri,
53             [
54             'Content-Type' => 'application/json',
55             ]
56             );
57              
58             $self->call_api_download(
59             $request,
60             sub {
61 0     0   0 my ($chunk, $res, $proto) = @_;
62 0         0 print $fh $chunk;
63             }
64 2         247 );
65 2         17 $fh->seek(0,0);
66 2         42 return $fh;
67             }
68              
69             sub call_api_download {
70 2     2 0 7 my ($self, $req, @opts) = @_;
71              
72 2         51 my $res = $self->lwp->request($req, @opts);
73 2 50       159 if (!$res->is_success) {
74 0         0 my $msg = join("$/", "", $req->as_string, $res->as_string);
75 0         0 my $apikey = $self->secret;
76 0         0 $msg =~ s/$apikey/APIKEYHIDDEN/g;
77 0         0 croak("ValidSign error: $msg");
78             }
79              
80 2         19 my $headers = $res->headers;
81              
82 2         14 foreach (qw(x-died client-aborted)) {
83 4 50       108 if (my $header = $headers->header($_)) {
84 0         0 my $msg = join("$/", "", $req->as_string, $res->as_string);
85 0         0 my $apikey = $self->secret;
86 0         0 $msg =~ s/$apikey/APIKEYHIDDEN/g;
87 0         0 die sprintf("%s: Unable to complete file download", $_);
88             }
89             }
90              
91 2         88 return 1;
92             }
93              
94             sub call_api {
95 4     4 0 13 my ($self, $req, %options) = @_;
96              
97 4 50 33     18 if ($options{with_token} && $self->can('auth')) {
98 0         0 $req->header("Authentication", $self->auth->token);
99             }
100              
101 4         114 my $res = $self->lwp->request($req);
102              
103 4 50       382 return decode_json($res->decoded_content) if $res->is_success;
104              
105 0           my $msg = join("$/", "", $req->as_string, $res->as_string);
106 0           my $apikey = $self->secret;
107 0           $msg =~ s/$apikey/APIKEYHIDDEN/g;
108 0           croak("ValidSign error: $msg");
109             }
110              
111              
112             1;
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             WebService::ValidSign::API - A REST API client for ValidSign
123              
124             =head1 VERSION
125              
126             version 0.003
127              
128             =head1 SYNOPSIS
129              
130             use WebService::ValidSign;
131              
132             my $client = WebService::ValidSign->new(
133             endpoint => 'https://hostname.validsign.nl/api'
134             );
135              
136             $client->
137              
138             =head1 ATTRIBUTES
139              
140             =over
141              
142             =item endpoint
143              
144             The API URI endpoint as described in the Acceplication Integrator's Guide
145              
146             =item lwp
147              
148             An LWP::UserAgent object. If you extend this module you can use your own
149             builder or just inject something that respects the LWP::UserAgent API.
150              
151             =back
152              
153             =head1 METHODS
154              
155             =head1 AUTHOR
156              
157             Wesley Schwengle <waterkip@cpan.org>
158              
159             =head1 COPYRIGHT AND LICENSE
160              
161             This software is Copyright (c) 2019 by Wesley Schwengle.
162              
163             This is free software, licensed under:
164              
165             The (three-clause) BSD License
166              
167             =cut