Skip to content

Instantly share code, notes, and snippets.

@sb25
Created November 18, 2009 09:44
Show Gist options
  • Select an option

  • Save sb25/237685 to your computer and use it in GitHub Desktop.

Select an option

Save sb25/237685 to your computer and use it in GitHub Desktop.
use LWP::UserAgent;
use URI;
use JSON;
# Settings
my $url_base = 'http://user:password@localhost:3000';
my $browser = LWP::UserAgent->new;
# Helper method
sub request {
my ($method, $url, $data) = @_;
$method ||= 'GET';
my $request = HTTP::Request->new($method => $url_base.'/'.$url);
if ($method =~ /^(POST|PUT)$/) {
$request->content_type('application/json');
$request->content($data);
}
my $response = $browser->request($request);
if ($response->is_success) {
return $response->content;
}
else {
die $response->content."\n";
}
}
# Examples
# Get allele list - first page
my $alleles_as_json = request( 'GET', 'alleles.json?page=1' );
print from_json( $alleles_as_json );
# Get allele id 1 - no need to keep track of the ids a search method is
# provided in the extended example.
my $allele_as_json = request( 'GET', 'alleles/1.json' );
print from_json( $allele_as_json );
# Create an allele
my %data =
(
pipeline_id => 1,
mgi_accession_id => 'MGI:44557',
project_design_id => 10000,
cassette => 'L1L2_gt1',
backbone => 'L3L4_pZero_kan',
assembly => 'NCBIM37',
chromosome => '11',
strand => '+',
design_type => 'KO',
design_subtype => 'Frameshift',
homology_arm_start => 10,
homology_arm_end => 10000,
cassette_start => 50,
cassette_end => 500,
loxp_start => 1000,
loxp_end => 1500
);
my $allele_as_json = request( 'POST', 'alleles.json', to_json(data) );
print from_json( $allele_as_json );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment