Sample Perl Script: Full API Request

Creation date: 6/11/2022 11:12 PM    Updated: 6/11/2022 11:13 PM
This is an example of how a script can use the Angelfish API. In order to simplify the example, most error handling has been omitted. If you are going to use this example as a template, please add appropriate error handling.
 
use strict;
use warnings;
use Data::Dumper;
use Digest::HMAC_MD5;
use HTTP::Request::Common qw(POST);
use JSON;
use LWP::UserAgent;
#------------------------------------------------------------------------------#
# The following variables should be changed to match your environment. #
#------------------------------------------------------------------------------#
# the url to your Angelfish server
my $agf_api_url = 'http://ANGELFISHSERVER:9000/';
# the Angelfish user to access data with
my $agf_api_user = 'somedude';
# the Angelfish user's password
my $agf_api_pass = 'itsasecret';
# the profile ids we want to request data from
my @ids = ('1','2');
# the data request parameters
my $request_data = {
 'username' => $agf_api_user, # username
 'start-time' => '20141225', # start-time in YYYYMMDD format
 'end-time' => '20141228', # end-time in YYYYMMDD format
 'dimensions' => 'source,medium', # comma separated dimensions
 'metrics' => 'visits,pageviews', # comma separated metrics
 'sort' => '-visits,source,medium', # comma separated sorting order
 'filters' => 'source=~google,visits>5', # comma separated filters
 'format' => 'json' # format for returned data. Options: json, xml, csv, tsv, jsonfields
};
#------------------------------------------------------------------------------#
# The above variables should be changed to match your environment. #
#------------------------------------------------------------------------------#
# Get an Angelfish authentication token for signing requests.
my $auth_token = GetAuthToken($agf_api_user, $agf_api_pass, $agf_api_url);
# For each profile id, lets request data and print it out.
foreach my $id (@ids) {
 
 # add the id to the request data
 $request_data->{ids} = $id;
 
 # add the signature to our request data
 $request_data->{signature} = GetSignature($request_data, $auth_token);
 
 # request the data
 my $data = RequestData($request_data, $agf_api_url);
 
 #----------------------------------------------------------------------------#
 # At this point, your data is in the form of a json string. This is where #
 # you would process the data for your needs. In this example we are simply #
 # parsing the json string into a perl hashref and dumping it. #
 #----------------------------------------------------------------------------#
 $data = decode_json($data);
 print Dumper($data);
 
 # remove the signature and id before signing and making another request
 delete $request_data->{ids};
 delete $request_data->{signature};
}
# revoke the token since we're done making requests with it
RevokeAuthToken($agf_api_user, $agf_api_url, $auth_token);
exit(0);

# Sub: RequestData
# Makes an Angelfish data api request with the provided request parameters and url.
#
# Args:
# $request - the request parameters
# $url - The Angelfish api url
#
# Returns: an array of report data
sub RequestData {
 my ($request, $url) = @_;
 
 # add the data api handler to the url
 $url .= 'api/data/'; # make the POST request
 my $ua = LWP::UserAgent->new;
 my $req = POST($url, [data => encode_json($request)]);
 my $resp = $ua->request($req); # return the requested data
 return $resp->content;
} # Sub: GetSignature
# Creates a signature for an Angelfish api request with the provided token.
#
# Args:
# $data - The data to get a signature for
# $token - The token to make the signature with
#
# Returns: The signature.
sub GetSignature {
 my ($data, $token) = @_;

# Extract and sort the data keys
 my @keys = sort(keys %{$data});

# Concatenate the key=value pairs into one string sorted by key.
 my $to_sign = '';
 foreach my $key (@keys) {
 $to_sign .= "$key=$data->{$key}";
 }
# Return a base 64 HMAC MD5 hash of the string.
 my $hmac = Digest::HMAC_MD5->new($token);
 $hmac->add($to_sign);
 return $hmac->b64digest;
}
# Sub: RevokeAuthToken
# Requests that an api authentication token be revoked from Angelfish using
# provided user, url, and token.
#
# Args:
# $user - The username to use when making the request
# $url - The Angelfish api url to make the request to
# $token - The auth token to revoke
#
# Returns: 1
sub RevokeAuthToken {
 my ($user, $url, $token) = @_;
 
 # add the revoke token api handler to the url
 $url .= 'api/token/revoke';
# parameters to use in the revoke request
 my $params = {username => $user};
# even though we're revoking the token, we still need to sign the request
 $params->{signature} = GetSignature($params,$token);
# make the POST to revoke the token
 my $ua = LWP::UserAgent->new;
 my $req = POST($url, [data => encode_json($params)]);
 $ua->request($req);
return 1;
}
# Sub: GetAuthToken
# Requests an api authentication token from Angelfish using provided user,
# password, and url.
#
# Args:
# $user - The username to use when requesting the token
# $pass - The password to use when requesting the token
# $url - The Angelfish api url to request the token from
#
# Returns: The Angelfish authentication token.
sub GetAuthToken {
 my ($user, $pass, $url) = @_;
 
 # add the token request api handler to the url
 $url .= 'api/token/request';
 
 my $params = {username => $user, password => $pass};
# make the POST for the token
 my $ua = LWP::UserAgent->new;
 my $req = POST($url, [data => encode_json($params)]);
 my $resp = $ua->request($req);
# return the token
 return decode_json($resp->content)->{token};
}