IO-Iron

 view release on metacpan or  search on metacpan

lib/IO/Iron/Connector.pm  view on Meta::CPAN

            $log->tracef( 'perform_iron_action(): url_param:%s', $_ );
            if ( $params->{ '{' . $_ . '}' } ) {
                $url_params .= "$_={$_}&";
            }
        }
        $url_params = substr $url_params, 0, ( length $url_params ) - 1;
    }
    if ($url_params) {
        $href .= ( q{?} . $url_params );
    }
    my $content_type = $iron_action->{'content_type'};
    $params->{'content_type'} = $content_type;
    $params->{'return_type'}  = $return_type;
    $log->tracef( 'href before value substitution:\'%s\'.', $href );
    foreach my $value_key ( sort keys %{$params} ) {
        my $value = $params->{$value_key};
        $log->tracef( 'Param key:%s; value=%s;', $value_key, $value );
        $href =~ s/$value_key/$value/gs;
    }
    $log->tracef( 'href after value substitution:\'%s\'.', $href );

    my ( $http_status_code, $returned_msg );
    my $keep_on_trying = 1;
    while ($keep_on_trying) {
        $keep_on_trying = 0;
        try {
            assert(  ( $require_body == 1 && defined $params->{'body'} && ref $params->{'body'} eq 'HASH' )
                  || ( $require_body == 0 && !defined $params->{'body'} ) );
            assert_in(
                $action_verb,
                [ 'GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD' ],
                'action_verb is a valid HTTP verb.'
            );
            assert_nonblank( $params->{'{Protocol}'},          'params->{Protocol} is defined and not blank.' );
            assert_nonblank( $params->{'{Port}'},              'params->{Port} is defined and not blank.' );
            assert_nonblank( $params->{'{Host}'},              'params->{Host} is defined and not blank.' );
            assert_nonblank( $params->{'{Project ID}'},        'params->{Project ID} is defined and not blank.' );
            assert_nonblank( $params->{'{API Version}'},       'params->{API Version} is defined and not blank.' );
            assert_nonblank( $params->{'authorization_token'}, 'params->{authorization_token} is defined and not blank.' );
            assert_nonblank( $params->{'http_client_timeout'}, 'params->{http_client_timeout} is defined and not blank.' );
            my $url_escape_these_fields = defined $iron_action->{'url_escape'} ? $iron_action->{'url_escape'} : {};

            foreach my $field_name ( keys %{$url_escape_these_fields} ) {
                if ( defined $params->{$field_name} ) {
                    $params->{$field_name} = uri_escape_utf8( $params->{$field_name} );
                }
            }
            #
            if ($paged) {
                $log->debugf('A paged query.');
                my @returned_msgs;
                my ( $http_status_code_temp, $returned_msg_temp );
                my $page_number = 0;
                while (1) {
                    my $page_href = $href;
                    $log->debugf( 'A paged query. Href:\'%s\'', $page_href );
                    $page_href .= ( $href =~ /\?/gsx ? q{&} : q{?} ) . 'per_page=' . $per_page . '&page=' . $page_number;
                    ( $http_status_code_temp, $returned_msg_temp ) =
                      $self->perform_http_action( $action_verb, $page_href, $params );
                    my $return_list = $returned_msg_temp;
                    my ( $return_type_def, $list_hash_key ) = ( split m/:/s, $return_type );
                    $return_list = $returned_msg_temp->{$list_hash_key}
                      if $return_type_def eq 'LIST' && defined $list_hash_key;    ## no critic (ControlStructures::ProhibitPostfixControls)
                    push @returned_msgs, @{$return_list};

                    if ( scalar @{$return_list} == 0 || @{$return_list} < $per_page ) {
                        last;
                    }
                    $page_number++;
                }
                $http_status_code = $http_status_code_temp;
                $returned_msg     = \@returned_msgs;
            }
            else {
                ( $http_status_code, $returned_msg ) = $self->perform_http_action( $action_verb, $href, $params );
            }
        }
        catch {
            $log->debugf( 'perform_iron_action(): Caught exception:\'%s\'.', $_ );
            croak $_ unless blessed $_ && $_->can('rethrow');    ## no critic (ControlStructures::ProhibitPostfixControls)
            if ( $_->isa('IronHTTPCallException') ) {
                if ( $_->status_code == $HTTP_CODE_SERVICE_UNAVAILABLE ) {

                    # 503 Service Unavailable. Clients should implement exponential backoff to retry the request.
                    $keep_on_trying = 1 if ( $retry == 1 );    ## no critic (ControlStructures::ProhibitPostfixControls)
                                                               # TODO Fix this temporary solution for backoff to retry the request.
                }
                else {
                    $log->debugf( 'perform_iron_action(): rethrow the exception.', $_ );
                    $_->rethrow;
                }
            }
            else {
                $_->rethrow;
            }
        };

        # Module::Pluggable here?
    }
    $log->tracef( 'Exiting Connector:perform_iron_action(): %s', $returned_msg );
    return $http_status_code, $returned_msg;
}

sub perform_http_action {
    my ( $self, $action_verb, $href, $params ) = @_;
    my $client = $self->{'client'};
    my $json   = JSON::MaybeXS->new( utf8 => 1, pretty => 1 );

    # TODO assert href is URL
    assert_in( $action_verb, [ 'GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD' ], 'action_verb is a valid HTTP verb.' );
    assert_exists(
        $params,
        [ 'http_client_timeout', 'authorization_token' ],
        'params contains items http_client_timeout and authorization_token.'
    );
    assert_integer( $params->{'http_client_timeout'}, 'params->{\'http_client_timeout\'} is integer.' );
    assert_nonblank( $params->{'authorization_token'}, 'params->{\'authorization_token\'} is a non-blank string.' );
    $log->tracef( 'Entering Connector:perform_http_action(%s, %s, %s)', $action_verb, $href, $params );
    #
    # HTTP request attributes
    my $timeout = $params->{'http_client_timeout'};



( run in 0.594 second using v1.01-cache-2.11-cpan-71847e10f99 )