CryptoTron-JsonHttp

 view release on metacpan or  search on metacpan

lib/CryptoTron/JsonHttp.pm  view on Meta::CPAN

            } elsif ($flag eq "False" && chk_hex_addr($addr) != 1) {
                $flag = "True"; 
            };
        };
        # Create the payload from the address.
        $payload = "\{\"address\":\"${addr}\",\"visible\":\"${flag}\"\}";
    };
    # Return the payload string.
    return $payload;    
};

# ---------------------------------------------------------------------------- #
# Subroutine json_data()                                                       #
# ---------------------------------------------------------------------------- #
sub json_data {
    # Assign the arguments to the local array.
    my ($args) = @_;
    # Set the local variables.
    my $outfmt = (defined $args->{OutputFormat} ? $args->{OutputFormat} : "RAW");
    # Get the name of the calling module.   
    my $module_name = $args->{ModuleName};
    # Get the payload string.
    my $payload = $args->{PayloadString};
    # Get service url and related method.
    my $service_url = $API_URL.$SERVICES{$module_name}[0];
    my $method = $SERVICES{$module_name}[1];
    my $content = "";
    # Initialise the return variable.
    my $output_data = "{}";
    # Get the content from the service url.
    ($content, undef, undef, undef) = HTTP_Request($service_url, $method, $payload);
    # Format the content for the output.
    $output_data = format_output($content, $outfmt);
    # Return the JSON data raw or formatted.
    return $output_data;
};

# ---------------------------------------------------------------------------- #
# Subroutine format_output()                                                   #
# ---------------------------------------------------------------------------- #
sub format_output {
    # Assign the subroutine arguments to the local variables.
    my ($content, $outflag) = @_;
    # Declare the return variable.
    my $output;
    # Format the content for the output.
    if ($outflag eq "RAW") {
        # Use the content as it is. 
        $output = $content;
    } else {
        # Encode the content.
        $output = encode_data($content);
    };
    # Return formatted output.
    return $output;
};
# ---------------------------------------------------------------------------- #
# Subroutine encode_data()                                                     #
#                                                                              #
# Description:                                                                 # 
# At first glance, it is not obvious why the response should be decoded and    #
# then encoded back again. However, if one assumes that the response can have  #
# any structure, this procedure makes sense. Decoding creates a Perl data      #
# structure from the response. Encoding then creates a formatted string from   #
# the Perl data structure.                                                     #
#                                                                              #
# @argument $content  Content from response      (scalar)                      #
# @return   $encoded  Encoded formatted content  (scalar)                      #
# ---------------------------------------------------------------------------- #
sub encode_data {
    # Assign the subroutine argument to the local variable.
    my $content = $_[0];
    # Decode the content of the response using 'JSON:PP'.
    my $decoded = $JSON->decode($content);
    # Encode the content of the response using 'JSON:PP'.
    my $encoded = $JSON->encode($decoded);
    # Return the encoded formatted JSON content.
    return $encoded;
};

# ---------------------------------------------------------------------------- #
# Subroutine HTTP_Request()                                                    # 
#                                                                              #
# Description:                                                                 #
# The subroutine is using the HTTP methods GET or POST to send a request to a  #
# known servive url of the FULL-NODE HTTP API. On success a content in form of #
# JSON data is returned.                                                       #
#                                                                              #
# @argument $service_url  Service url       (scalar)                           #
# @return   $content      Response content  (scalar)                           #
# ---------------------------------------------------------------------------- #
sub HTTP_Request {
    # Assign the subroutine arguments to the local variables.
    my ($service_url, $method, $payload) = @_;
    # Initialise the local variables.
    my $content = "";
    my $response = "";
    my $errcode = "";
    my $errmsg = "";
    # Create a new uri object from the service url.
    my $uri = URI->new($service_url);
    # Create a new user agent object.
    my $ua = LWP::UserAgent->new();
    # Set the default header of the request.
    $ua->default_header('Accept' => 'application/json');
    $ua->default_header('Content_Type' => 'application/json');
    # Get the response from the uri based on the given HTTP method.
    if ($method eq 'POST') {
        $response = $ua->post($uri, 'Content' => $payload);
    } elsif ($method eq 'GET') {
        $response = $ua->get($uri, 'Content' => $payload);
    };
    # Get error code and error message.
    $errcode = $response->code;
    $errmsg = $response->message;
    # Check success of operation.
    if ($response->is_success) {
        # Get the content from the response.
        $content = $response->content;
    } else {
        # Set the content to an empty string.
        $content = "";
    };
    # Return content, error code, error message and service url.
    return ($content, $errcode, $errmsg, $service_url);
};

1;

__END__

=head1 NAME

CryptoTron::JsonHttp - Perl extension for use with the blockchain of the crypto coin Tron.

=head1 SYNOPSIS



( run in 0.899 second using v1.01-cache-2.11-cpan-4ef0a570458 )