blib/lib/WWW/PiHole.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 totaltotal 0.0% 0 / 99 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # ABSTRACT: Perl interface to Pi-hole
2  
3 use v5.37.9;
4 use experimental qw( class builtin try );
5  
6 package WWW::PiHole;
7  
8 class WWW::PiHole {
9  
10 use URI;
11 use HTTP::Tiny;
12 use JSON::PP;
13 use Syntax::Operator::In;
14 use Term::ANSIColor;
15  
16 # @formatter:off
17  
18 field $auth :param = undef;
19  
20 # @formatter:on
21  
22 my $uri = URI -> new( 'http://pi.hole/admin/api.php' );
23 my $http = HTTP::Tiny -> new;
24 my $json = JSON::PP -> new;
25  
26 method _content ( ) {
27 $http -> get( $uri ) -> {content};
28 }
29  
30 method _content_json ( ) {
31 $json -> decode( $http -> get( $uri ) -> {content} ); # 'content' is HTTP response body
32 }
33  
34 method _status ( $uri ) {
35 $self -> _content_json -> {status};
36 }
37  
38 method _list ( $uri ) {
39 my $json_body = $self -> _content_json;
40 if ( $json_body -> {success} ) { # JSON::PP::Boolean
41 $json_body -> {message}; # {"success":true,"message":null}
42 }
43 }
44  
45 method version ( $mode = 'current' ) {
46 # Modes: 'update', 'current', 'latest', 'branch'
47  
48 #@formatter:off
49  
50 die colored ['bright_red', 'bold'], 'Bad mode'
51 unless $mode in : eq ( 'update' , 'current' , 'latest' , 'branch' );
52  
53 #@formatter :on
54  
55 $uri -> query_param( versions => undef );
56  
57 my $hash = $json -> decode( $http -> get( $uri ) -> {content} );
58  
59 sprintf "Core: %s, Web: %s, FTL: %s\n" ,
60 $hash -> {join '_' , 'core' , $mode} ,
61 $hash -> {join '_' , 'core' , $mode} ,
62 $hash -> {join '_' , 'core' , $mode} ,
63 }
64  
65  
66 method enable ( ) {
67 $uri -> query_param( auth => $auth );
68 $uri -> query_param( enable => undef );
69 $self -> _status( $uri );
70 }
71  
72  
73 method disable ( ) {
74 $uri -> query_param( auth => $auth );
75 $uri -> query_param( disable => undef );
76 $self -> _status( $uri );
77 }
78  
79  
80 method status ( ) {
81 $uri -> query_param( status => undef );
82 $self -> _status( $uri );
83 }
84  
85  
86 method add ( $domain , $list = 'black' ) {
87 $uri -> query_param( auth => $auth );
88 $uri -> query_param( list => $list );
89 $uri -> query_param( add => $domain );
90 $self -> _list( $uri );
91 }
92  
93  
94 method remove ( $domain , $list = 'black' ) {
95 $uri -> query_param( auth => $auth );
96 $uri -> query_param( list => $list );
97 $uri -> query_param( sub => $domain );
98 $self -> _list( $uri );
99 }
100  
101  
102 method recent ( ) {
103 $uri -> query_param( recentBlocked => undef );
104 $self -> content; # domain name
105 }
106  
107  
108 method add_dns ( $domain , $ip ) {
109  
110 $uri -> query_param( auth => $auth );
111 $uri -> query_param( customdns => undef );
112 $uri -> query_param( action => 'add' );
113 $uri -> query_param( domain => $domain );
114 $uri -> query_param( ip => $ip );
115  
116 $self -> _content; # domain name
117  
118 # https://github.com/pi-hole/AdminLTE/blob/b29a423b9553654f113bcdc8a82296eb6e4613d7/scripts/pi-hole/php/func.php#L223
119  
120 }
121  
122  
123 method remove_dns ( $domain , $ip ) {
124  
125 # Command: pihole -a removecustomdns
126  
127 $uri -> query_param( auth => $auth );
128 $uri -> query_param( customdns => undef );
129 $uri -> query_param( action => 'delete' );
130 $uri -> query_param( domain => $domain );
131 $uri -> query_param( ip => $ip );
132  
133 $self -> _content; # domain name
134  
135 }
136  
137  
138
139 method get_dns ()
140 {
141 $uri -> query_param( auth => $auth );
142 $uri -> query_param( customdns => undef );
143 $uri -> query_param( action => 'get' );
144  
145 $self -> _content_json -> {data};
146 }
147  
148 method add_cname ( $domain , $target ) {
149  
150 $uri -> query_param( auth => $auth );
151 $uri -> query_param( customcname => undef );
152 $uri -> query_param( action => 'add' );
153 $uri -> query_param( domain => $domain );
154 $uri -> query_param( target => $target );
155  
156 $self -> _content; # domain name
157  
158 }
159  
160  
161 method remove_cname ( $domain , $target ) {
162  
163 $uri -> query_param( auth => $auth );
164 $uri -> query_param( customcname => undef );
165 $uri -> query_param( action => 'delete' );
166 $uri -> query_param( domain => $domain );
167 $uri -> query_param( target => $target );
168  
169 $self -> _content; # domain name
170  
171 }
172  
173  
174  
175 method get_cname ()
176 {
177 $uri -> query_param( auth => $auth );
178 $uri -> query_param( customcname => undef );
179 $uri -> query_param( action => 'get' );
180  
181 $self -> _content_json -> {data};
182 }
183  
184 }
185  
186 # https://github.com/pi-hole/AdminLTE/blob/master/api.php
187 # https://github.com/pi-hole/AdminLTE/blob/master/scripts/pi-hole/php/func.php
188  
189 __END__