| 1 | 3 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | package WebService::Netlify::Roles::OpenAPI; | ||||||||||
| 3 | 3 | our $VERSION = '0.001'; | |||||||||
| 4 | # ABSTRACT: An OpenAPI role | ||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use Object::Pad; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | role WebService::Netlify::Roles::OpenAPI; | ||||||||||
| 10 | 3 | ▶ | use Carp qw(croak cluck); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use OpenAPI::Client; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use Mojo::File qw(path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | field $filename :param = 'openapi.json'; | ||||||||||
| 17 | 0 | ▶ | field $client :accessor; | ||||||||
|
Subroutine
client: not called CC=2 SCAR=11.6
Pod
documented
Branch: 1 branch
| |||||||||||
| 18 | |||||||||||
| 19 | field $api_key :param = undef; | ||||||||||
| 20 | |||||||||||
| 21 | 3 | ADJUST { | |||||||||
| 22 | |||||||||||
| 23 | if (!defined $api_key) { | ||||||||||
| 24 | croak("Please supply an API-key with construction"); | ||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | my $definition = sprintf('data://%s/%s', ref $self, $filename); | ||||||||||
| 28 | $client = OpenAPI::Client->new($definition); | ||||||||||
| 29 | |||||||||||
| 30 | $client->ua->on(start => sub ($ua, $tx) { | ||||||||||
| 31 | $tx->req->headers->header('authorization' => "Bearer $api_key"); | ||||||||||
| 32 | }); | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | $client->ua->transactor->add_generator(deploy_file => sub { | ||||||||||
| 36 | my ($t, $tx, $path) = @_; | ||||||||||
| 37 | |||||||||||
| 38 | my $data = path($path)->slurp; | ||||||||||
| 39 | $tx->req->headers->content_type('application/octet-stream'); | ||||||||||
| 40 | $tx->req->headers->content_length(length $data); | ||||||||||
| 41 | $tx->req->body($data); | ||||||||||
| 42 | |||||||||||
| 43 | return $tx; | ||||||||||
| 44 | }); | ||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 3 | method api_call { | |||||||||
| 48 | my ($operation, $query, %content) = @_; | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | my $tx = try { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 51 | 0 | $client->call( | |||||||||
| 52 | $operation => $query, %content | ||||||||||
| 53 | ); | ||||||||||
| 54 | } | ||||||||||
| 55 | 0 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 56 | 0 | die("Died calling Netlify API with operation '$operation': $_", $/); | |||||||||
| 57 | }; | ||||||||||
| 58 | |||||||||||
| 59 | if ($tx->error) { | ||||||||||
| 60 | # Any other error | ||||||||||
| 61 | croak( | ||||||||||
| 62 | sprintf( | ||||||||||
| 63 | "Error calling Netlify API with operation '%s': '%s' (%s)", | ||||||||||
| 64 | $operation, $tx->result->body, $tx->error->{message} | ||||||||||
| 65 | ), | ||||||||||
| 66 | ); | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | return $tx->res->json; | ||||||||||
| 70 | 3 | } | |||||||||
| 71 | |||||||||||
| 72 | 3 | 1; | |||||||||
| 73 | |||||||||||
| 74 | __END__ | ||||||||||