| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of DNS-NIOS | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2021 by Christian Segundo. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | ## no critic | ||||||||||||||||||||||||||||
| 11 | package DNS::NIOS; | ||||||||||||||||||||||||||||
| 12 | 5 | $DNS::NIOS::VERSION = '0.003'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # ABSTRACT: Perl binding for NIOS | ||||||||||||||||||||||||||||
| 15 | # VERSION | ||||||||||||||||||||||||||||
| 16 | # AUTHORITY | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ## use critic | ||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use JSON qw(to_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use DNS::NIOS::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use Role::Tiny::With; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | use Class::Tiny qw( password username wapi_addr traits ), | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | wapi_version => 'v2.7', | ||||||||||||||||||||||||||||
| 33 | scheme => 'https', | ||||||||||||||||||||||||||||
| 34 | insecure => 0, | ||||||||||||||||||||||||||||
| 35 | timeout => 10, | ||||||||||||||||||||||||||||
| 36 | debug => $ENV{NIOS_DEBUG} | ||||||||||||||||||||||||||||
| 37 | 5 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub BUILD { | ||||||||||||||||||||||||||||
| 40 | 7 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | defined( $self->$_ ) | ||||||||||||||||||||||||||||
| 43 | or croak("$_ is required!") | ||||||||||||||||||||||||||||
| 44 | 7 | ▶ | for qw(username password wapi_addr); ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 6 | ▶ | ( ( $self->scheme eq 'http' ) or ( $self->scheme eq 'https' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->scheme eq "http" or $self->scheme eq "https"
MC/DC100%2/2
$self->scheme eq "http" or $self->scheme eq "https"
$self->scheme eq "http"
$self->scheme eq "https"
Truth table100%3/3
A or B
A= $self->scheme eq "http"B= $self->scheme eq "https"
| |||||||||||||||||||||||||||||
| 47 | or croak( "scheme not supported: " . $self->scheme ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $self->{base_url} = | ||||||||||||||||||||||||||||
| 50 | 5 | $self->scheme . "://" | |||||||||||||||||||||||||||
| 51 | . $self->wapi_addr | ||||||||||||||||||||||||||||
| 52 | . "/wapi/" | ||||||||||||||||||||||||||||
| 53 | . $self->wapi_version . "/"; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 5 | $self->{ua} = LWP::UserAgent->new( timeout => $self->timeout ); | |||||||||||||||||||||||||||
| 56 | 5 | $self->{ua}->agent( 'NIOS-perl/' . $DNS::NIOS::VERSION ); | |||||||||||||||||||||||||||
| 57 | 5 | ▶ | $self->{ua}->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->insecure and $self->scheme eq "https"
MC/DC0%0/2
$self->insecure and $self->scheme eq "https"
$self->insecure
$self->scheme eq "https"
Truth table33%1/3
A and B
A= $self->insecureB= $self->scheme eq "https"
| |||||||||||||||||||||||||||||
| 58 | if $self->insecure and $self->scheme eq 'https'; ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||||||||||||||||||||
| 59 | 5 | $self->{ua}->default_header( 'Accept' => 'application/json' ); | |||||||||||||||||||||||||||
| 60 | 5 | $self->{ua}->default_header( 'Content-Type' => 'application/json' ); | |||||||||||||||||||||||||||
| 61 | 5 | $self->{ua}->default_header( 'Authorization' => 'Basic ' | |||||||||||||||||||||||||||
| 62 | . encode_base64( $self->username . ":" . $self->password ) ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 5 | ▶ | if ( $self->traits ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 2 | foreach ( @{ $self->traits } ) { | |||||||||||||||||||||||||||
| 66 | 3 | with $_; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub create { | ||||||||||||||||||||||||||||
| 72 | 2 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | defined( $args{$_} ) | ||||||||||||||||||||||||||||
| 75 | or croak("$_ is required!") | ||||||||||||||||||||||||||||
| 76 | 2 | ▶ | for qw(path payload); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | return $self->__request( 'POST', $args{path}, | ||||||||||||||||||||||||||||
| 79 | 2 | ( payload => $args{payload}, params => $args{params} ) ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub update { | ||||||||||||||||||||||||||||
| 83 | 1 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | defined( $args{$_} ) | ||||||||||||||||||||||||||||
| 86 | or croak("$_ is required!") | ||||||||||||||||||||||||||||
| 87 | 1 | ▶ | for qw(path payload); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | return $self->__request( 'PUT', $args{path}, | ||||||||||||||||||||||||||||
| 90 | 1 | ( payload => $args{payload}, params => $args{params} ) ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub get { | ||||||||||||||||||||||||||||
| 94 | 1207 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | defined( $args{path} ) | ||||||||||||||||||||||||||||
| 97 | 1207 | ▶ | or croak("path is required!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1207 | return $self->__request( 'GET', $args{path}, ( params => $args{params} ) ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub delete { | ||||||||||||||||||||||||||||
| 103 | 1 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | defined( $args{path} ) | ||||||||||||||||||||||||||||
| 106 | 1 | ▶ | or croak("path is required!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | return $self->__request( 'DELETE', $args{path}, ( params => $args{params} ) ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub __request { | ||||||||||||||||||||||||||||
| 112 | 1211 | ▶ | my ( $self, $op, $path, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__request: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1211 | my $payload = delete $args{payload}; | |||||||||||||||||||||||||||
| 115 | 1211 | my $params = delete $args{params}; | |||||||||||||||||||||||||||
| 116 | 1211 | my $query_params = q{}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1211 | ▶ | grep( /(^\Q$op\E$)/, qw(GET POST PUT DELETE) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | or die("invalid operation: $op"); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1211 | ▶ | croak("invalid path") unless ( defined $path and length $path ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $path and length $path
MC/DC0%0/2
defined $path and length $path
defined $path
length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1211 | ▶ | if ( $op eq 'PUT' or $op eq 'POST' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op eq "PUT" or $op eq "POST"
MC/DC100%2/2
$op eq "PUT" or $op eq "POST"
$op eq "PUT"
$op eq "POST"
Truth table100%3/3
A or B
A= $op eq "PUT"B= $op eq "POST"
| |||||||||||||||||||||||||||||
| 124 | 3 | ▶ | croak("invalid payload") unless keys %{$payload}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1211 | ▶ | if ( defined $params ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1205 | my $u = URI->new( q{}, 'http' ); | |||||||||||||||||||||||||||
| 129 | 1205 | $query_params = q{?}; | |||||||||||||||||||||||||||
| 130 | 1205 | foreach ( keys %{$params} ) { | |||||||||||||||||||||||||||
| 131 | 4811 | $u->query_param( $_ => $params->{$_} ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 1205 | $query_params .= $u->query; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | my $request = | ||||||||||||||||||||||||||||
| 137 | 1211 | HTTP::Request->new( $op, $self->{base_url} . $path . $query_params ); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1211 | ▶ | if ( $op eq 'PUT' or $op eq 'POST' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op eq "PUT" or $op eq "POST"
MC/DC100%2/2
$op eq "PUT" or $op eq "POST"
$op eq "PUT"
$op eq "POST"
Truth table100%3/3
A or B
A= $op eq "PUT"B= $op eq "POST"
| |||||||||||||||||||||||||||||
| 140 | 3 | $request->content( to_json($payload) ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | return DNS::NIOS::Response->new( | ||||||||||||||||||||||||||||
| 144 | 1211 | _http_response => $self->{ua}->request($request) ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 5 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||