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
No related branches found
No related tags found
No related merge requests found
......@@ -60,13 +60,16 @@ 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
$discovery = new Discovery(new Client(), $this->sessionManager);
$discovery = new Discovery(
$this->getGuzzleClient(
__METHOD__,
[
'filters' => $filters,
'options' => $options,
]
),
$this->sessionManager
);
$api = lcfirst($this->objectType);
$objects = $discovery
->{$api}()
......@@ -158,7 +161,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
$object = new $class($fields);
$discovery = new Discovery(
$this->getGuzzleClient($branch),
$this->getGuzzleClient(
__METHOD__,
[
'branch' => $branch,
'options' => $options,
]
),
$this->sessionManager
);
$api = lcfirst($this->objectType);
......@@ -209,7 +218,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
$object->setId($id);
$discovery = new Discovery(
$this->getGuzzleClient($branch),
$this->getGuzzleClient(
__METHOD__,
[
'branch' => $branch,
'options' => $options,
]
),
$this->sessionManager
);
$api = lcfirst($this->objectType);
......@@ -530,20 +545,32 @@ class ObjectClient implements ClientInterface, ObjectClientInterface {
*
* Currently, it adds the appropriate header if a branch is given.
*
* @param string|null $branch
* @param string $caller
* The name of the method that requested the client e.g. `create`, `update`
* 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
* The instantiated Guzzle client.
*/
protected function getGuzzleClient(string|null $branch): Client {
if ($branch === NULL) {
protected function getGuzzleClient(
string $caller,
array $options = []
): Client {
if (!isset($options['branch'])) {
return new Client();
}
// We use a middleware so that all requests sent by this client will have
// the header.
$branch = $options['branch'];
$handler = HandlerStack::create();
$handler->push(Middleware::mapRequest(
function (RequestInterface $request) use ($branch) {
......@@ -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]);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment