I use Laravel as a web service that collect data from external websites using API.
Every website, has a different api_key and endpoint url with completely different code strategy logics for returning data.
class Website1 {
getBooks(){
//a logic for getting data.
}
}
Website2.php
class Website2 {
getBooks(){
//another logic for getting data.
}
}
Website.php Model
public function getBooks(){
//How to call Website classes getBooks method based on $this->name ?
}
For example, how to call Website classes getBooks methods in getBooks Model method based on $this->name ? And how to access apikey and endpoints?
Or where should I put these classes?
In the table, also hold the name of a class that can process that particular API format.
Do you access them all on the same frequency? If so, just get all the rows and loop through them, calling the relevant class and passing it the record which contains the endpoint and the credentials.
$sites = Website::get();
foreach($sites as $site) {
$website = new $site->name($site);
$website->getBooks;
}
change the name column to include the full namespace of the class
change the constructor of your class to accept the website details