File Coverage

blib/lib/IO/Iron/Connection.pm
Criterion Covered Total %
statement 51 101 50.5
branch 12 28 42.8
condition n/a
subroutine 12 14 85.7
pod 2 2 100.0
total 77 145 53.1


line stmt bran cond sub pod time code
1             package IO::Iron::Connection;
2              
3             ## no critic (Documentation::RequirePodAtEnd)
4             ## no critic (Documentation::RequirePodSections)
5             ## no critic (RegularExpressions::RequireLineBoundaryMatching)
6              
7 7     7   124 use 5.010_000;
  7         25  
8 7     7   50 use strict;
  7         19  
  7         161  
9 7     7   34 use warnings;
  7         14  
  7         203  
10              
11             # Global creator
12       7     BEGIN {
13             # No exports.
14             }
15              
16             # Global destructor
17       7     END {
18             }
19              
20              
21             # ABSTRACT: Internet connection reference!
22              
23             our $VERSION = '0.13'; # VERSION: generated by DZP::OurPkgVersion
24              
25              
26              
27 7     7   55 use Log::Any qw{$log};
  7         12  
  7         54  
28 7     7   1520 use Hash::Util 0.06 qw{lock_keys unlock_keys};
  7         133  
  7         43  
29 7     7   492 use Carp::Assert;
  7         15  
  7         53  
30 7     7   955 use Carp::Assert::More;
  7         15  
  7         1096  
31 7     7   47 use English '-no_match_vars';
  7         12  
  7         40  
32              
33              
34             # DEFAULTS
35 7     7   2578 use Const::Fast;
  7         16  
  7         48  
36             const my $DEFAULT_PROTOCOL => 'https';
37             const my $DEFAULT_PORT => 443;
38             const my $DEFAULT_TIMEOUT => 3;
39             ;
40              
41              
42              
43             sub new {
44 3     3 1 8 my ($class, $params) = @_;
45 3         11 $log->tracef('Entering new(%s, %s)', $class, $params);
46 3         485 my $self;
47 3         12 my @self_keys = ( ## no critic (CodeLayout::ProhibitQuotedWordLists)
48             'project_id', # The ID of the project to use for requests.
49             'token', # The OAuth token that should be used to authenticate requests. Can be found in the HUD.
50             'host', # The domain name the API can be located at. Defaults to a product-specific value, but always using Amazon's cloud.
51             'protocol', # The protocol that will be used to communicate with the API. Defaults to "https", which should be sufficient for 99% of users.
52             'port', # The port to connect to the API through. Defaults to 443, which should be sufficient for 99% of users.
53             'api_version', # The version of the API to connect through. Defaults to the version supported by the client. End-users should probably never change this. Except: IronMQ service upgraded from v2 to v3 in 2015!
54             'timeout', # REST client timeout (for REST calls accessing Iron services)
55             'connector', # Reference to the object which does the actual REST client calls, or mocks them.
56             );
57 3         8 lock_keys(%{$self}, @self_keys);
  3         15  
58 3         193 $log->debugf('The params: %s', $params);
59 3 50       510 $self->{'project_id'} = defined $params->{'project_id'} ? $params->{'project_id'} : undef;
60 3 100       12 $self->{'token'} = defined $params->{'token'} ? $params->{'token'} : undef;
61 3 50       14 $self->{'host'} = defined $params->{'host'} ? $params->{'host'} : undef;
62 3 100       11 $self->{'protocol'} = defined $params->{'protocol'} ? $params->{'protocol'} : $DEFAULT_PROTOCOL;
63 3 100       108 $self->{'port'} = defined $params->{'port'} ? $params->{'port'} : $DEFAULT_PORT;
64 3 50       80 $self->{'api_version'} = defined $params->{'api_version'} ? $params->{'api_version'} : undef;
65 3 100       16 $self->{'timeout'} = defined $params->{'timeout'} ? $params->{'timeout'} : $DEFAULT_TIMEOUT;
66             # Set up the connector object.
67 3 50       8 if(defined $params->{'connector'}) {
68 0         0 $self->{'connector'} = $params->{'connector'}; # The connector has been instantiated for us.
69             }
70             else {
71 3         1479 require IO::Iron::Connector;
72 3         29 $self->{'connector'} = IO::Iron::Connector->new();
73             }
74              
75 3         25 unlock_keys(%{$self});
  3         19  
76 3         42 bless $self, $class;
77 3         6 lock_keys(%{$self}, @self_keys);
  3         31  
78              
79             #$self->_assert_configuration($self);
80              
81             $log->infof('IO::Iron::Connection client created with config: (project_id=%s; token=%s; host=%s; protocol=%s; port=%s; api_version=%s; timeout=%s).',
82 3         308 $self->{'project_id'}, $self->{'token'}, $self->{'host'}, $self->{'protocol'}, $self->{'port'}, $self->{'api_version'}, $self->{'timeout'});
83 3         297 $log->tracef('Exiting new: %s', $self);
84 3         721 return $self;
85             }
86              
87              
88             sub perform_iron_action {
89 0     0 1   my ($self, $iron_action, $params) = @_;
90 0 0         if(!defined $params) {
91 0           $params = {};
92             }
93 0           $log->tracef('Entering perform_iron_action(%s, %s)', $iron_action, $params);
94 0           $self->_assert_configuration();
95              
96 0           my $href = $iron_action->{'href'};
97 0           my $action_verb = $iron_action->{'action'};
98 0           my $retry = $iron_action->{'retry'};
99 0           my $require_body = $iron_action->{'require_body'};
100 0 0         my $paged = $iron_action->{'paged'} ? $iron_action->{'paged'} : 0;
101 0 0         my $per_page = $iron_action->{'per_page'} ? $iron_action->{'per_page'} : 0;
102 0 0         my $log_message = $iron_action->{'log_message'} ? $iron_action->{'log_message'} : q{};
103 0 0         my $request_fields = $iron_action->{'request_fields'} ? $iron_action->{'request_fields'} : {};
104 0           my $content_type = $iron_action->{'content_type'};
105              
106 0           $params->{'{Protocol}'} = $self->{'protocol'};
107 0           $params->{'{Port}'} = $self->{'port'};
108 0           $params->{'{Host}'} = $self->{'host'};
109 0           $params->{'{Project ID}'} = $self->{'project_id'};
110 0           $params->{'{API Version}'} = $self->{'api_version'};
111 0           $params->{'authorization_token'} = $self->{'token'};
112 0           $params->{'http_client_timeout'} = $self->{'timeout'};
113 0           $params->{'content_type'} = $content_type;
114              
115 0           my $connector = $self->{'connector'};
116 0           my ($http_status_code, $returned_msg) = $connector->perform_iron_action($iron_action, $params);
117              
118             # Logging
119 0           foreach my $key (sort keys %{$params}) {
  0            
120 0           my $value = $params->{$key};
121 0           $log_message =~ s/$key/$value/gs; ## no critic (RegularExpressions::RequireExtendedFormatting)
122             };
123 0           foreach my $key (sort keys %{$request_fields}) {
  0            
124 0           my $field_name = $request_fields->{$key};
125 0 0         my $field_value = $params->{'body'}->{$key} ? $params->{'body'}->{$key} : q{};
126 0           $log_message =~ s/$field_name/$field_value/gs; ## no critic (RegularExpressions::RequireExtendedFormatting)
127             };
128 0           $log->info($log_message);
129 0           $log->tracef('Exiting perform_iron_action(): %s', $returned_msg );
130 0           return $http_status_code, $returned_msg;
131             }
132              
133             # INTERNAL METHODS
134              
135             # Assert that all the configuration is valid before making any network operation.
136             sub _assert_configuration {
137 0     0     my ($self) = @_;
138 0           $log->tracef('Entering _assert_configuration(%s)', $self);
139              
140 0           my $rval = 1;
141 0           assert_nonblank( $self->{'project_id'}, 'self->{project_id} is defined and not blank.' );
142 0           assert_nonblank( $self->{'token'}, 'self->{token} is defined and not blank.' );
143 0           assert_nonblank( $self->{'host'}, 'self->{host} is defined and not blank.' );
144 0           assert_nonblank( $self->{'protocol'}, 'self->{protocol} is defined and not blank.' );
145 0           assert_nonblank( $self->{'port'}, 'self->{port} is defined and not blank.' );
146 0           assert_nonblank( $self->{'api_version'}, 'self->{api_version} is defined and not blank.' );
147             #assert_nonblank( $self->{'timeout'}, 'self->{timeout} is defined and not blank.' );
148 0           assert_nonnegative_integer( $self->{'timeout'}, 'self->{timeout} is a nonnegative integer.' );
149 0           assert_isa( $self->{'connector'}, 'IO::Iron::ConnectorBase', 'self->{connector} is a descendant of IO::Iron::ConnectorBase.' );
150              
151 0           $log->tracef('Exiting _assert_configuration(): %d', $rval);
152 0           return $rval;
153             }
154              
155             1;
156              
157             __END__