+ Start a Discussion
Ron HessRon Hess 

package Sfdc

Here is the primary routine to access the sforce api from perl, assumes you have the Sfdc::Serializer, I will send the full package to adam to attach.  Note the code to retry upon rate limit.

this is called to login, query, update ...

my $login = call('sfdc.login', username => $n, password => $p);

etc...

####################

sub call { my $method = shift;
 my %args = (client => $client,version => type(string => $sfdcversion));  
 my ($name,$value);
 while ($name = shift, $value=shift ) { %args = (%args, $name,$value); }
 
 # make xml string
 my $content = Sfdc::Serializer->method( $method => {%args} );
 my $ua = LWP::UserAgent->new; $ua->agent($client);
 my $req = POST $proxy;  
 
 # store the string into the user agent
 $req->content( $content );  

 # add a cookie header for SF.com to look at the session id
 if ( $session_id ) {  
  $req->push_header('Cookie',"sid=$session_id");
 }
 retry:
 # send request
 my $res = $ua->request($req);       
 $res->is_success or die "method $content failed \n";   
 # check for success
 my $result = XMLRPC::Deserializer->deserialize($res->content);
 # return SOM
 if ($result->fault) {
  # if 1010, we are asked to wait and re-try
  # parse the number mil seconds , wait and retry
  if ( $result->faultcode == 1010 ) {
   print "rate limit encountered \n";
   print $result->faultcode, " ",
    $result->faultstring, "\n";
   # pull off the number of ms to wait
   my $retry_time = $result->faultstring;
   $retry_time =~ s/^(\d+)ms delay.*$/$1/;
   print "retry time is $retry_time or ",
    $retry_time/1000," seconds\n";
   sleep (($retry_time/1000)+1) ; # 2 seconds is 2000 ms
   goto retry; # TODO, ouch, this can loop forever...
  }
  print $result->faultcode, " fault ",
   $result->faultstring, "\n";
   
 }
 if ( wantarray ) {
  if ( defined $result->result ) { return @{$result->result}; }
  else { return (); }
 }
 return $result;
}