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   130 use 5.010_000;
  7         33  
8 7     7   51 use strict;
  7         16  
  7         172  
9 7     7   39 use warnings;
  7         22  
  7         169  
10              
11             # Global creator
12       7     BEGIN {
13             # No exports.
14             }
15              
16             # Global destructor
17       7     END {
18             }
19              
20             # ABSTRACT: Internet connection reference!
21              
22             our $VERSION = '0.14'; # VERSION: generated by DZP::OurPkgVersion
23              
24 7     7   58 use Log::Any qw{$log};
  7         20  
  7         50  
25 7     7   1495 use Hash::Util 0.06 qw{lock_keys unlock_keys};
  7         116  
  7         38  
26 7     7   1628 use Carp::Assert;
  7         2543  
  7         68  
27 7     7   1012 use Carp::Assert::More;
  7         19  
  7         1305  
28 7     7   57 use English '-no_match_vars';
  7         11  
  7         50  
29              
30             # DEFAULTS
31 7     7   2654 use Const::Fast;
  7         15  
  7         44  
32             const my $DEFAULT_PROTOCOL => 'https';
33             const my $DEFAULT_PORT => 443;
34             const my $DEFAULT_TIMEOUT => 3;
35              
36             sub new {
37 3     3 1 14 my ( $class, $params ) = @_;
38 3         14 $log->tracef( 'Entering new(%s, %s)', $class, $params );
39 3         521 my $self;
40 3         9 my @self_keys = ( ## no critic (CodeLayout::ProhibitQuotedWordLists)
41             'project_id', # The ID of the project to use for requests.
42             'token', # The OAuth token that should be used to authenticate requests. Can be found in the HUD.
43             'host', # The domain name the API can be located at. Defaults to a product-specific value, but always using Amazon's cloud.
44             'protocol'
45             , # The protocol that will be used to communicate with the API. Defaults to "https", which should be sufficient for 99% of users.
46             'port', # The port to connect to the API through. Defaults to 443, which should be sufficient for 99% of users.
47             'api_version'
48             , # 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!
49             'timeout', # REST client timeout (for REST calls accessing Iron services)
50             'connector', # Reference to the object which does the actual REST client calls, or mocks them.
51             );
52 3         10 lock_keys( %{$self}, @self_keys );
  3         18  
53 3         194 $log->debugf( 'The params: %s', $params );
54 3 50       517 $self->{'project_id'} = defined $params->{'project_id'} ? $params->{'project_id'} : undef;
55 3 100       15 $self->{'token'} = defined $params->{'token'} ? $params->{'token'} : undef;
56 3 50       16 $self->{'host'} = defined $params->{'host'} ? $params->{'host'} : undef;
57 3 100       17 $self->{'protocol'} = defined $params->{'protocol'} ? $params->{'protocol'} : $DEFAULT_PROTOCOL;
58 3 100       10 $self->{'port'} = defined $params->{'port'} ? $params->{'port'} : $DEFAULT_PORT;
59 3 50       27 $self->{'api_version'} = defined $params->{'api_version'} ? $params->{'api_version'} : undef;
60 3 100       11 $self->{'timeout'} = defined $params->{'timeout'} ? $params->{'timeout'} : $DEFAULT_TIMEOUT;
61              
62             # Set up the connector object.
63 3 50       8 if ( defined $params->{'connector'} ) {
64 0         0 $self->{'connector'} = $params->{'connector'}; # The connector has been instantiated for us.
65             }
66             else {
67 3         1600 require IO::Iron::Connector;
68 3         26 $self->{'connector'} = IO::Iron::Connector->new();
69             }
70              
71 3         37 unlock_keys( %{$self} );
  3         35  
72 3         71 bless $self, $class;
73 3         11 lock_keys( %{$self}, @self_keys );
  3         66  
74              
75             #$self->_assert_configuration($self);
76              
77             $log->infof(
78             'IO::Iron::Connection client created with config: (project_id=%s; token=%s; host=%s; protocol=%s; port=%s; api_version=%s; timeout=%s).',
79             $self->{'project_id'}, $self->{'token'}, $self->{'host'}, $self->{'protocol'},
80 3         527 $self->{'port'}, $self->{'api_version'}, $self->{'timeout'}
81             );
82 3         497 $log->tracef( 'Exiting new: %s', $self );
83 3         1359 return $self;
84             }
85              
86             sub perform_iron_action {
87 0     0 1   my ( $self, $iron_action, $params ) = @_;
88 0 0         if ( !defined $params ) {
89 0           $params = {};
90             }
91 0           $log->tracef( 'Entering perform_iron_action(%s, %s)', $iron_action, $params );
92 0           $self->_assert_configuration();
93              
94 0           my $href = $iron_action->{'href'};
95 0           my $action_verb = $iron_action->{'action'};
96 0           my $retry = $iron_action->{'retry'};
97 0           my $require_body = $iron_action->{'require_body'};
98 0 0         my $paged = $iron_action->{'paged'} ? $iron_action->{'paged'} : 0;
99 0 0         my $per_page = $iron_action->{'per_page'} ? $iron_action->{'per_page'} : 0;
100 0 0         my $log_message = $iron_action->{'log_message'} ? $iron_action->{'log_message'} : q{};
101 0 0         my $request_fields = $iron_action->{'request_fields'} ? $iron_action->{'request_fields'} : {};
102 0           my $content_type = $iron_action->{'content_type'};
103              
104 0           $params->{'{Protocol}'} = $self->{'protocol'};
105 0           $params->{'{Port}'} = $self->{'port'};
106 0           $params->{'{Host}'} = $self->{'host'};
107 0           $params->{'{Project ID}'} = $self->{'project_id'};
108 0           $params->{'{API Version}'} = $self->{'api_version'};
109 0           $params->{'authorization_token'} = $self->{'token'};
110 0           $params->{'http_client_timeout'} = $self->{'timeout'};
111 0           $params->{'content_type'} = $content_type;
112              
113 0           my $connector = $self->{'connector'};
114 0           my ( $http_status_code, $returned_msg ) = $connector->perform_iron_action( $iron_action, $params );
115              
116             # Logging
117 0           foreach my $key ( sort keys %{$params} ) {
  0            
118 0           my $value = $params->{$key};
119 0           $log_message =~ s/$key/$value/gs; ## no critic (RegularExpressions::RequireExtendedFormatting)
120             }
121 0           foreach my $key ( sort keys %{$request_fields} ) {
  0            
122 0           my $field_name = $request_fields->{$key};
123 0 0         my $field_value = $params->{'body'}->{$key} ? $params->{'body'}->{$key} : q{};
124 0           $log_message =~ s/$field_name/$field_value/gs; ## no critic (RegularExpressions::RequireExtendedFormatting)
125             }
126 0           $log->info($log_message);
127 0           $log->tracef( 'Exiting perform_iron_action(): %s', $returned_msg );
128 0           return $http_status_code, $returned_msg;
129             }
130              
131             # INTERNAL METHODS
132              
133             # Assert that all the configuration is valid before making any network operation.
134             sub _assert_configuration {
135 0     0     my ($self) = @_;
136 0           $log->tracef( 'Entering _assert_configuration(%s)', $self );
137              
138 0           my $rval = 1;
139 0           assert_nonblank( $self->{'project_id'}, 'self->{project_id} is defined and not blank.' );
140 0           assert_nonblank( $self->{'token'}, 'self->{token} is defined and not blank.' );
141 0           assert_nonblank( $self->{'host'}, 'self->{host} is defined and not blank.' );
142 0           assert_nonblank( $self->{'protocol'}, 'self->{protocol} is defined and not blank.' );
143 0           assert_nonblank( $self->{'port'}, 'self->{port} is defined and not blank.' );
144 0           assert_nonblank( $self->{'api_version'}, 'self->{api_version} is defined and not blank.' );
145              
146             #assert_nonblank( $self->{'timeout'}, 'self->{timeout} is defined and not blank.' );
147 0           assert_nonnegative_integer( $self->{'timeout'}, 'self->{timeout} is a nonnegative integer.' );
148 0           assert_isa( $self->{'connector'}, 'IO::Iron::ConnectorBase', 'self->{connector} is a descendant of IO::Iron::ConnectorBase.' );
149              
150 0           $log->tracef( 'Exiting _assert_configuration(): %d', $rval );
151 0           return $rval;
152             }
153              
154             1;
155              
156             __END__
157              
158             =pod
159              
160             =encoding UTF-8
161              
162             =head1 NAME
163              
164             IO::Iron::Connection - Internet connection reference!
165              
166             =head1 VERSION
167              
168             version 0.14
169              
170             =head1 SYNOPSIS
171              
172             This package is for internal use of IO::Iron packages.
173              
174             =head1 DESCRIPTION
175              
176             =for stopwords Params IronMQ params Mikko Koivunalho ACKNOWLEDGMENTS TODO
177              
178             =head1 SUBROUTINES/METHODS
179              
180             =head2 new
181              
182             Creator function.
183              
184             =head2 perform_iron_action
185              
186             =over 8
187              
188             =item Params: action name, params hash.
189              
190             =item Return: 1/0 (1 if success, 0 in all failures),
191             HTTP return code, hash if success/failed request.
192              
193             =back
194              
195             =head1 AUTHOR
196              
197             Mikko Koivunalho <mikko.koivunalho@iki.fi>
198              
199             =head1 BUGS
200              
201             Please report any bugs or feature requests to bug-io-iron@rt.cpan.org or through the web interface at:
202             http://rt.cpan.org/Public/Dist/Display.html?Name=IO-Iron
203              
204             =head1 COPYRIGHT AND LICENSE
205              
206             This software is copyright (c) 2023 by Mikko Koivunalho.
207              
208             This is free software; you can redistribute it and/or modify it under
209             the same terms as the Perl 5 programming language system itself.
210              
211             The full text of the license can be found in the
212             F<LICENSE> file included with this distribution.
213              
214             =cut