| 1 | package IO::Iron::Connection; | ||||||||||
| 2 | |||||||||||
| 3 | ## no critic (Documentation::RequirePodAtEnd) | ||||||||||
| 4 | ## no critic (Documentation::RequirePodSections) | ||||||||||
| 5 | ## no critic (RegularExpressions::RequireLineBoundaryMatching) | ||||||||||
| 6 | |||||||||||
| 7 | 7 | ▶ | use 5.010_000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # Global creator | ||||||||||
| 12 | ▶ | BEGIN { | |||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | # No exports. | ||||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | # Global destructor | ||||||||||
| 17 | ▶ | END { | |||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | # ABSTRACT: Internet connection reference! | ||||||||||
| 22 | |||||||||||
| 23 | 7 | our $VERSION = '0.13'; # VERSION: generated by DZP::OurPkgVersion | |||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | 7 | ▶ | use Log::Any qw{$log}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 7 | ▶ | use Hash::Util 0.06 qw{lock_keys unlock_keys}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 7 | ▶ | use Carp::Assert; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 7 | ▶ | use Carp::Assert::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 7 | ▶ | use English '-no_match_vars'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | # DEFAULTS | ||||||||||
| 35 | 7 | ▶ | use Const::Fast; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 7 | const my $DEFAULT_PROTOCOL => 'https'; | |||||||||
| 37 | 7 | const my $DEFAULT_PORT => 443; | |||||||||
| 38 | 7 | const my $DEFAULT_TIMEOUT => 3; | |||||||||
| 39 | ; | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | sub new { | ||||||||||
| 44 | 3 | ▶ | my ($class, $params) = @_; | ||||||||
|
Subroutine
new: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||
| 45 | 3 | $log->tracef('Entering new(%s, %s)', $class, $params); | |||||||||
| 46 | 3 | my $self; | |||||||||
| 47 | 3 | 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 | lock_keys(%{$self}, @self_keys); | |||||||||
| 58 | 3 | $log->debugf('The params: %s', $params); | |||||||||
| 59 | 3 | ▶ | $self->{'project_id'} = defined $params->{'project_id'} ? $params->{'project_id'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 3 | ▶ | $self->{'token'} = defined $params->{'token'} ? $params->{'token'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 3 | ▶ | $self->{'host'} = defined $params->{'host'} ? $params->{'host'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 3 | ▶ | $self->{'protocol'} = defined $params->{'protocol'} ? $params->{'protocol'} : $DEFAULT_PROTOCOL; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 3 | ▶ | $self->{'port'} = defined $params->{'port'} ? $params->{'port'} : $DEFAULT_PORT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 3 | ▶ | $self->{'api_version'} = defined $params->{'api_version'} ? $params->{'api_version'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 3 | ▶ | $self->{'timeout'} = defined $params->{'timeout'} ? $params->{'timeout'} : $DEFAULT_TIMEOUT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | # Set up the connector object. | ||||||||||
| 67 | 3 | ▶ | if(defined $params->{'connector'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | $self->{'connector'} = $params->{'connector'}; # The connector has been instantiated for us. | |||||||||
| 69 | } | ||||||||||
| 70 | else { | ||||||||||
| 71 | 3 | require IO::Iron::Connector; | |||||||||
| 72 | 3 | $self->{'connector'} = IO::Iron::Connector->new(); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 3 | unlock_keys(%{$self}); | |||||||||
| 76 | 3 | bless $self, $class; | |||||||||
| 77 | 3 | lock_keys(%{$self}, @self_keys); | |||||||||
| 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 | $self->{'project_id'}, $self->{'token'}, $self->{'host'}, $self->{'protocol'}, $self->{'port'}, $self->{'api_version'}, $self->{'timeout'}); | |||||||||
| 83 | 3 | $log->tracef('Exiting new: %s', $self); | |||||||||
| 84 | 3 | return $self; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | sub perform_iron_action { | ||||||||||
| 89 | 0 | ▶ | my ($self, $iron_action, $params) = @_; | ||||||||
|
Subroutine
perform_iron_action: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||
| 90 | 0 | ▶ | if(!defined $params) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $paged = $iron_action->{'paged'} ? $iron_action->{'paged'} : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | ▶ | my $per_page = $iron_action->{'per_page'} ? $iron_action->{'per_page'} : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | ▶ | my $log_message = $iron_action->{'log_message'} ? $iron_action->{'log_message'} : q{}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | ▶ | my $request_fields = $iron_action->{'request_fields'} ? $iron_action->{'request_fields'} : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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}) { | |||||||||
| 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}) { | |||||||||
| 124 | 0 | my $field_name = $request_fields->{$key}; | |||||||||
| 125 | 0 | ▶ | my $field_value = $params->{'body'}->{$key} ? $params->{'body'}->{$key} : q{}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_assert_configuration: not called CC=1 SCAR=6.9
| |||||||||||
| 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 | 7 | 1; | |||||||||
| 156 | |||||||||||
| 157 | __END__ | ||||||||||