File Coverage

blib/lib/WebService/ValidSign/API.pm
Criterion Covered Total %
statement 33 40 82.5
branch 2 4 50.0
condition 1 3 33.3
subroutine 9 10 90.0
pod 0 2 0.0
total 45 59 76.2


line stmt bran cond sub pod time code
1             package WebService::ValidSign::API;
2              
3             our $VERSION = '0.002';
4 2     2   1035 use Moo::Role;
  2         5  
  2         18  
5 2     2   859 use namespace::autoclean;
  2         4  
  2         12  
6             with 'WebService::ValidSign::API::Constructor';
7              
8             # ABSTRACT: A REST API client for ValidSign
9              
10 2     2   171 use Carp qw(croak);
  2         4  
  2         133  
11 2     2   15 use HTTP::Request;
  2         4  
  2         65  
12 2     2   11 use JSON qw(decode_json);
  2         5  
  2         13  
13 2     2   243 use URI;
  2         5  
  2         65  
14 2         23 use WebService::ValidSign::Types qw(
15             WebServiceValidSignURI
16             WebServiceValidSignAuthModule
17 2     2   16 );
  2         10  
18              
19             requires qw(action_endpoint);
20              
21             has auth => (
22             is => 'ro',
23             required => 1,
24             isa => WebServiceValidSignAuthModule,
25             );
26              
27             sub get_endpoint {
28 1     1 0 5 my ($self, @misc) = @_;
29 1         18 my $uri = $self->endpoint->clone;
30 1         80 my @path = $uri->path_segments;
31 1         53 @path = grep { defined $_ } @path, @misc;
  4         9  
32 1         7 $uri->path_segments(@path);
33 1         109 return $uri;
34             }
35              
36             sub call_api {
37 1     1 0 4 my ($self, $req, %options) = @_;
38              
39 1 50 33     6 if ($options{with_token} && $self->can('auth')) {
40 0         0 $req->header("Authentication", $self->auth->token);
41             }
42              
43 1         7 $req->header("Authorization", join(" ", "Basic", $self->secret));
44              
45 1         76 my $res = $self->lwp->request($req);
46              
47 1 50       87 return decode_json($res->decoded_content) if $res->is_success;
48              
49 0           my $msg = join("$/", "", $req->as_string, $res->as_string);
50 0           my $apikey = $self->secret;
51 0           $msg =~ s/$apikey/APIKEYHIDDEN/g;
52 0           croak("ValidSign error: $msg");
53             }
54              
55             sub _build_lwp {
56 0     0     require LWP::UserAgent;
57 0           return LWP::UserAgent->new(
58             agent => join('/', __PACKAGE__, "$VERSION"),
59             protocols_allowed => [qw(https)],
60             ssl_opts => { verify_hostname => 1 },
61             requests_redirectable => [qw(HEAD GET)],
62             );
63             }
64              
65              
66             1;
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             WebService::ValidSign::API - A REST API client for ValidSign
77              
78             =head1 VERSION
79              
80             version 0.002
81              
82             =head1 SYNOPSIS
83              
84             use WebService::ValidSign;
85              
86             my $client = WebService::ValidSign->new(
87             endpoint => 'https://hostname.validsign.nl/api'
88             );
89              
90             $client->
91              
92             =head1 ATTRIBUTES
93              
94             =over
95              
96             =item endpoint
97              
98             The API URI endpoint as described in the Acceplication Integrator's Guide
99              
100             =item lwp
101              
102             An LWP::UserAgent object. If you extend this module you can use your own
103             builder or just inject something that respects the LWP::UserAgent API.
104              
105             =back
106              
107             =head1 METHODS
108              
109             =head1 AUTHOR
110              
111             Wesley Schwengle <waterkip@cpan.org>
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             This software is Copyright (c) 2019 by Wesley Schwengle.
116              
117             This is free software, licensed under:
118              
119             The (three-clause) BSD License
120              
121             =cut