goplutus Posted December 11, 2010 Share Posted December 11, 2010 Is there a good way to batch API calls? I'm building a service that requires n^2 api calls for each city I add. Naturally, this is getting a bit clunky (6 cities is 36 separate calls). I lieu of finding a better webservice that can consolidate this into one API call, is there a good way to batch these so I don't get fatal errors when one of these fails? Ideally, I'd be able to loop the calls until results are returned for each. Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted December 12, 2010 Share Posted December 12, 2010 Depends on the API. Why n^2? Could it be reduced to at least n^2/2? Quote Link to comment Share on other sites More sharing options...
ignace Posted December 12, 2010 Share Posted December 12, 2010 interface MyBatchService { public function someMethod(); } class SomeService1 implements MyBatchService { private $successor; public function __construct(MyBatchService $successor) { $this->successor = $successor; } public function someMethod() { if(!$this->canHandle()) { if($this->successor) return $this->successor->someMethod(); throw new Exception('Failed to handle the request'); } } } class SomeService2 implements MyBatchService { private $successor; public function __construct(MyBatchService $successor) { $this->successor = $successor; } public function someMethod() { if(!$this->canHandle()) { if($this->successor) return $this->successor->someMethod(); throw new Exception('Failed to handle the request'); } } } class SomeService3 implements MyBatchService { private $successor; public function __construct(MyBatchService $successor) { $this->successor = $successor; } public function someMethod() { if(!$this->canHandle()) { if($this->successor) return $this->successor->someMethod(); throw new Exception('Failed to handle the request'); } } } try { $batch = new SomeService1(new SomeService2(new SomeService3())); $batch->someMethod(); // initialize the chain } catch(Exception $e) { echo $e->getMessage(); } I assume each service returns the same information. The interface declares the common interface eg getUser(..) If a service fails to be able to handle the request (due to overload) it will try it's successor until the request is successfully handled or an exception is thrown. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.