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 10 10 100.0
total 66 74 89.1


line stmt bran cond sub pod time code
1 4     4   26 use utf8;
  4         7  
  4         21  
2             package WebService::KvKAPI::Roles::OpenAPI;
3             our $VERSION = '0.103';
4             # ABSTRACT: WebService::KvkAPI::Roles::OpenAPI package needs a propper abstract
5              
6 4     4   218 use v5.26;
  4         12  
7 4     4   30 use Object::Pad;
  4         8  
  4         19  
8              
9             role WebService::KvKAPI::Roles::OpenAPI;
10 4     4   1430 use Carp qw(croak);
  4         8  
  4         246  
11 4     4   1789 use OpenAPI::Client;
  4         1827898  
  4         43  
12 4     4   2381 use Try::Tiny;
  4         5122  
  4         4285  
13              
14 1 50   1 1 7 has $api_host :accessor :param = undef;
  1     1 1 6  
15             has $api_key :param = undef;
16             has $spoof :param = 0;
17 3 50   3 1 69 has $client :accessor;
  3     3 1 11  
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 1 39 method has_api_host {
        7 1    
50 7 100       33 return defined $api_host ? 1 : 0;
51             }
52              
53 4     4 1 9 method api_call {
        4 1    
54 4         11 my ($operation, %query) = @_;
55              
56             my $tx = try {
57 4     4   371 $client->call(
58             $operation => \%query,
59             );
60             }
61             catch {
62 1     1   28 die("Died calling KvK API with operation '$operation': $_", $/);
63 4         23 };
64              
65 3 100       169 if ($tx->error) {
66             # Not found, no result
67 2 100       42 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         59 ),
75             );
76             }
77              
78 1         49 return $tx->res->json;
79             }
80              
81             1;
82              
83             __END__