Skip to content

Commit

Permalink
Merge pull request #105 from garthbrantley/feat-middleware
Browse files Browse the repository at this point in the history
Allow passing addl. Guzzle middleware in to GuzzleClient in createDefaultClient().
  • Loading branch information
bakura10 authored Feb 23, 2020
2 parents 6283dcc + c447ea5 commit b30b858
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.2.1

* Extend ShopifyClient to allow passing in array of middleware to be added to GuzzleClient handler stack.

# 6.2.0

* Add metafields methods for blog, collection, draft order resources
Expand Down
12 changes: 8 additions & 4 deletions src/ShopifyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ class ShopifyClient
* @param array $connectionOptions
* @param GuzzleClient|null $guzzleClient
*/
public function __construct(array $connectionOptions, GuzzleClient $guzzleClient = null)
public function __construct(array $connectionOptions, GuzzleClient $guzzleClient = null, array $guzzleMiddleware = [])
{
$this->validateConnectionOptions($connectionOptions);
$this->connectionOptions = $connectionOptions;

$this->guzzleClient = $guzzleClient ?? $this->createDefaultClient();
$this->guzzleClient = $guzzleClient ?? $this->createDefaultClient($guzzleMiddleware);
}

/**
Expand Down Expand Up @@ -571,13 +571,17 @@ private function validateConnectionOptions(array $connectionOptions)
/**
* @return GuzzleClient
*/
private function createDefaultClient(): GuzzleClient
private function createDefaultClient(array $guzzleMiddleware = []): GuzzleClient
{
$baseUri = 'https://' . str_replace('.myshopify.com', '', $this->connectionOptions['shop']) . '.myshopify.com';

$handlerStack = HandlerStack::create(new CurlHandler());
$handlerStack->push(Middleware::retry([$this, 'retryDecider'], [$this, 'retryDelay']));

foreach ($guzzleMiddleware as $curMiddleware) {
$handlerStack->push($curMiddleware);
}

$httpClient = new Client(['base_uri' => $baseUri, 'handler' => $handlerStack]);
$description = new Description(require __DIR__ . '/ServiceDescription/Shopify-v1.php');

Expand Down Expand Up @@ -654,7 +658,7 @@ private function iterateResources(string $commandName, array $args): Generator

// Advance the since_id
$args['since_id'] = end($results)['id'];
} while(count($results) >= 250);
} while (count($results) >= 250);
}
}

Expand Down

0 comments on commit b30b858

Please sign in to comment.