Perl.gif

Previous: Supported Languages

Contents

64-warn.gif
NOTE: GoGrid Support WILL NOT troubleshoot customer-modified code. The code snippets and examples provided in this API documentation can be modified to customize your own code, but GoGrid Support cannot troubleshoot any errors you may receive due to missing syntax or illegal arguments. We WILL support our API interface and can troubleshoot any API server-side errors you may be receiving. Please note that all of the code snippets and examples provided in this API documentation have been thoroughly tested and verified as properly working.

Prerequisites

GoGridClient.pm

This is a perl module that provides the ability to formulate and submit a GoGrid API request.

package GoGridClient;

# Constructor that initializes all values.
sub new {
  my $class = shift;
  my $self  = {
    _format  => @_ ? shift : "xml",
    _server  => @_ ? shift : "https://api.gogrid.com/api",
    _apikey  => @_ ? shift : "YOUR API KEY",
    _secret  => @_ ? shift : "YOUR SHARED SECRET",
    _version => @_ ? shift : "1.0",

  };

  bless $self, $class;
  return $self;
}

# Construct a request URL for the GoGrid API based on method and parameters passed in
sub getRequestURL {
  use URI::Escape;

  # Initialize locals from arguments
  my ( $self, $method, %params ) = @_;

  # Create an array containing default parameter values
  %_params = (
    "api_key", $self->{_apikey},
    "v", $self->{_version},
    "format",  $self->{_format},
    "sig", $self->getSignature( $self->{_apikey}, $self->{_secret} )
  );

  # Merge in the passed in parameters, url encoding any values
  while ( ( $key, $value ) = each %params ) {
    $_params{$key} = uri_escape($value);
  }

  # Start building the URL using the server and method
  $url = $self->{_server} . "/" . $method . "?";
  
  # Append the parameter values to the URL
  while ( ( $key, $value ) = each %_params ) {
    $url .= "&$key=$value";
  }

  # Return the URL
  return $url;
}

# Generate request signature from key and secret members
sub getSignature {
  my ($self) = @_;

  use Digest::MD5;

  $key       = $self->{_apikey};
  $secret    = $self->{_secret};
  $timestamp = time();

  $md5 = Digest::MD5->new;
  $md5->add( $key, $secret, $timestamp );
  $digest = $md5->hexdigest;

  return $digest;
}

# This method sends the HTTP request and returns the response, handling any HTTP errors
sub sendAPIRequest {
  my ( $self, $url ) = @_;
  require LWP::UserAgent;

  my $ua = LWP::UserAgent->new;
  $ua->timeout(10);
  $ua->env_proxy;

  my $response = $ua->get($url);

  if ( $response->is_success ) {
    # If successful return the body of the response
    return $response->content;    # or whatever
  }
  else {
    # If failure return the error message
    return $response->status_line;
  }
}
1;

main.pl

This is an example perl script that uses the GoGridClient module to make calls against the GoGrid API.

use GoGridClient;

# Create a new GoGridClient object with the default values
$myObject = GoGridClient->new();

# Create the paramets to the method call
%params = ("format","json");

# Create the request for the server/list method
$requestURL = $myObject->getRequestURL("grid/server/list",%params);

# Print the request
print "\nRequest :\n$requestURL";

# Send the request
$results = $myObject->sendAPIRequest($requestURL);

#print the results
print "\n\nResponse :\n$results";

Previous: Supported Languages

Personal tools