File Coverage

blib/lib/DNS/NIOS.pm
Criterion Covered Total %
statement 76 76 100.0
branch 16 22 72.7
condition 15 23 65.2
subroutine 16 16 100.0
pod 4 5 80.0
total 127 142 89.4


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.004';
13              
14             # ABSTRACT: Perl binding for NIOS
15             # VERSION
16             # AUTHORITY
17              
18             ## use critic
19 4     4   11809 use strictures 2;
  4         47  
  4         144  
20              
21 4     4   669 use Carp qw(croak);
  4         7  
  4         337  
22 4     4   2631 use JSON qw(to_json);
  4         51432  
  4         22  
23 4     4   7366 use LWP::UserAgent;
  4         185829  
  4         174  
24 4     4   2045 use MIME::Base64 qw(encode_base64);
  4         2673  
  4         242  
25 4     4   28 use URI;
  4         7  
  4         113  
26 4     4   1939 use URI::QueryParam;
  4         3252  
  4         143  
27 4     4   1844 use DNS::NIOS::Response;
  4         10  
  4         128  
28 4     4   1984 use Role::Tiny::With;
  4         21189  
  4         307  
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   29 };
  4         8  
  4         69  
38              
39             sub BUILD {
40 7     7 0 15411908 my ( $self, $args ) = @_;
41              
42             defined( $self->$_ )
43             or croak("$_ is required!")
44 7   100     247 for qw(username password wapi_addr); ## no critic (ControlStructures::ProhibitPostfixControls)
45              
46 6 100 100     581 ( ( $self->scheme eq 'http' ) or ( $self->scheme eq 'https' ) )
47             or croak( "scheme not supported: " . $self->scheme );
48              
49             $self->{base_url} =
50 5         157 $self->scheme . "://"
51             . $self->wapi_addr
52             . "/wapi/"
53             . $self->wapi_version . "/";
54              
55 5         389 $self->{ua} = LWP::UserAgent->new( timeout => $self->timeout );
56 5         4514 $self->{ua}->agent( 'NIOS-perl/' . $DNS::NIOS::VERSION );
57 5 50 33     447 $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         80 $self->{ua}->default_header( 'Accept' => 'application/json' );
60 5         277 $self->{ua}->default_header( 'Content-Type' => 'application/json' );
61 5         343 $self->{ua}->default_header( 'Authorization' => 'Basic '
62             . encode_base64( $self->username . ":" . $self->password ) );
63              
64 5 100       592 if ( $self->traits ) {
65 2         32 foreach ( @{ $self->traits } ) {
  2         34  
66 3         676 with $_;
67             }
68             }
69             }
70              
71             sub create {
72 2     2 1 52 my ( $self, %args ) = @_;
73              
74             defined( $args{$_} )
75             or croak("$_ is required!")
76 2   33     33 for qw(path payload);
77              
78             return $self->__request( 'POST', $args{path},
79 2         23 ( payload => $args{payload}, params => $args{params} ) );
80             }
81              
82             sub update {
83 1     1 1 6 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 5259 my ( $self, %args ) = @_;
95              
96             defined( $args{path} )
97 1207 50       4543 or croak("path is required!");
98              
99 1207         4345 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       7 or croak("path is required!");
107              
108 1         5 return $self->__request( 'DELETE', $args{path}, ( params => $args{params} ) );
109             }
110              
111             sub __request {
112 1211     1211   4270 my ( $self, $op, $path, %args ) = @_;
113              
114 1211         2657 my $payload = delete $args{payload};
115 1211         2358 my $params = delete $args{params};
116 1211         2293 my $query_params = q{};
117              
118 1211 50       13598 grep( /(^\Q$op\E$)/, qw(GET POST PUT DELETE) )
119             or die("invalid operation: $op");
120              
121 1211 50 33     7395 croak("invalid path") unless ( defined $path and length $path );
122              
123 1211 100 100     6322 if ( $op eq 'PUT' or $op eq 'POST' ) {
124 3 50       8 croak("invalid payload") unless keys %{$payload};
  3         14  
125             }
126              
127 1211 100       3322 if ( defined $params ) {
128 1205         6087 my $u = URI->new( q{}, 'http' );
129 1205         88289 $query_params = q{?};
130 1205         1994 foreach ( keys %{$params} ) {
  1205         4321  
131 4811         587949 $u->query_param( $_ => $params->{$_} );
132             }
133 1205         257056 $query_params .= $u->query;
134             }
135              
136             my $request =
137 1211         23202 HTTP::Request->new( $op, $self->{base_url} . $path . $query_params );
138              
139 1211 100 100     176727 if ( $op eq 'PUT' or $op eq 'POST' ) {
140 3         30 $request->content( to_json($payload) );
141             }
142              
143             return DNS::NIOS::Response->new(
144 1211         7064 _http_response => $self->{ua}->request($request) );
145             }
146              
147             1;
148              
149             __END__