File Coverage

blib/lib/WWW/PlCloud.pm
Criterion Covered Total %
statement 21 61 34.4
branch 0 16 0.0
condition 0 6 0.0
subroutine 7 13 53.8
pod 0 4 0.0
total 28 100 28.0


line stmt bran cond sub pod time code
1             package WWW::PlCloud;
2              
3 1     1   17542 use 5.006;
  1         4  
  1         39  
4 1     1   8 use strict;
  1         1  
  1         33  
5 1     1   4 use warnings;
  1         9  
  1         45  
6              
7             =head1 NAME
8              
9             WWW::PlCloud - The great new WWW::PlCloud!
10              
11             =head1 VERSION
12              
13             Version 0.01
14              
15             =cut
16              
17             our $VERSION = '0.02';
18              
19 1     1   499 use Moo;
  1         13417  
  1         9  
20              
21 1     1   596024 use LWP::UserAgent;
  1         54351  
  1         47  
22 1     1   868 use JSON qw/to_json from_json/;
  1         10373  
  1         5  
23 1     1   757 use Data::Dumper;
  1         5395  
  1         568  
24              
25             has sessionid => (
26             is => 'rw',
27             );
28              
29             has csrftoken => (
30             is => 'rw',
31             );
32              
33             has ua => (
34             is => 'rw',
35             );
36              
37             has user => (
38             is => 'rw', required => 1,
39             );
40              
41             has pass => (
42             is => 'rw', required => 1,
43             );
44              
45              
46              
47             =head1 SYNOPSIS
48              
49             宝德云 API SDK,
50             只提供了一些基本的接口, 例如登陆.
51             具体的 API 列表参考:
52             http://docs.plcloud.com/api/list.html
53              
54             调用方法:
55              
56             #!/usr/bin/env perl
57              
58             use strict;
59             use warnings;
60             use Data::Dumper;
61             use PlCloud;
62              
63             my $pc = PlCloud->new( user => 'user_name', pass => 'password' );
64             $pc->login();
65              
66             #
67             # 无参数API调用方法
68             $pc->run_api( 'GetUsageLimits' );
69              
70             # 带参数API调用方法
71             $pc->run_api( 'GetUsageLimits', obj_id => 789, port_id => 'x' );
72              
73              
74             目前所有的API返回数据都直接输出至 STDOUT
75              
76              
77             =head1 SUBROUTINES/METHODS
78             =cut
79              
80             sub run_api {
81 0     0 0   my ( $self, $api, @o_args ) = @_;
82 0           my $uri = "https://console.plcloud.com/api/";
83              
84             # Build
85 0 0         my %post = @o_args ? @o_args : ();
86 0           $post{ action } = $api;
87              
88 0           my $req = HTTP::Request->new( 'POST', $uri );
89 0           $req->header( 'Content-Type' => 'application/json' );
90              
91             # 必须加额外的这个垃圾头
92 0           $req->header( 'x-csrftoken' => $self->csrftoken );
93 0           $req->content( to_json( \%post ) );
94              
95 0           my $res = $self->ua->request( $req );
96 0 0 0       if ( $res && $res->is_success ) {
97 0           my $json = from_json( $res->content );
98             # 输出数据
99 0           print_result( $json );
100             }
101             }
102              
103              
104             sub login {
105 0     0 0   my $self = shift;
106 0           my $uri = 'https://console.plcloud.com/auth/login/';
107              
108 0           my $post = {
109             csrfmiddlewaretoken => $self->csrftoken,
110             username => $self->user,
111             password => $self->pass,
112             region => 'http://58.67.194.89:5001/v2.0',
113             };
114              
115 0           my $res = $self->ua->post( $uri, $post );
116              
117 0 0 0       return 1 if $res && $res->code == 302;
118 0           return 0;
119             }
120              
121              
122             # init PlCloud Object
123             sub BUILD {
124 0     0 0   my ( $self ) = @_;
125              
126 0           $self->ua( LWP::UserAgent->new( cookie_jar => {} ) );
127 0           $self->ua->max_redirect( 9 );
128              
129             # 初始化 csrftoken
130 0           $self->__init_csrftoken();
131              
132 0           return $self;
133             }
134              
135             sub __init_csrftoken {
136 0     0     my $self = shift;
137              
138 0           my $uri = 'https://console.plcloud.com/api/';
139 0           my $res = $self->ua->get( $uri );
140              
141 0 0         if ( $res->is_success ) {
142 0           my $cookie = $self->ua->cookie_jar;
143             $cookie->scan( sub {
144 0 0   0     $self->csrftoken( $_[2] ) if $_[1] eq 'csrftoken';
145 0 0         $self->sessionid( $_[2] ) if $_[1] eq 'sessionid';
146 0           } );
147              
148             }else {
149 0           die "Can't get CSRFtoken\n";
150             }
151              
152 0           return $self;
153             }
154              
155             sub print_result {
156 0     0 0   my ( $json, $shift ) = @_;
157              
158 0           foreach my $k ( sort keys %$json ) {
159 0 0         if ( ref $json->{ $k } eq 'HASH' ) {
160 0           print "$k:\n";
161 0           print_result( $json->{ $k }, 1 );
162             } else {
163 0 0         print "\t" if $shift;
164 0           print "$k: $json->{ $k }\n";
165             }
166             }
167             }
168              
169             1;
170              
171             =head1 AUTHOR
172              
173             MC Cheung, C<< >>
174              
175             =head1 BUGS
176              
177             Please report any bugs or feature requests to C, or through
178             the web interface at L. I will be notified, and then you'll
179             automatically be notified of progress on your bug as I make changes.
180              
181              
182              
183              
184             =head1 SUPPORT
185              
186             You can find documentation for this module with the perldoc command.
187              
188             perldoc WWW::PlCloud
189              
190              
191             You can also look for information at:
192              
193             =over 4
194              
195             =item * RT: CPAN's request tracker (report bugs here)
196              
197             L
198              
199             =item * AnnoCPAN: Annotated CPAN documentation
200              
201             L
202              
203             =item * CPAN Ratings
204              
205             L
206              
207             =item * Search CPAN
208              
209             L
210              
211             =back
212              
213              
214             =head1 ACKNOWLEDGEMENTS
215              
216              
217             =head1 LICENSE AND COPYRIGHT
218              
219             Copyright 2015 MC Cheung.
220              
221             This program is free software; you can redistribute it and/or modify it
222             under the terms of the the Artistic License (2.0). You may obtain a
223             copy of the full license at:
224              
225             L
226              
227             Any use, modification, and distribution of the Standard or Modified
228             Versions is governed by this Artistic License. By using, modifying or
229             distributing the Package, you accept this license. Do not use, modify,
230             or distribute the Package, if you do not accept this license.
231              
232             If your Modified Version has been derived from a Modified Version made
233             by someone other than you, you are nevertheless required to ensure that
234             your Modified Version complies with the requirements of this license.
235              
236             This license does not grant you the right to use any trademark, service
237             mark, tradename, or logo of the Copyright Holder.
238              
239             This license includes the non-exclusive, worldwide, free-of-charge
240             patent license to make, have made, use, offer to sell, sell, import and
241             otherwise transfer the Package with respect to any patent claims
242             licensable by the Copyright Holder that are necessarily infringed by the
243             Package. If you institute patent litigation (including a cross-claim or
244             counterclaim) against any party alleging that the Package constitutes
245             direct or contributory patent infringement, then this Artistic License
246             to you shall terminate on the date that such litigation is filed.
247              
248             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
249             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
250             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
251             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
252             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
253             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
254             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
255             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256              
257              
258             =cut
259              
260             1; # End of WWW::PlCloud