Tips on code structure for talking with external API
Hi
I am creating an API that will use Wordpress API and in future it will use our own DATABASE. For now & i think i did it wrong, Here is the logic:
I have a controller (SellerController) that acts on endpoints such as sellers, sellers/id etc...
Each Controller has a Repository (SellerRepository) that has some properties set such as Wordpress endpoint to use such as wp-json/wp/.... and an entity to tell it what entity to use (using Jessenger\Model) in this case Seller Entity.
Each Repository extends BASE repository, which has a function getDataFromAPI() which creates the endpoint/url, and then pass it to Wordpress wrapper I have created. (getDataFromApi i suppose should really be 'find/findall')
Wordpress API (Class) uses Guzzle to connect to the wordpress & returns the data.
Now I know Repositories are like a wrapper so we can switch the underlying database/or whatever we are getting data from. But At this point, I believe I will have to do lots of work when I switch from WP to using our own Database? For example:
Remove the redundant Wordpress endpoint API from each repository
Change from Jessenger\Model to using Eloquent Model, but this should be easy as we can create Eloquent Entities in the same folder and then be done with it
in the repository I say $this->api->findAll()... but how do i switch this to use something like Seller:find(id);...
For 3) you would just make your new eloquent repository version return the values but via the same calls. So say :
class EloquentReposity {
public function findAll()
{
return Seller::all();
}
public function find($id) {
return Seller::find($id);
}
}
Depending what you are expecting back of course. If your current code returns a plain array, or JSON data or... you'll either have to do the same with eloquent, or make your current code return a collection so it matches what you'll get 'natively' from laravel.
@alenabdula yeah the goal is to eliminate WP but it wil take time and the company wants to launch apps hence the API in Laravel. So when we are ready to move towards database, the API we created is there & we just 'change' key settings..
I could have taken few routes like using WP database in laravel (ie writing Models & relationships), I could have ofcourse created new database & wrote API for that and then have a 'logic/cron jobs' to periodically update it with data from Wordpress. but it would have taken time and we needed to launch the web apps within couple of weeks.
Now that I have done this, I could easily re-write the whole thing using a new db but i thought maybe for experience purposes it would be good to maybe change the current code slightly to make it work