Viewing file: client_example.pl (3.68 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
#!/usr/bin/perl
# This is a very simple example of a Perl client script interacting with Authority HTTP Server.
$| = 42; use strict; use warnings; use Getopt::Long; use LWP::UserAgent;
our @COMMANDS = qw( ping mon max-message-size license microupdate-versions supported-attributes runtime message content sender analysis ip);
main();
sub main { GetOptions('h|help' => sub { usage(); },) or usage(); usage() if !@ARGV; my ($server, $command, $parameter) = @ARGV;
# validate server URL my $commands_str = join('|', @COMMANDS); my $commands_regex = qr|$commands_str|;
usage("Server URL is required") if !$server || $server !~ /^https?:\/\/[-\w\.]+:\d+$/; usage("Command is required") if !$command || $command !~ /^($commands_regex)$/;
# validate optional parameter based on the detected API command my ($message, $analysis, $ip); if ($command eq 'message' || $command eq 'content' || $command eq 'sender') { # read mesage from file usage("Parameter for command '$command' is required to be filename containing message to score") if !$parameter || !-s $parameter; open my $fh, '<', $parameter or die "Error reading file $parameter: $!\n"; local $/ = undef; $message = <$fh>; close $fh; } elsif ($command eq 'analysis') { # analysis string usage("Parameter for command '$command' is required to be an analysis string to score") if !$parameter; print "Using analysis string $parameter\n"; $analysis = $parameter; } elsif ($command eq 'ip') { # IP address usage("Parameter for command '$command' is required to be an IP address to score") if !$parameter || $parameter !~ /^\d{1,3}(?:\.\d{1,3}){3}$/; print "Using IP address $parameter\n"; $ip = $parameter; }
# create user agent object my $ua = new LWP::UserAgent;
# send request to server my $response; my $request = $server . "/score/v2/" . $command; print "Request: " . $request . "\n"; if ($message || $analysis) { # send a POST request my $form; if ($message) { $form = { rfc822 => $message }; } elsif ($analysis) { $form = { analysis => $analysis }; } # Note that we are using multipart/form-data content type, # to comply with Authority HTTP Server's handling of form uploads. # See RFC2388 (http://www.ietf.org/rfc/rfc2388.txt) for more info. $response = $ua->post($request, Content_Type => 'form-data', Content => $form); } elsif ($ip) { # send a GET request $request .= "/$ip"; $response = $ua->get($request); } else { # send a GET request $response = $ua->get($request); }
# parse response; if successful, decode json and dump to screen if (!$response->is_success) { die "Server error: " . $response->status_line . "\n"; } my $content = $response->decoded_content;
print $content . "\n"; }
sub usage { my ($msg) = @_; if ($msg) { print "$msg\n"; print "See $0 --help\n"; } else { my $commands_p = join('', map { " $_\n" } @COMMANDS); print <<EOF; Usage: $0 [-h|help] <server> command {parameter}
Server URL must be in this format: http://127.0.0.1:2780
Valid commands: $commands_p Parameter is required for message, contente, sender, analysis and ip commands. For message, content, and sender, parameter must be filename containing mailbox with one message (supporting larger mailboxes is left as an excercise for the user) For analysis, parameter is the analysis string to score For ip, parameter is the IP address to score EOF } exit; }
|