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