Hi guys,
I am breaking my head around the following:
In my app users have the option to connect their woocommerce site.
Process:
- Users enters their url in form and submit form
- My WoocommerceAuth service class generates an auth url and redirect the user to it
- User allows access trough the auth url and returns back to my app
- The external api sends a response to the callback of my app, here I will save the users consumer_key and consumer_secret to my database (WooAccount table/model).
- Whenever a new WooAccount is created, I will dispatch a job for each part like SyncOrders, SyncProducts.
// This all works fine
Within each job I am doing this right now:
public function __construct($wooAccount)
{
$this->wooAccount = $wooAccount;
$woocommerce = new Client(
$wooAccount->store->url,
$wooAccount->consumer_key,
$wooAccount->consumer_secret,
[
'wp_api' => true,
'version' => 'wc/v3',
'verify_ssl' => false
]
);
$this->woocommerce = $woocommerce;
}
It works, but it is not quite DRY..
How could I improve this and easy instantiate a new api client connection each time without repeating myself? All the external api tutorials/github packages I checked are using a service provider. All these only use 1 api connection set by the app owner and not by the user himself.
I was thinking about doing something like this on my WooAccount model:
public function wooApi()
{
return new Client($this->store_id, $this->consumerKey, $this->consumer_secret, [
'wp_api' => true,
'version' => 'wc/v3',
'verify_ssl' => false
]
);
}
Would love to get some feedback and help pointing me to the right direction.