File Coverage

blib/lib/WWW/Codeguard/Partner.pm
Criterion Covered Total %
statement 29 62 46.7
branch 2 4 50.0
condition 0 6 0.0
subroutine 9 18 50.0
pod 6 7 85.7
total 46 97 47.4


line stmt bran cond sub pod time code
1             package WWW::Codeguard::Partner;
2              
3 1     1   5 use strict;
  1         2  
  1         27  
4 1     1   4 use warnings FATAL => 'all', NONFATAL => 'uninitialized';
  1         1  
  1         28  
5              
6 1     1   424 use parent qw(WWW::Codeguard);
  1         221  
  1         4  
7              
8 1     1   44 use JSON;
  1         1  
  1         3  
9 1     1   545 use LWP::UserAgent;
  1         33977  
  1         30  
10 1     1   9 use HTTP::Request;
  1         2  
  1         506  
11              
12             =head1 NAME
13              
14             WWW::Codeguard::Partner - Perl interface to interact with the Codeguard API as a 'partner'.
15              
16             =cut
17              
18             =head1 SYNOPSIS
19              
20             This module provides you with an perl interface to interact with the Codeguard API and perform the 'partner' level calls.
21              
22             use WWW::Codeguard::Partner;
23              
24             my $api = WWW::Codeguard::Partner->new(
25             $api_url,
26             {
27             partner_key => $partner_key,
28             verify_hostname => 1,
29             }
30             );
31              
32             $api->create_user($params_for_create_user);
33             $api->list_user($params_for_list_user);
34             $api->delete_user($params_for_delete_user);
35              
36             =cut
37              
38             sub new {
39              
40 2     2 0 3 my $class = shift;
41 2         3 my $self = {};
42 2         3 bless $self, $class;
43 2         4 $self->_initialize(@_);
44 2         5 return $self;
45             }
46              
47             sub _initialize {
48              
49 2     2   4 my ($self, $api_url, $opts) = @_;
50              
51 2         7 $self->{api_url} = $api_url;
52 2 50       5 $self->{partner_key} = delete $opts->{partner_key} or $self->_error('partner_key is a required parameter', 1);
53              
54             # initialize the UA
55             $self->{_ua} = LWP::UserAgent->new(
56             agent => 'WWW-Codeguard-Partner '.$self->VERSION,
57             ssl_opts => {
58 2 50       7 verify_hostname => (exists $opts->{verify_hostname}? $opts->{verify_hostname} : 1),
59             },
60             );
61              
62 2         2257 return $self;
63             }
64              
65             =head1 METHODS
66              
67             Each of these map to a call on Codeguard's Partner API.
68            
69             =cut
70              
71             =head2 create_user
72              
73             This allows you to create an account, on to which you can add website/database resources. Params should be a hashref that contains the following attributes:
74              
75             Required: The request will not succeed without these attributes.
76              
77             name
78             email
79              
80             Optional Attributes:
81              
82             password
83             time_zone
84             partner_data - This is a string that can be used to store user-specific information, that the partner may use to identify the user (such as package id, etc)
85             plan_id
86              
87             =cut
88              
89             sub create_user {
90              
91 0     0 1 0 my ($self, $params) = @_;
92 0         0 return $self->_do_method('create_user', $params);
93             }
94              
95             =head2 list_user
96              
97             This allows you to fetch information for a user. Params should be a hashref that contains the following attributes:
98              
99             Required:
100              
101             user_id
102              
103             Optional:
104              
105             None
106              
107             =cut
108              
109             sub list_user {
110              
111 0     0 1 0 my ($self, $params) = @_;
112 0         0 return $self->_do_method('list_user', $params);
113             }
114              
115             =head2 delete_user
116              
117             This method is used to delete existing users that were created using the specified partner_key. Parters can not delete users created by other partners.
118              
119             B Deleting a user resource will also delete all associated Website and Database records.
120              
121             Params should be a hashref that contains the following attributes:
122              
123             Required:
124              
125             user_id
126              
127             Optional Attributes:
128              
129             None
130              
131             =cut
132              
133             sub delete_user {
134              
135 0     0 1 0 my ($self, $params) = @_;
136 0         0 return $self->_do_method('delete_user', $params);
137             }
138              
139             =head2 change_user_plan
140              
141             This method is used to change an existing user's plan. Params should be a hashref that contains the following attributes:
142              
143             Required:
144              
145             user_id
146             plan_id
147              
148             Optional Attributes
149              
150             None
151              
152             =cut
153              
154             sub change_user_plan {
155              
156 0     0 1 0 my ($self, $params) = @_;
157 0         0 return $self->_do_method('change_user_plan', $params);
158             }
159              
160             =head2 user_quota_report
161              
162             Required:
163              
164             None
165              
166             Optional:
167              
168             None
169              
170             Returns an array of Users who are using more than their allowed disk quota.
171              
172             B: Full User models are not included. Only the Attributes listed below are returned:
173              
174             =over 4
175              
176             =item * email - User email address.
177              
178             =item * package - CodeGuard plan ID associated with this user.
179              
180             =item * quota - Allowed quota in bytes.
181              
182             =item * usage - Disk usage in bytes.
183              
184             =item * percent_usage - Percent of quota used. This can be > 100%.
185              
186             =item * signup_date - Date of User account creation.
187              
188             =item * overage_date - Date the user was notified or 'Not yet notified' if a notification has not yet occurred.
189              
190             =back
191              
192             =cut
193              
194             sub user_quota_report {
195              
196 0     0 1 0 my ($self, $params) = @_;
197 0         0 return $self->_do_method('user_quota_report', $params);
198             }
199              
200             =head1 Accessors
201              
202             Basic accessor methods to retrieve the current settings
203              
204             =cut
205              
206             =head2 get_partner_key
207              
208             Returns the partner_key of the object instance.
209              
210             =cut
211              
212 1     1 1 4 sub get_partner_key { shift->{partner_key}; }
213              
214             # Internal Methods
215              
216             sub _create_request {
217              
218 0     0     my ($self, $action, $params) = @_;
219 0           my $action_map = {
220             'change_user_plan' => 'POST',
221             'create_user' => 'POST',
222             'delete_user' => 'DELETE',
223             'list_user' => 'GET',
224             'user_quota_report' => 'GET',
225             };
226 0           my $request = HTTP::Request->new( $action_map->{$action} );
227 0           $request->header('Content-Type' => 'application/json' );
228 0           $self->_set_content($request, $params);
229 0           $self->_set_uri($action, $request, $params);
230 0           return $request;
231             }
232              
233             sub _set_uri {
234              
235 0     0     my ($self, $action, $request, $params) = @_;
236 0           my $base_url = $self->get_api_url();
237             my $uri_map = {
238             'change_user_plan' => '/users/'.($params->{user_id} || '').'/plan',
239             'create_user' => '/users',
240             'delete_user' => '/users/'.($params->{user_id} || ''),
241 0   0       'list_user' => '/users/'.($params->{user_id} || ''),
      0        
      0        
242             'user_quota_report' => '/partners/user_quota_report',
243             };
244 0           $request->uri($base_url.$uri_map->{$action}.'?api_key='.$self->get_partner_key);
245 0           return;
246             }
247              
248             sub _fetch_required_params {
249              
250 0     0     my ($self, $action, $params) = @_;
251             my $required_keys_map = {
252 0           'create_user' => { map { ($_ => 1) } qw(name email) },
253 0           'list_user' => { map { ($_ => 1) } qw(user_id) },
254 0           'change_user_plan' => { map { ($_ => 1) } qw(user_id plan_id) },
  0            
255             'user_quota_report' => { },
256             };
257 0           $required_keys_map->{delete_user} = $required_keys_map->{list_user};
258 0           return $required_keys_map->{$action};
259             }
260              
261             sub _fetch_optional_params {
262              
263 0     0     my ($self, $action) = @_;
264             my $optional_keys_map = {
265 0           'create_user' => { map { ($_ => 1) } qw(password time_zone partner_data plan_id) },
  0            
266             };
267 0           return $optional_keys_map->{$action};
268             }
269              
270             =head1 AUTHOR
271              
272             Rishwanth Yeddula, C<< >>
273              
274             =head1 BUGS
275              
276             Please report any bugs or feature requests to C, or through
277             the web interface at L. I will be notified, and then you'll
278             automatically be notified of progress on your bug as I make changes.
279              
280             =head1 SUPPORT
281              
282             You can find documentation for this module with the following perldoc commands.
283              
284             perldoc WWW::Codeguard
285             perldoc WWW::Codeguard::Partner
286             perldoc WWW::Codeguard::User
287              
288              
289             You can also look for information at:
290              
291             =over 4
292              
293             =item * RT: CPAN's request tracker (report bugs here)
294              
295             L
296              
297             =item * AnnoCPAN: Annotated CPAN documentation
298              
299             L
300              
301             =item * CPAN Ratings
302              
303             L
304              
305             =item * Search CPAN
306              
307             L
308              
309             =back
310              
311             =head1 ACKNOWLEDGMENTS
312              
313             Thanks to L for funding the development of this module and providing test resources.
314              
315             =head1 LICENSE AND COPYRIGHT
316              
317             Copyright 2014 Rishwanth Yeddula.
318              
319             This program is free software; you can redistribute it and/or modify it
320             under the terms of the the Artistic License (2.0). You may obtain a
321             copy of the full license at:
322              
323             L
324              
325             Any use, modification, and distribution of the Standard or Modified
326             Versions is governed by this Artistic License. By using, modifying or
327             distributing the Package, you accept this license. Do not use, modify,
328             or distribute the Package, if you do not accept this license.
329              
330             If your Modified Version has been derived from a Modified Version made
331             by someone other than you, you are nevertheless required to ensure that
332             your Modified Version complies with the requirements of this license.
333              
334             This license does not grant you the right to use any trademark, service
335             mark, tradename, or logo of the Copyright Holder.
336              
337             This license includes the non-exclusive, worldwide, free-of-charge
338             patent license to make, have made, use, offer to sell, sell, import and
339             otherwise transfer the Package with respect to any patent claims
340             licensable by the Copyright Holder that are necessarily infringed by the
341             Package. If you institute patent litigation (including a cross-claim or
342             counterclaim) against any party alleging that the Package constitutes
343             direct or contributory patent infringement, then this Artistic License
344             to you shall terminate on the date that such litigation is filed.
345              
346             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
347             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
348             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
349             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
350             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
351             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
352             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
353             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
354              
355             =cut
356              
357             1;