File Coverage

blib/lib/WebService/KvKAPI/Roles/OpenAPI.pm
Criterion Covered Total %
statement 32 34 94.1
branch 8 12 66.6
condition n/a
subroutine 16 18 88.8
pod 8 10 80.0
total 64 74 86.4


line stmt bran cond sub pod time code
1 4     4   33 use utf8;
  4         9  
  4         24  
2             package WebService::KvKAPI::Roles::OpenAPI;
3             our $VERSION = '0.104';
4             # ABSTRACT: WebService::KvkAPI::Roles::OpenAPI package needs a propper abstract
5              
6 4     4   255 use v5.26;
  4         29  
7 4     4   25 use Object::Pad;
  4         9  
  4         25  
8              
9             role WebService::KvKAPI::Roles::OpenAPI;
10 4     4   1645 use Carp qw(croak);
  4         42  
  4         267  
11 4     4   2036 use OpenAPI::Client;
  4         2081097  
  4         46  
12 4     4   2640 use Try::Tiny;
  4         5953  
  4         6024  
13              
14             field $api_host :accessor :param = undef;
15 1 50   1 1 7 field $api_key :param = undef;
  1     1 1 9  
16             field $spoof :param = 0;
17 3 50   3 0 77 field $client :accessor;
  3     3 1 14  
18              
19             ADJUST {
20              
21             my $base_uri = 'https://api.kvk.nl/api';
22              
23             if ($spoof) {
24             $base_uri = 'https://api.kvk.nl/test/api';
25             # publicly known API key
26             $api_key = 'l7xx1f2691f2520d487b902f4e0b57a0b197';
27             }
28              
29             # work around api-key not being present with the api_call method
30             if (!defined $api_key) {
31             croak("Please supply an API-key with construction");
32             }
33              
34             my $definition = sprintf('data://%s/kvkapi.yml', ref $self);
35             $client = OpenAPI::Client->new($definition, base_url => $base_uri);
36             if ($self->has_api_host) {
37             $client->base_url->host($self->api_host);
38             }
39              
40             $client->ua->on(start => sub ($ua, $tx) {
41             $tx->req->headers->header('apikey' => $api_key);
42             });
43             }
44              
45 0     0 1 0 method is_spoof {
        0 1    
46 0 0       0 return $spoof ? 1 : 0;
47             }
48              
49 7     7 0 47 method has_api_host {
        7 1    
50 7 100       45 return defined $api_host ? 1 : 0;
51             }
52              
53 4     4 1 11 method api_call {
        4 1    
54 4         13 my ($operation, %query) = @_;
55              
56             my $tx = try {
57 4     4   463 $client->call(
58             $operation => \%query,
59             );
60             }
61             catch {
62 1     1   30 die("Died calling KvK API with operation '$operation': $_", $/);
63 4         33 };
64              
65 3 100       159 if ($tx->error) {
66             # Not found, no result
67 2 100       52 return if $tx->res->code == 404;
68              
69             # Any other error
70             croak(
71             sprintf(
72             "Error calling KvK API with operation '%s': '%s' (%s)",
73             $operation, $tx->result->body, $tx->error->{message}
74 1         71 ),
75             );
76             }
77              
78 1         81 return $tx->res->json;
79             }
80              
81             1;
82              
83             __END__