blib/lib/BrandMeister/API.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 134 CC 11 SCAR 22.3
CC
11
Cov
0%
_send_request
20.0
_action
20.0
_build_request
12.0
CRAP 9.3
1 use strict;
2  
3 package BrandMeister::API;
4  
5 use LWP::UserAgent;
6 use HTTP::Request::Common;
7 use JSON;
8 use MIME::Base64;
9 #use LWP::ConsoleLogger::Everywhere ();
10 #use Data::Dumper;
11  
12 =head1 NAME
13  
14 BM::API - Use the BM API from Perl
15  
16 =head1 SYNOPSIS
17  
18 Implementation of the BM API in Perl.
19  
20 =head1 AUTHOR
21  
22 Simon (G7RZU) <simon@gb7fr.org.uk>
23  
24 =cut
25  
26 use vars qw($VERSION);
27 #Define version
28 $VERSION = '0.3';
29  
30 =head1 METHODS
31  
32 =head2 new
33
34 my($bmobj) = BrandMeister::API->new({
35 BM_APIKEY => '<api key>',
36 DMRID => '<id>',
37 });
38  
39 =cut
40  
41 sub new {
42 my($class) = shift;
43 my($self) = shift;
44 if (!exists($self->{BM_APIKEY}) || !exists($self->{DMRID})) {
45 return(0);
46 }
47 $self->{BM_APIBASEURL} = "https://api.brandmeister.network/v1.0/repeater/";
48 $self->{BM_APIKEYBASE64} = encode_base64($self->{BM_APIKEY}.':');
49 bless($self,$class);
50 return($self);
51 };
52  
53 sub _build_request {
54 my($self) = shift;
55 #my($json) = shift;
56 my($requrlpart) = shift;
57 my($formdataref) = shift;
58 my($uri) = $self->{BM_APIBASEURL}.$requrlpart;
59 print("Building HTTP request\n") if($self->{DEBUG});
60 my($req);
61 if ($formdataref) {
62 $req = HTTP::Request::Common::POST( $uri,$formdataref);
63 } else {
64 $req = HTTP::Request::Common::POST( $uri);
65 };
66 $req->header( 'Content-Type' => 'application/x-www-form-urlencoded',
67 'Authorization'=>'Basic ' . $self->{BM_APIKEYBASE64}
68 );
69
70 #$req->content( $json );
71 return($req);
72
73 };
74  
75 sub _send_request {
76 my($self) = shift;
77 my($req) = shift;
78 return(1) if (!$req);
79 my($ua) = LWP::UserAgent->new;
80 my($jsonresobj) = JSON->new;
81 my($res) = $ua->request($req);
82 print('Request status line: '.$res->status_line."\n") if($self->{DEBUG}) ;
83 if (!$res->is_success) {
84 return($res->status_line);
85 };
86 $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content);
87 return(0);
88 };
89
90  
91 =head2 json_response
92  
93 Returns a hash ref to the last JSON response or undef if there is none
94
95 $jsonhashref = $jsonobj->json_response;
96  
97 =cut
98  
99 sub json_response {
100 my($self) = shift;
101 print("Return JSON response\n") if($self->{DEBUG});
102 return($self->{_JSONRESPONSEREF});
103 };
104  
105  
106 sub _do_action {
107 my($self) = shift;
108 my($requrlpart) = shift;
109 my($formdataref) = shift;
110 my($req) = $self->_build_request($requrlpart,$formdataref);
111 my($res) = $self->_send_request($req);
112 return($res);
113
114 };
115  
116 sub _action {
117 my($self) = shift;
118 my($action) = shift;
119 my($reqaction);
120 my($formdataref);
121 my($ts,$tg) = @_;
122 if ($action eq 'delstatic') {
123 $reqaction = 'talkgroup/?action=DEL&id='.$self->{DMRID};
124 $formdataref = [
125 talkgroup => $tg,
126 timeslot => $ts
127 ];
128 } elsif ($action eq 'addstatic') {
129 $reqaction = 'talkgroup/?action=ADD&id='.$self->{DMRID};
130 $formdataref = [
131 talkgroup => $tg,
132 timeslot => $ts
133 ];
134 } elsif ($action eq 'dropdynamic') {
135 $reqaction = 'setRepeaterTarantool.php?action=dropDynamicGroups&slot='.$ts.'&q='.$self->{DMRID};
136 } else {
137 return(1);
138 };
139 $self->{LASTACTIONRES} = $self->_do_action($reqaction,$formdataref);
140 return(0);
141  
142 };
143  
144 sub result {
145 my($self) = shift;
146 return($self->{LASTACTIONRES});
147 };
148  
149 =head2 add_static_tg
150
151 Add static TG to repeater config.
152  
153 $res = $bmobj->add_static_tg(<timeslot>,<talkgroup>)
154
155 =cut
156  
157 sub add_static_tg {
158 my($self) = shift;
159 my($ts,$tg) = @_;
160 #return(1) if (($ts < 1 || $ts > 2) || !$tg || $tg == 9 || $tg == 8 || $tg == 6);
161 return($self->_action('addstatic',$ts,$tg));
162 };
163  
164 =head2 del_static_tg
165
166 Delete static TG from repeater config.
167  
168 $res = $bmobj->del_static_tg(<timeslot>,<talkgroup>)
169
170 =cut
171  
172 sub del_static_tg {
173 my($self) = shift;
174 my($ts,$tg) = @_;
175 #return(1) if (($ts < 1 || $ts > 2) || !$tg || $tg == 9 || $tg == 8 || $tg == 6);
176 return($self->_action('delstatic',$ts,$tg));
177 };
178  
179 =head2 dropdynamic
180
181 Drop dynamic groups from timeslot
182
183 $bmobj->dropdynamic(<timeslot>);
184
185 =cut
186  
187 sub dropdynamic {
188 my($self) = shift;
189 my($ts) = shift;
190 return($self->_action('dropdynamic',$ts));
191 };
192  
193 1;