blib/lib/WWW/PiHole.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 totaltotal 0.0% 0 / 74 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  
14 # @formatter:off
15  
16 field $auth :param = undef;
17  
18 # @formatter:on
19  
20 my $uri = URI -> new( 'http://pi.hole/admin/api.php' );
21 my $http = HTTP::Tiny -> new;
22 my $json = JSON::PP -> new;
23  
24 method _status ( $uri ) {
25 $json -> decode( $http -> get( $uri ) -> {content} ) -> {status};
26 }
27  
28 method _list ( $uri ) {
29 my $hash = $json -> decode( $http -> get( $uri ) -> {content} );
30 if ( $hash -> {success} ) { # JSON::PP::Boolean
31 $hash -> {message}; # {"success":true,"message":null}
32 }
33 }
34  
35 method enable ( ) {
36 $uri -> query_param( auth => $auth );
37 $uri -> query_param( enable => undef );
38 $self -> _status( $uri );
39 }
40  
41  
42 method disable ( ) {
43 $uri -> query_param( auth => $auth );
44 $uri -> query_param( disable => undef );
45 $self -> _status( $uri );
46 }
47  
48  
49 method status ( ) {
50 $uri -> query_param( status => undef );
51 $self -> _status( $uri );
52 }
53  
54  
55 method add ( $domain , $list = 'black' ) {
56 $uri -> query_param( auth => $auth );
57 $uri -> query_param( list => $list );
58 $uri -> query_param( add => $domain );
59 $self -> _list( $uri );
60 }
61  
62  
63 method remove ( $domain , $list = 'black' ) {
64 $uri -> query_param( auth => $auth );
65 $uri -> query_param( list => $list );
66 $uri -> query_param( sub => $domain );
67 $self -> _list( $uri );
68 }
69  
70  
71 method recent ( ) {
72 $uri -> query_param( recentBlocked => undef );
73 $http -> get( $uri ) -> {content}; # domain name
74 }
75  
76  
77  
78 method add_dns ( $domain , $ip ) {
79  
80 $uri -> query_param( auth => $auth );
81 $uri -> query_param( customdns => undef );
82 $uri -> query_param( action => 'add' );
83 $uri -> query_param( domain => $domain );
84 $uri -> query_param( ip => $ip );
85  
86 $http -> get( $uri ) -> {content}; # domain name
87  
88 # https://github.com/pi-hole/AdminLTE/blob/b29a423b9553654f113bcdc8a82296eb6e4613d7/scripts/pi-hole/php/func.php#L223
89  
90 }
91  
92  
93 method remove_dns ( $domain , $ip ) {
94  
95 # Command: pihole -a removecustomdns
96  
97 $uri -> query_param( auth => $auth );
98 $uri -> query_param( customdns => undef );
99 $uri -> query_param( action => 'delete' );
100 $uri -> query_param( domain => $domain );
101 $uri -> query_param( ip => $ip );
102  
103 $http -> get( $uri ) -> {content}; # domain name
104  
105 }
106  
107 method add_cname ( $domain , $target ) {
108  
109 $uri -> query_param( auth => $auth );
110 $uri -> query_param( customcname => undef );
111 $uri -> query_param( action => 'add' );
112 $uri -> query_param( domain => $domain );
113 $uri -> query_param( target => $target );
114  
115 $http -> get( $uri ) -> {content}; # domain name
116  
117 }
118  
119  
120  
121 method remove_cname ( $domain , $target ) {
122  
123 $uri -> query_param( auth => $auth );
124 $uri -> query_param( customcname => undef );
125 $uri -> query_param( action => 'delete' );
126 $uri -> query_param( domain => $domain );
127 $uri -> query_param( target => $target );
128  
129 $http -> get( $uri ) -> {content}; # domain name
130  
131 }
132  
133 }
134  
135 # https://github.com/pi-hole/AdminLTE/blob/master/api.php
136  
137 __END__