File Coverage

blib/lib/WebService/ValidSign/API.pm
Criterion Covered Total %
statement 47 62 75.8
branch 5 10 50.0
condition 1 3 33.3
subroutine 12 13 92.3
pod 0 4 0.0
total 65 92 70.6


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