line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
$DNS::NIOS::VERSION = '0.005'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# ABSTRACT: Perl binding for NIOS |
15
|
|
|
|
|
|
|
# VERSION |
16
|
|
|
|
|
|
|
# AUTHORITY |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
## use critic |
19
|
4
|
|
|
4
|
|
12235
|
use strictures 2; |
|
4
|
|
|
|
|
54
|
|
|
4
|
|
|
|
|
148
|
|
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
4
|
|
714
|
use Carp qw(croak); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
379
|
|
22
|
4
|
|
|
4
|
|
2684
|
use JSON qw(to_json); |
|
4
|
|
|
|
|
54112
|
|
|
4
|
|
|
|
|
24
|
|
23
|
4
|
|
|
4
|
|
7875
|
use LWP::UserAgent; |
|
4
|
|
|
|
|
191620
|
|
|
4
|
|
|
|
|
218
|
|
24
|
4
|
|
|
4
|
|
2396
|
use MIME::Base64 qw(encode_base64); |
|
4
|
|
|
|
|
2687
|
|
|
4
|
|
|
|
|
296
|
|
25
|
4
|
|
|
4
|
|
31
|
use URI; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
123
|
|
26
|
4
|
|
|
4
|
|
1932
|
use URI::QueryParam; |
|
4
|
|
|
|
|
3389
|
|
|
4
|
|
|
|
|
138
|
|
27
|
4
|
|
|
4
|
|
1911
|
use DNS::NIOS::Response; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
157
|
|
28
|
4
|
|
|
4
|
|
2057
|
use Role::Tiny::With; |
|
4
|
|
|
|
|
21437
|
|
|
4
|
|
|
|
|
342
|
|
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
|
4
|
|
|
4
|
|
35
|
}; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
69
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub BUILD { |
40
|
7
|
|
|
7
|
0
|
15418180
|
my ( $self, $args ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
defined( $self->$_ ) |
43
|
|
|
|
|
|
|
or croak("$_ is required!") |
44
|
7
|
|
100
|
|
|
264
|
for qw(username password wapi_addr); ## no critic (ControlStructures::ProhibitPostfixControls) |
45
|
|
|
|
|
|
|
|
46
|
6
|
100
|
100
|
|
|
484
|
( ( $self->scheme eq 'http' ) or ( $self->scheme eq 'https' ) ) |
47
|
|
|
|
|
|
|
or croak( "scheme not supported: " . $self->scheme ); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$self->{base_url} = |
50
|
5
|
|
|
|
|
145
|
$self->scheme . "://" |
51
|
|
|
|
|
|
|
. $self->wapi_addr |
52
|
|
|
|
|
|
|
. "/wapi/" |
53
|
|
|
|
|
|
|
. $self->wapi_version . "/"; |
54
|
|
|
|
|
|
|
|
55
|
5
|
|
|
|
|
335
|
$self->{ua} = LWP::UserAgent->new( timeout => $self->timeout ); |
56
|
5
|
|
|
|
|
4330
|
$self->{ua}->agent( 'NIOS-perl/' . $DNS::NIOS::VERSION ); |
57
|
5
|
50
|
33
|
|
|
426
|
$self->{ua}->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00 ) |
58
|
|
|
|
|
|
|
if $self->insecure and $self->scheme eq 'https'; ## no critic (ControlStructures::ProhibitPostfixControls) |
59
|
5
|
|
|
|
|
73
|
$self->{ua}->default_header( 'Accept' => 'application/json' ); |
60
|
5
|
|
|
|
|
271
|
$self->{ua}->default_header( 'Content-Type' => 'application/json' ); |
61
|
5
|
|
|
|
|
341
|
$self->{ua}->default_header( 'Authorization' => 'Basic ' |
62
|
|
|
|
|
|
|
. encode_base64( $self->username . ":" . $self->password ) ); |
63
|
|
|
|
|
|
|
|
64
|
5
|
100
|
|
|
|
561
|
if ( $self->traits ) { |
65
|
2
|
|
|
|
|
16
|
foreach ( @{ $self->traits } ) { |
|
2
|
|
|
|
|
33
|
|
66
|
3
|
|
|
|
|
612
|
with $_; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub create { |
72
|
2
|
|
|
2
|
1
|
80
|
my ( $self, %args ) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
defined( $args{$_} ) |
75
|
|
|
|
|
|
|
or croak("$_ is required!") |
76
|
2
|
|
33
|
|
|
21
|
for qw(path payload); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return $self->__request( 'POST', $args{path}, |
79
|
2
|
|
|
|
|
14
|
( payload => $args{payload}, params => $args{params} ) ); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub update { |
83
|
1
|
|
|
1
|
1
|
5
|
my ( $self, %args ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
defined( $args{$_} ) |
86
|
|
|
|
|
|
|
or croak("$_ is required!") |
87
|
1
|
|
33
|
|
|
8
|
for qw(path payload); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return $self->__request( 'PUT', $args{path}, |
90
|
1
|
|
|
|
|
7
|
( payload => $args{payload}, params => $args{params} ) ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub get { |
94
|
1207
|
|
|
1207
|
1
|
4594
|
my ( $self, %args ) = @_; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
defined( $args{path} ) |
97
|
1207
|
50
|
|
|
|
3757
|
or croak("path is required!"); |
98
|
|
|
|
|
|
|
|
99
|
1207
|
|
|
|
|
3578
|
return $self->__request( 'GET', $args{path}, ( params => $args{params} ) ); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub delete { |
103
|
1
|
|
|
1
|
1
|
11
|
my ( $self, %args ) = @_; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
defined( $args{path} ) |
106
|
1
|
50
|
|
|
|
8
|
or croak("path is required!"); |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
7
|
return $self->__request( 'DELETE', $args{path}, ( params => $args{params} ) ); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub __request { |
112
|
1211
|
|
|
1211
|
|
3482
|
my ( $self, $op, $path, %args ) = @_; |
113
|
|
|
|
|
|
|
|
114
|
1211
|
|
|
|
|
2215
|
my $payload = delete $args{payload}; |
115
|
1211
|
|
|
|
|
2085
|
my $params = delete $args{params}; |
116
|
1211
|
|
|
|
|
2603
|
my $query_params = q{}; |
117
|
|
|
|
|
|
|
|
118
|
1211
|
50
|
|
|
|
12691
|
grep( /(^\Q$op\E$)/, qw(GET POST PUT DELETE) ) |
119
|
|
|
|
|
|
|
or die("invalid operation: $op"); |
120
|
|
|
|
|
|
|
|
121
|
1211
|
50
|
33
|
|
|
6132
|
croak("invalid path") unless ( defined $path and length $path ); |
122
|
|
|
|
|
|
|
|
123
|
1211
|
100
|
100
|
|
|
4874
|
if ( $op eq 'PUT' or $op eq 'POST' ) { |
124
|
3
|
50
|
|
|
|
6
|
croak("invalid payload") unless keys %{$payload}; |
|
3
|
|
|
|
|
15
|
|
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
1211
|
100
|
|
|
|
2623
|
if ( defined $params ) { |
128
|
1205
|
|
|
|
|
4888
|
my $u = URI->new( q{}, 'http' ); |
129
|
1205
|
|
|
|
|
74717
|
$query_params = q{?}; |
130
|
1205
|
|
|
|
|
1897
|
foreach ( keys %{$params} ) { |
|
1205
|
|
|
|
|
4104
|
|
131
|
4811
|
|
|
|
|
562445
|
$u->query_param( $_ => $params->{$_} ); |
132
|
|
|
|
|
|
|
} |
133
|
1205
|
|
|
|
|
253867
|
$query_params .= $u->query; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $request = |
137
|
1211
|
|
|
|
|
19795
|
HTTP::Request->new( $op, $self->{base_url} . $path . $query_params ); |
138
|
|
|
|
|
|
|
|
139
|
1211
|
100
|
100
|
|
|
156055
|
if ( $op eq 'PUT' or $op eq 'POST' ) { |
140
|
3
|
|
|
|
|
32
|
$request->content( to_json($payload) ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
return DNS::NIOS::Response->new( |
144
|
1211
|
|
|
|
|
5744
|
_http_response => $self->{ua}->request($request) ); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
__END__ |