Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created May 18, 2010 13:07
Show Gist options
  • Save j1n3l0/404966 to your computer and use it in GitHub Desktop.
Save j1n3l0/404966 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# Author:: Nelo Onyiah (mailto:io1@sanger.ac.uk)
use JSON;
use REST::Client;
my $domain = 'localhost:3000';
my $user = 'user';
my $pass = 'pass';
#
# Generic helper function for handling the web requests to the repository.
sub request {
my ( $method, $url, $data ) = @_;
die "Method $method unknown when requesting URL $url"
unless $method =~ m/DELETE|GET|POST|PUT/;
my @args = $data if $data;
my $client = REST::Client->new( { host => "http://$user:$pass\@$domain" } );
# Set the Content-Type and call the method with @args
$client->addHeader( content_type => "application/json" );
$client->$method( $url, @args );
# A small update message
warn join( " ", $method, $url, '-', $client->responseCode ), "\n";
# Handle failures here -- only code 200 | 201 are OK
die "Bad HTTP response ", $client->responseCode
unless $client->responseCode =~ m/20[01]/;
return $client->responseContent;
}
##
## Scenarios:
## - GET, CREATE, UPDATE some allele(s)
## - CREATE a Targeting Vector
## - CREATE a ES Cell
## - DELETE created objects
##
#
# Get allele lists - first page
my $response = request( "GET", "alleles.json?page=1" );
my $alleles_ref = from_json($response);
my $first_allele = $alleles_ref->[0];
#
# GET first allele
# We provide a search method (for when you don't have the ID).
# See the extended example for details
$response = request( "GET", "alleles/$first_allele->{id}.json" );
my $allele = from_json($response);
#
# Create an allele
my %data = (
pipeline_id => 1,
mgi_accession_id => "MGI:123456",
project_design_id => 1,
cassette => "L1L2_gt2",
backbone => "L3L4_pZero_kan",
assembly => "NCBIM37",
chromosome => "1",
strand => "+",
design_type => "Knock Out",
design_subtype => "Frameshift",
homology_arm_start => 10,
homology_arm_end => 10000,
cassette_start => 50,
cassette_end => 500,
loxp_start => 600,
loxp_end => 700,
);
my $allele_json = to_json( { molecular_structure => \%data } );
$response = request( "POST", "alleles.json", $allele_json );
my $created_allele = from_json($response);
#
# Update an allele (for example: the one created above)
%data = ( subtype_description => "SOME NEW INFORMATION" );
$allele_json = to_json( { molecular_structure => \%data } );
$response = request( "PUT", "alleles/$created_allele->{id}.json", $allele_json );
my $updated_allele = from_json($response);
#
# CREATE a targeting vector (related to the above allele)
%data = (
molecular_structure_id => $created_allele->{id},
name => 'PGDGR001',
ikmc_project_id => 1,
);
my $targ_vec_json = to_json( { targeting_vector => \%data } );
$response = request( "POST", 'targeting_vectors.json', $targ_vec_json );
my $created_targ_vec = from_json($response);
#
# CREATE an ES Cell (related to the above targeting vector and allele)
%data = (
molecular_structure_id => $created_allele->{id},
targeting_vector_id => $created_targ_vec->{id},
name => 'EPD7890',
);
my $es_cell_json = to_json( { es_cell => \%data } );
$response = request( "POST", 'es_cells.json', $es_cell_json );
my $created_es_cell = from_json($response);
#
# Delete everything
request( "DELETE", "es_cells/$created_es_cell->{id}.json" );
request( "DELETE", "targeting_vectors/$created_targ_vec->{id}.json" );
request( "DELETE", "alleles/$created_allele->{id}.json" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment