Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created May 28, 2010 00:17
Show Gist options
  • Save j1n3l0/416564 to your computer and use it in GitHub Desktop.
Save j1n3l0/416564 to your computer and use it in GitHub Desktop.
package HTGT::REST;
use Moose;
use Moose::Util::TypeConstraints;
use JSON;
use Readonly;
use REST::Client;
use URI;
# ABSTRACT: a RESTful interface for HTGT
=head1 SYNOPSIS
use HTGT::REST;
$hr = HTGT::REST->new(%options);
$hr->get_allele($id);
=cut
has [qw/ domain pass protocol user /] => ( is => 'ro', required => 1 );
sub host {
my $self = shift;
return URI->new( sprintf '%s://%s:%s@%s',
$self->protocol, $self->user, $self->pass, $self->domain )->as_iri;
}
Readonly::Array my @REST_METHODS => qw/ GET /;
has client => (
lazy_build => 1,
isa => duck_type( [@REST_METHODS] ),
handles => [@REST_METHODS],
);
sub get_allele {
my ( $self, $id ) = @_;
return from_json( $self->GET("alleles/$id.json") );
}
around @REST_METHODS => sub {
my ( $orig, $self, @args ) = @_;
my $response = $self->$orig(@args);
confess "bad response code" unless $response->responseCode =~ m/20[01]/;
return $response->responseContent;
};
sub _build_client {
my $self = shift;
my $client = REST::Client->new;
$client->setHost( $self->host );
$client->addHeader( content_type => 'application/json' );
return $client;
}
__PACKAGE__->meta->make_immutable();
no Moose;
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment