| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 4 |  |  | 4 |  | 27 | use utf8; | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 4 |  |  |  |  | 25 |  | 
| 2 |  |  |  |  |  |  | package WebService::KvKAPI::Roles::OpenAPI; | 
| 3 |  |  |  |  |  |  | our $VERSION = '0.105'; | 
| 4 |  |  |  |  |  |  | # ABSTRACT: WebService::KvkAPI::Roles::OpenAPI package needs a propper abstract | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 4 |  |  | 4 |  | 274 | use v5.26; | 
|  | 4 |  |  |  |  | 13 |  | 
| 7 | 4 |  |  | 4 |  | 37 | use Object::Pad; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 26 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | role WebService::KvKAPI::Roles::OpenAPI; | 
| 10 | 4 |  |  | 4 |  | 1858 | use Carp qw(croak); | 
|  | 4 |  |  |  |  | 21 |  | 
|  | 4 |  |  |  |  | 268 |  | 
| 11 | 4 |  |  | 4 |  | 2165 | use OpenAPI::Client; | 
|  | 4 |  |  |  |  | 2134017 |  | 
|  | 4 |  |  |  |  | 45 |  | 
| 12 | 4 |  |  | 4 |  | 2754 | use Try::Tiny; | 
|  | 4 |  |  |  |  | 6177 |  | 
|  | 4 |  |  |  |  | 6271 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | field $api_host :accessor :param = undef; | 
| 15 | 1 | 50 |  | 1 | 1 | 9 | field $api_key  :param = undef; | 
|  | 1 |  |  | 1 | 1 | 11 |  | 
| 16 |  |  |  |  |  |  | field $spoof    :param = 0; | 
| 17 | 3 | 50 |  | 3 | 1 | 78 | field $client   :accessor; | 
|  | 3 |  |  | 3 | 0 | 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 | 1 | 51 | method has_api_host { | 
|  |  |  |  | 7 | 1 |  |  | 
| 50 | 7 | 100 |  |  |  | 46 | return defined $api_host ? 1 : 0; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 4 |  |  | 4 | 1 | 9 | method api_call { | 
|  |  |  |  | 4 | 1 |  |  | 
| 54 | 4 |  |  |  |  | 14 | my ($operation, %query) = @_; | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | my $tx = try { | 
| 57 | 4 |  |  | 4 |  | 568 | $client->call( | 
| 58 |  |  |  |  |  |  | $operation => \%query, | 
| 59 |  |  |  |  |  |  | ); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | catch { | 
| 62 | 1 |  |  | 1 |  | 52 | die("Died calling KvK API with operation '$operation': $_", $/); | 
| 63 | 4 |  |  |  |  | 31 | }; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 3 | 100 |  |  |  | 210 | if ($tx->error) { | 
| 66 |  |  |  |  |  |  | # Not found, no result | 
| 67 | 2 | 100 |  |  |  | 54 | 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 |  |  |  |  | 84 | ), | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 1 |  |  |  |  | 82 | return $tx->res->json; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | 1; | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | __END__ |