I have to program a generic client that uses diverse concrete clients (diverse APIs), all sharing the same interface. I have to log the response of the client.
To accomplish this (logging in background without user having to manually do it), I created these classes:
Request
, Response
, ClientService
, Client1
, Client2
, ClientInterface
.
So, you instantiate ClientService and set the client name, example:
$client = $this->get('clientService')->setClient('client1');
// $data is an associative array
$request = $client->createRequest($data);
// $response was saved to database by ClientService, before returning it.
$response = $client->doRequest($request);
ClientService
, Client1
and Client2
implements ClientInterface
, which has methods doRequest()
, createRequest()
, getError()
, etc, but all ClientService implementation does is to forward the request to the actual client.
I did this to be able to save the response to database automatically, and for this to happen without having to repeat code in all the clients, and to let this concern be in the service, it had to have control over the whole request/response lifecycle.
I wonder if there is a pattern that already describes this scenario.