File Coverage

blib/lib/IO/Iron/IronCache/Client.pm
Criterion Covered Total %
statement 76 126 60.3
branch 3 6 50.0
condition n/a
subroutine 17 21 80.9
pod 6 6 100.0
total 102 159 64.1


line stmt bran cond sub pod time code
1             package IO::Iron::IronCache::Client;
2              
3             ## no critic (Documentation::RequirePodAtEnd)
4             ## no critic (Documentation::RequirePodSections)
5             ## no critic (Subroutines::RequireArgUnpacking)
6              
7 6     6   70291 use 5.010_000;
  6         32  
8 6     6   37 use strict;
  6         9  
  6         121  
9 6     6   27 use warnings;
  6         13  
  6         206  
10              
11             # Global creator
12 6         605 BEGIN {
13 6     6   1359 use parent qw(IO::Iron::ClientBase); # Inheritance
  6         971  
  6         31  
14 6     6   328 use parent qw(IO::Iron::IronCache::Policy );
  6         16  
  6         27  
15             }
16              
17             # Global destructor
18       6     END {
19             }
20              
21              
22             # ABSTRACT: IronCache (Online Item-Value Storage) Client.
23              
24             our $VERSION = '0.12_01'; # TRIAL VERSION: generated by DZP::OurPkgVersion
25              
26              
27              
28 6     6   34 use Log::Any qw{$log};
  6         53  
  6         1203  
29 6     6   110 use Hash::Util 0.06 qw{lock_keys lock_keys_plus unlock_keys legal_keys};
  6         35  
  6         433  
30 6     6   12 use Carp::Assert::More;
  6         841  
  6         57  
31 6     6   14 use English '-no_match_vars';
  6         36  
  6         2222  
32 6     6   12 use Params::Validate qw(:all);
  6         806  
  6         2874  
33              
34 6     6   17 use IO::Iron::IronCache::Api ();
  6         126  
  6         47  
35 6     6   15 use IO::Iron::Common ();
  6         203  
  6         1391  
36             require IO::Iron::Connection;
37             require IO::Iron::IronCache::Cache;
38              
39             # CONSTANTS for this package
40              
41             # DEFAULTS
42 6     6   7812 use Const::Fast;
  6         44  
  2         5435  
43              
44             # Service specific!
45             const my $DEFAULT_API_VERSION => '1';
46             const my $DEFAULT_HOST => 'cache-aws-us-east-1.iron.io';
47              
48              
49              
50              
51              
52             sub new {
53 2     2 1 10 my $class = shift;
54             my %params = validate(
55             @_, {
56 20         154 map { $_ => { type => SCALAR, optional => 1 }, } IO::Iron::Common::IRON_CLIENT_PARAMETERS(), ## no critic (ValuesAndExpressions::ProhibitCommaSeparatedStatements)
  2         43  
57             }
58             );
59              
60 2         912 $log->tracef('Entering new(%s, %s)', $class, \%params);
61 2         6 my $self = IO::Iron::ClientBase->new();
62             # Add more keys to the self hash.
63             my @self_keys = (
64             'caches', # References to all objects created of class IO::Iron::IronCache::Cache. Not in use!
65             'policy', # The policies of this client.
66 2         9 legal_keys(%{$self}),
  2         16  
67             );
68 2         9 unlock_keys(%{$self});
  2         19  
69 2         8 lock_keys_plus(%{$self}, @self_keys);
  2         118  
70 2         23 my $config = IO::Iron::Common::get_config(%params);
71 2         500 $log->debugf('The config: %s', $config);
72 2 50       11 $self->{'project_id'} = defined $config->{'project_id'} ? $config->{'project_id'} : undef;
73 2         8 $self->{'caches'} = [];
74 2         29 assert_nonblank( $self->{'project_id'}, 'self->{project_id} is not defined or is blank');
75              
76 2         9 unlock_keys(%{$self});
  2         17  
77 2         3 bless $self, $class;
78 2         22 lock_keys(%{$self}, @self_keys);
  2         124  
79              
80             # Set up the policies
81             # We have to do this after blessing the object
82             # because get_policies has late bindings.
83 2         26 $self->{'policy'} = $self->get_policies('policies' => $config->{'policies'});
84              
85             # Set up the connection client
86             my $connection = IO::Iron::Connection->new( {
87             'project_id' => $config->{'project_id'},
88             'token' => $config->{'token'},
89             'host' => defined $config->{'host'} ? $config->{'host'} : $DEFAULT_HOST,
90             'protocol' => $config->{'protocol'},
91             'port' => $config->{'port'},
92             'api_version' => defined $config->{'api_version'} ? $config->{'api_version'} : $DEFAULT_API_VERSION,
93             'timeout' => $config->{'timeout'},
94 2 50       18 'connector' => $params{'connector'},
    50          
95             }
96             );
97 2         22 $self->{'connection'} = $connection;
98 2         184 $log->debugf('IronCache client created with config: (project_id=%s; token=%s; host=%s; timeout=%s).', $config->{'project_id'}, $config->{'token'}, $config->{'host'}, $config->{'timeout'});
99 2         841 $log->tracef('Exiting new: %s', $self);
100 0         0 return $self;
101             }
102              
103              
104             sub get_caches {
105 0     0 1 0 my $self = shift;
106 0         0 validate(
107             @_, {
108             # No parameters
109             }
110             );
111 0         0 $log->tracef('Entering get_caches()');
112              
113 0         0 my @caches;
114 0         0 my $connection = $self->{'connection'};
115 0         0 my ($http_status_code, $response_message) = $connection->perform_iron_action(
116             IO::Iron::IronCache::Api::IRONCACHE_LIST_CACHES(), { } );
117 0         0 $self->{'last_http_status_code'} = $http_status_code;
118 0         0 foreach my $cache_info (@{$response_message}) {
  0         0  
119 0         0 my $get_cache_name = $cache_info->{'name'};
120             my $cache = IO::Iron::IronCache::Cache->new({
121             'ironcache_client' => $self, # Pass a reference to the parent object.
122             'name' => $get_cache_name,
123             'connection' => $self->{'connection'},
124 0         0 'policy' => $self->{'policy'},
125             });
126 0         0 push @caches, $cache;
127             }
128             #push @{$self->{'caches'}}, @caches; # Store only created caches!
129 0         0 $log->debugf('Created caches: %s', \@caches);
130              
131 0         0 $log->tracef('Exiting get_caches: %s', \@caches);
132 0         0 return @caches;
133             }
134              
135              
136             sub get_info_about_cache {
137 0     0 1 0 my $self = shift;
138 0         0 my %params = validate(
139             @_, {
140             'name' => { type => SCALAR, }, # cache name.
141             }
142             );
143 0         0 $log->tracef('Entering get_info_about_cache(%s)', \%params);
144              
145 0         0 my $connection = $self->{'connection'};
146             my ($http_status_code, $response_message) = $connection->perform_iron_action(
147             IO::Iron::IronCache::Api::IRONCACHE_GET_INFO_ABOUT_A_CACHE(),
148 0         0 { '{Cache Name}' => $params{'name'}, }
149             );
150 0         0 $self->{'last_http_status_code'} = $http_status_code;
151 0         0 my $info = $response_message;
152              
153             # info:
154             # {'id':'523566104a734c39bf00041e','project_id':'51bdf5fb2267d84ced002c99',
155             # 'name':'TEST_CACHE_01','size':0,'data_size':0}
156             # Fetched info about cache:{created_at => '0001-01-01T00:00:00Z',data_size => 0,id => '53083eb79ef7915a7f005bc0'
157             # ,name => 'name',project_id => '51bdf5fb2267d84ced002c99',size => 1,updated_at => '0001-01-01T00:00:00Z'}
158 0         0 $log->tracef('Exiting get_info_about_cache: %s', $info);
159 0         0 return $info;
160             }
161              
162              
163             sub get_cache {
164 0     0 1 0 my $self = shift;
165 0         0 my %params = validate(
166             @_, {
167             'name' => { type => SCALAR, }, # cache name.
168             }
169             );
170 0         0 $log->tracef('Entering get_cache(%s)', \%params);
171              
172 0         0 my $connection = $self->{'connection'};
173             my ($http_status_code, $response_message) = $connection->perform_iron_action(
174             IO::Iron::IronCache::Api::IRONCACHE_GET_INFO_ABOUT_A_CACHE(),
175 0         0 { '{Cache Name}' => $params{'name'}, }
176             );
177 0         0 $self->{'last_http_status_code'} = $http_status_code;
178 0         0 my $get_cache_name = $response_message->{'name'};
179             my $cache = IO::Iron::IronCache::Cache->new({
180             'ironcache_client' => $self, # Pass a reference to the parent object.
181             'name' => $get_cache_name,
182             'connection' => $self->{'connection'},
183 0         0 'policy' => $self->{'policy'},
184             });
185 0         0 push @{$self->{'caches'}}, $cache;
  0         0  
186 0         0 $log->debugf('Created a new IO::Iron::IronCache::Cache object (name=%s).', $get_cache_name);
187 0         0 $log->tracef('Exiting get_cache: %s', $cache);
188 2         55 return $cache;
189             }
190              
191              
192             sub create_cache {
193 2     2 1 42 my $self = shift;
194             my %params = validate(
195             @_, {
196             'name' => { type => SCALAR, callbacks => {
197 2     2   21 'RFC 3986 reserved character check' => sub { return ! IO::Iron::Common::contains_rfc_3986_res_chars(shift) },
198             }}, # cache name.
199             }
200 2         10 );
201 2         501 $log->tracef('Entering create_cache(%s)', \%params);
202              
203 1         13 $self->validate_cache_name('name' => $params{'name'});
204             my $cache = IO::Iron::IronCache::Cache->new({
205             'ironcache_client' => $self, # Pass a reference to the parent object.
206             'name' => $params{'name'},
207             'connection' => $self->{'connection'},
208 1         8 'policy' => $self->{'policy'},
209             });
210 1         11 push @{$self->{'caches'}}, $cache;
  1         6  
211              
212 1         80 $log->debugf('Created a new IO::Iron::IronCache::Cache object (name=%s.)', $params{'name'});
213 1         409 $log->tracef('Exiting get_cache: %s', $cache);
214 0           return $cache;
215             }
216              
217              
218             sub delete_cache {
219 0     0 1   my $self = shift;
220 0           my %params = validate(
221             @_, {
222             'name' => { type => SCALAR, }, # cache name.
223             }
224             );
225 0           $log->tracef('Entering delete_cache(%s)', \%params);
226              
227 0           my $connection = $self->{'connection'};
228             my ($http_status_code) = $connection->perform_iron_action(
229             IO::Iron::IronCache::Api::IRONCACHE_DELETE_A_CACHE(),
230             {
231 0           '{Cache Name}' => $params{'name'},
232             }
233             );
234 0           $self->{'last_http_status_code'} = $http_status_code;
235 0           @{$self->{'caches'}} = grep { $_->name() ne $params{'name'} } @{$self->{'caches'}};
  0            
  0            
  0            
236              
237 0           $log->debugf('Deleted cache (name=%s.)', $params{'name'});
238 0           $log->tracef('Exiting delete_cache: %d', 1);
239             return 1;
240             }
241              
242             1;
243              
244             __END__