slightly more testing/debugging. basically working
[Net-GlobalPOPs-MediaServicesAPI.git] / lib / Net / GlobalPOPs / MediaServicesAPI.pm
1 package Net::GlobalPOPs::MediaServicesAPI;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use XML::Simple;
7 use XML::Writer;
8 use Net::HTTPS::Any qw(https_post);
9
10 =head1 NAME
11
12 Net::GlobalPOPs::MediaServicesAPI - Interface to GlobalPOPs Media Services API
13
14 =head1 VERSION
15
16 Version 0.01
17
18 =cut
19
20 our $VERSION = '0.01';
21 our $URL     = 'https://www.loginto.us/VOIP/api.pl';
22 #could be parsed from URL, if it mattered...
23 our $HOST    = 'www.loginto.us';
24 our $PATH    = '/VOIP/api.pl';
25
26 our $AUTOLOAD;
27 our $errstr = '';
28
29 =head1 SYNOPSIS
30
31     use Net::GlobalPOPs::MediaServicesAPI;
32
33     my $handle = Net::GlobalPOPs::MediaServicesAPI->new(
34         'login'    => 'tofu',
35         'password' => 'beast',
36         'debug'    => 1,
37     );
38
39     #DID functions
40     #auditDIDs
41     #queryDID
42     #reserveDID
43     #assignDID
44     #configDID
45     #releaseDID
46
47     #911 Functions
48
49     #Locator Functions
50
51     ...
52
53 =head1 METHODS
54
55 =head2 new HASHREF | OPTION, VALUE ...
56
57 Creates a new Net::GlobalPOPs::MediaServicesAPI object.  Options may be passed
58 as a hash reference or a flat list of names and values.
59
60 =over 4
61
62 =item login (required)
63
64 =item password (required)
65
66 =item login (required)
67
68 =back
69
70 =cut
71
72 #  If there is an error,
73 #returns false and sets an error string which may be queried with the I<errstr>
74 #class method.
75
76 sub new {
77   my $proto = shift;
78   my $class = ref($proto) || $proto;
79   my $self = ref($_[0]) ? shift : { @_ };
80   bless($self, $class);
81 }
82
83 =head2 errstr
84
85 =cut
86
87 sub errstr {
88   my $class = shift;
89
90   return $errstr
91     unless ref($class) && $class->isa('Net::GlobalPOPs::MediaServicesAPI');
92
93   my $self = $class;
94   $self->{errstr};
95 }
96
97 sub DESTROY { }; # no-op
98
99 sub AUTOLOAD {
100   my $self = shift;
101   my $opts = ref($_[0]) ? shift : { @_ };
102
103   $AUTOLOAD =~ /(^|::)(\w+)$/ or die "unparsable AUTOLOAD: $AUTOLOAD";
104   my $function = $2;
105
106   my $output;
107   my $w = new XML::Writer(OUTPUT => \$output, DATA_MODE => 1, DATA_INDENT => 3);
108
109   $w->xmlDecl('ISO-8859-1');
110   $w->doctype('request', undef, $URL);
111
112   $w->startTag('request', 'id' => '');  #XXX request ID???
113     $w->startTag('header');
114       $w->startTag('sender');
115
116         if ( $self->{'sessionid'} ) {
117
118           #$w->dataElement( 'login'     => ''                   );
119           #$w->dataElement( 'password'  => ''                   );
120           $w->dataElement( 'sessionid' => $self->{'sessionid'} );
121
122         } else {
123
124           $w->dataElement( 'login'     => $self->{'login'}     );
125           $w->dataElement( 'password'  => $self->{'password'}  );
126           #$w->dataElement( 'sessionid' => ''                   );
127
128         }
129
130       $w->endTag('sender');
131     $w->endTag('header');
132
133     $w->startTag('body');
134
135       $w->dataElement( 'requesttype' => $function );
136
137       foreach my $opt ( keys %$opts ) {
138
139         $w->dataElement( $opt => $opts->{$opt} );
140
141       }
142
143     $w->endTag('body');
144
145   $w->endTag('request');
146
147   #$output =~ s/\n+/\n/g;
148
149   warn "XML Request for $function function:\n$output"
150     if $self->{'debug'};
151
152   my( $page, $response, %reply_headers ) = https_post(
153     'host'         => $HOST,
154     'path'         => $PATH,
155     'args'         => { 'apidata' => $output, },
156     #'content'      => $output,
157     #'Content-Type' => 'text/plain',
158     #'Content-Type' => 'text/xml',
159     #'Content-Type' => 'application/xml',
160     #'headers' => {},
161     'debug'        => $self->{'debug'},
162   );
163
164   unless ( $response =~ /^HTTP\/[\d\.]+\s+200/i ) {
165     $self->{'errstr'} = $response;
166     return '';
167   }
168
169   warn "XML Response for $function function:\n: $page"
170     if $self->{'debug'};
171
172   my $hashref = XMLin( $page );
173
174   warn "Parsed response for $function funtion:\n". Dumper($hashref)
175     if $self->{'debug'};
176
177   my $return = $hashref->{'body'};
178
179   warn "Returning data:\n". Dumper($return)
180     if $self->{'debug'};
181
182   $return;
183
184 }
185
186 =head1 AUTHOR
187
188 Ivan Kohler, C<< <ivan-net-globalpops at freeside.biz> >>
189
190 =head1 BUGS
191
192 Please report any bugs or feature requests to C<bug-net-globalpops-mediaservicesapi at rt.cpan.org>, or through
193 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-GlobalPOPs-MediaServicesAPI>.  I will be notified, and then you'll
194 automatically be notified of progress on your bug as I make changes.
195
196
197
198
199 =head1 SUPPORT
200
201 You can find documentation for this module with the perldoc command.
202
203     perldoc Net::GlobalPOPs::MediaServicesAPI
204
205
206 You can also look for information at:
207
208 =over 4
209
210 =item * RT: CPAN's request tracker
211
212 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-GlobalPOPs-MediaServicesAPI>
213
214 =item * AnnoCPAN: Annotated CPAN documentation
215
216 L<http://annocpan.org/dist/Net-GlobalPOPs-MediaServicesAPI>
217
218 =item * CPAN Ratings
219
220 L<http://cpanratings.perl.org/d/Net-GlobalPOPs-MediaServicesAPI>
221
222 =item * Search CPAN
223
224 L<http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI>
225
226 =back
227
228
229 =head1 ACKNOWLEDGEMENTS
230
231
232 =head1 COPYRIGHT & LICENSE
233
234 Copyright 2008 Freeside Internet Services, Inc. (http://freeside.biz/)
235
236 This program is free software; you can redistribute it and/or modify it
237 under the same terms as Perl itself.
238
239
240 =head1 ADVERTISEMENT
241
242 Open-source billing, trouble ticketing and automation for VoIP providers:
243
244 http://freeside.biz/freeside/
245
246 =cut
247
248 1; # End of Net::GlobalPOPs::MediaServicesAPI