Skip to content

Instantly share code, notes, and snippets.

@brendanc
Last active December 19, 2015 07:59
Show Gist options
  • Select an option

  • Save brendanc/5922847 to your computer and use it in GitHub Desktop.

Select an option

Save brendanc/5922847 to your computer and use it in GitHub Desktop.
Litmus api example with a create followed by a get.
#!/usr/bin/perl
use 5.006;
use SOAP::Lite 'trace', 'debug' ;
# Sample code to create a new email test using Litmus' Reseller Api
# This sample uses SOAP:Lite, available at: http://www.soaplite.com/
$api_key = "api-key";
$api_password = "api-password";
$api_ns = "https://soapapi.litmusapp.com/";
$api_types = "https://soapapi.litmusapp.com/encodedTypes";
$api_url = "https://soapapi.litmusapp.com/2010-06-21/api.asmx";
#====================
# Creates a new Litmus email test and returns the soap test object.
#====================
sub create_test
{
my $server= SOAP::Lite
-> readable(1)
-> ns($api_types,'types')
-> ns($api_ns,'tns')
-> proxy($api_url)
-> on_action(sub { return "\"$_[1]\"" }) ;
$user = SOAP::Data->name("apiKey"=>$api_key);
$pass = SOAP::Data->name("apiPass"=>$api_password);
$gmailClient = SOAP::Data->name("item" => \SOAP::Data->value(
SOAP::Data->name("ApplicationName" => "GMAILNEW")
))->type("types:Client");
$outlook2003Client = SOAP::Data->name("item" => \SOAP::Data->value(
SOAP::Data->name("ApplicationName" => "OL2003")
))->type("types:Client");
my @clients = ();
push(@clients,$gmailClient);
push(@clients,$outlook2003Client);
$testParam = SOAP::Data->name("emailTest" => \SOAP::Data->value(
SOAP::Data->name("TestType" => 'email'),
SOAP::Data->name("Sandbox")->value('true')->type('boolean'),
SOAP::Data->name("Results" => \@clients)->type("soapenc:Array")->attr({"soapenc:arrayType","types:Client[".@clients."]"})
));
my $newTest = $server -> CreateEmailTest($user,$pass,$testParam);
#print "Test created! Send an email to " . $newTest->valueof("//InboxGUID/") . "\@emailtests.com\n";
return $newTest;
}
#====================
# Retrieves a Litmus email test, takes the test id as an argument.
#====================
sub get_test{
my $server= SOAP::Lite
-> readable(1)
-> ns($api_types,'types')
-> ns($api_ns,'tns')
-> proxy($api_url)
-> on_action(sub { return "\"$_[1]\"" }) ;
$user = SOAP::Data->name("apiKey"=>$api_key);
$pass = SOAP::Data->name("apiPass"=>$api_password);
$testId = SOAP::Data->name("emailTestID"=>$_[0]);
my $retrieveTest = $server -> GetEmailTest($user,$pass,$testId);
return $retrieveTest;
}
#====================
# Entry point for this example script
#====================
sub do_it(){
my $test = create_test();
my $sendToAddress = $test->valueof("//InboxGUID/") . "\@emailtests.com";
my $testId = $test->valueof("//ID/");
my $retrievedTest = get_test($testId);
my $state = $retrievedTest->valueof("//State/");
while($state eq "waiting"){
print "state of test " . $testId ." is " . $state . "\n";
print "send your message to " . $sendToAddress . "\n";
print "sleeping 10 seconds then will try GetEmailTest again\n";
sleep(10);
# refetch test to see if the state has changed
$retrievedTest = get_test($testId);
$state = $retrievedTest->valueof("//State/");
}
print "Litmus got your email and is processing your test now....\n";
}
do_it();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment