Skip to content
Snippets Groups Projects
Commit 26245bb3 authored by Dimitris Bozelos's avatar Dimitris Bozelos
Browse files

Issue #3374278 Made it easy to override guzzle client creation

parent 91a5de20
Branches
Tags
No related merge requests found
...@@ -60,13 +60,16 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { ...@@ -60,13 +60,16 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
]); ]);
} }
// phpcs:disable $discovery = new Discovery(
// @I Allow injecting the Guzzle client for mocking or config purposes $this->getGuzzleClient(
// type : task __METHOD__,
// priority : low [
// labels : testing 'filters' => $filters,
// phpcs:enable 'options' => $options,
$discovery = new Discovery(new Client(), $this->sessionManager); ]
),
$this->sessionManager
);
$api = lcfirst($this->objectType); $api = lcfirst($this->objectType);
$objects = $discovery $objects = $discovery
->{$api}() ->{$api}()
...@@ -158,7 +161,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { ...@@ -158,7 +161,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
$object = new $class($fields); $object = new $class($fields);
$discovery = new Discovery( $discovery = new Discovery(
$this->getGuzzleClient($branch), $this->getGuzzleClient(
__METHOD__,
[
'branch' => $branch,
'options' => $options,
]
),
$this->sessionManager $this->sessionManager
); );
$api = lcfirst($this->objectType); $api = lcfirst($this->objectType);
...@@ -209,7 +218,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { ...@@ -209,7 +218,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
$object->setId($id); $object->setId($id);
$discovery = new Discovery( $discovery = new Discovery(
$this->getGuzzleClient($branch), $this->getGuzzleClient(
__METHOD__,
[
'branch' => $branch,
'options' => $options,
]
),
$this->sessionManager $this->sessionManager
); );
$api = lcfirst($this->objectType); $api = lcfirst($this->objectType);
...@@ -530,20 +545,32 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { ...@@ -530,20 +545,32 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
* *
* Currently, it adds the appropriate header if a branch is given. * Currently, it adds the appropriate header if a branch is given.
* *
* @param string|null $branch * @param string $caller
* The Acumatica branch to use for the header, or NULL to not include the * The name of the method that requested the client e.g. `create`, `update`
* branch header. * etc.
* @param array $options
* An array of options that may be used to determine the middleware that
* will be added to the client handler. This is not the same as, but may
* contain, the client options passed to the caller method. Supported
* options by the default implementation are:
* - branch (string, optional):
* The Acumatica branch to use for the header, or NULL to not include the
* branch header.
* *
* @return \GuzzleHttp\Client * @return \GuzzleHttp\Client
* The instantiated Guzzle client. * The instantiated Guzzle client.
*/ */
protected function getGuzzleClient(string|null $branch): Client { protected function getGuzzleClient(
if ($branch === NULL) { string $caller,
array $options = []
): Client {
if (!isset($options['branch'])) {
return new Client(); return new Client();
} }
// We use a middleware so that all requests sent by this client will have // We use a middleware so that all requests sent by this client will have
// the header. // the header.
$branch = $options['branch'];
$handler = HandlerStack::create(); $handler = HandlerStack::create();
$handler->push(Middleware::mapRequest( $handler->push(Middleware::mapRequest(
function (RequestInterface $request) use ($branch) { function (RequestInterface $request) use ($branch) {
...@@ -551,6 +578,12 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { ...@@ -551,6 +578,12 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
}) })
); );
// phpcs:disable
// @I Allow injecting the Guzzle client for mocking or config purposes
// type : task
// priority : low
// labels : testing
// phpcs:enable
return new Client(['handler' => $handler]); return new Client(['handler' => $handler]);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment