Skip to content

Example Perl Client

Andrew Yates edited this page Jun 16, 2014 · 6 revisions
use strict;
use warnings;
use HTTP::Tiny;
use Time::HiRes qw/sleep/;
use JSON qw/decode_json/;

my $server = 'http://rest.ensembl.org/';
my $ping_endpoint = '/info/ping';
my $url = $server.$ping_endpoint;
my $headers = { accept => 'application/json' };

my $response = rest_request($url, $headers);
my $status = ($response->{ping}) ? 'up' : 'down';
print "Service is ${status}\n";

sub rest_request {
  my ($url, $headers, $attempts) = @_;
  $attempts //= 0;
  my $http = HTTP::Tiny->new();
  my $response = $http->get($url, {headers => $headers});
  if($response->{success}) {
    my $content = $response->{content};
    my $json = decode_json($content);
    return $json;
  }
  $attempts++;
  my $reason = $response->{reason};
  if($attempts > 3) {
    warn 'Failure with request '.$reason;
    die "Attempted to submit the URL $url more than 3 times without success";
  }
  my $response_code = $response->{status};
  # we were rate limited
  if($response_code == 429) {
    my $sleep_time = $response->{headers}->{'retry-after'};
    sleep($sleep_time);
    return rest_request($url, $headers, $attempts);
  }

  die "Cannot do request because of HTTP reason: '${reason}' (${response_code})";
}

Clone this wiki locally