Skip to content

Commit

Permalink
Fix merging pending requests
Browse files Browse the repository at this point in the history
  • Loading branch information
firstred committed Apr 29, 2021
1 parent b242eab commit 0873386
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 30 deletions.
21 changes: 20 additions & 1 deletion src/HttpClient/BaseHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use function array_push;
use function is_null;
use function max;
use function user_error;
use const E_USER_DEPRECATED;

abstract class BaseHttpClient
{
Expand Down Expand Up @@ -263,10 +265,27 @@ public function clearRequests()
* @param RequestInterface[] $requests
*
* @return HttpClientException[]|ResponseInterface[]
*
* @throws InvalidArgumentException
*/
public function doRequests($requests = [])
{
$requests = array_merge($this->pendingRequests, $requests);
if ($requests instanceof RequestInterface) {
user_error(
'Passing a single request to HttpClientInterface::doRequests is deprecated',
E_USER_DEPRECATED
);
$requests = [$requests];
}
if (!is_array($requests)) {
throw new InvalidArgumentException('Invalid requests array passed');
}
if (!is_array($this->pendingRequests)) {
$this->pendingRequests = [];
}

// Handle pending requests as well
$requests = $this->pendingRequests + $requests;

$responses = [];
foreach ($requests as $id => $request) {
Expand Down
23 changes: 23 additions & 0 deletions src/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@
namespace Firstred\PostNL\HttpClient;

use Composer\CaBundle\CaBundle;
use Exception;
use Firstred\PostNL\Exception\ApiConnectionException;
use Firstred\PostNL\Exception\ApiException;
use Firstred\PostNL\Exception\HttpClientException;
use Firstred\PostNL\Exception\InvalidArgumentException;
use GuzzleHttp\Psr7\Message as PsrMessage;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LogLevel;
use function define;
use function defined;
use function is_array;
use function user_error;
use const CURLOPT_FOLLOWLOCATION;
use const CURLOPT_HTTPHEADER;
use const CURLOPT_PROTOCOLS;
use const CURLOPT_REDIR_PROTOCOLS;
use const CURLOPT_SSL_VERIFYPEER;
use const E_USER_DEPRECATED;

if (!defined('CURL_SSLVERSION_TLSv1')) {
define('CURL_SSLVERSION_TLSv1', 1);
Expand Down Expand Up @@ -136,12 +141,30 @@ public function doRequest(RequestInterface $request)
* @param RequestInterface[] $requests
*
* @return ResponseInterface[]|HttpClientException[]
*
* @throws InvalidArgumentException
*/
public function doRequests($requests = [])
{
if ($requests instanceof RequestInterface) {
user_error(
'Passing a single request to HttpClientInterface::doRequests is deprecated',
E_USER_DEPRECATED
);
$requests = [$requests];
}
if (!is_array($requests)) {
throw new InvalidArgumentException('Invalid requests array passed');
}
if (!is_array($this->pendingRequests)) {
$this->pendingRequests = [];
}

// Reset request headers array
$curlHandles = [];
$mh = curl_multi_init();

// Handle pending requests as well
$requests = $this->pendingRequests + $requests;
foreach ($requests as $uuid => $request) {
$curl = curl_init();
Expand Down
26 changes: 19 additions & 7 deletions src/HttpClient/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
namespace Firstred\PostNL\HttpClient;

use Composer\CaBundle\CaBundle;
use Exception;
use Firstred\PostNL\Exception\HttpClientException;
use Firstred\PostNL\Exception\InvalidArgumentException;
use Firstred\PostNL\Exception\ResponseException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
Expand All @@ -45,7 +47,10 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use function is_array;
use function method_exists;
use function user_error;
use const E_USER_DEPRECATED;

/**
* Class GuzzleClient.
Expand Down Expand Up @@ -247,19 +252,26 @@ public function doRequest(RequestInterface $request)
* @param RequestInterface[] $requests
*
* @return HttpClientException[]|ResponseInterface[]
*
* @throws InvalidArgumentException
*/
public function doRequests($requests = [])
{
// If this is a single request, create the requests array
if (!is_array($requests)) {
if (!$requests instanceof RequestInterface) {
return [];
}

if ($requests instanceof RequestInterface) {
user_error(
'Passing a single request to HttpClientInterface::doRequests is deprecated',
E_USER_DEPRECATED
);
$requests = [$requests];
}
if (!is_array($requests)) {
throw new InvalidArgumentException('Invalid requests array passed');
}
if (!is_array($this->pendingRequests)) {
$this->pendingRequests = [];
}

// Handle pending requests
// Handle pending requests as well
$requests = $this->pendingRequests + $requests;
$this->clearRequests();

Expand Down
26 changes: 22 additions & 4 deletions src/HttpClient/HTTPlugClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Firstred\PostNL\Exception\HttpClientException;
use Firstred\PostNL\Exception\InvalidArgumentException;
use Firstred\PostNL\Util\EachPromise;
use GuzzleHttp\Psr7\Message as PsrMessage;
use Http\Client\Exception\HttpException;
Expand All @@ -22,6 +23,9 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use function is_array;
use function user_error;
use const E_USER_DEPRECATED;

/**
* Class HTTPlugClient.
Expand Down Expand Up @@ -100,15 +104,29 @@ public function __construct(
*
* Exceptions are captured into the result array
*
* @param array $requests
* @param RequestInterface[] $requests
*
* @psalm-param array<string, RequestInterface> $requests
* @return HttpClientException[]|ResponseInterface[]
*
* @return array
* @throws InvalidArgumentException
*/
public function doRequests($requests = [])
{
// Handle pending requests
if ($requests instanceof RequestInterface) {
user_error(
'Passing a single request to HttpClientInterface::doRequests is deprecated',
E_USER_DEPRECATED
);
$requests = [$requests];
}
if (!is_array($requests)) {
throw new InvalidArgumentException('Invalid requests array passed');
}
if (!is_array($this->pendingRequests)) {
$this->pendingRequests = [];
}

// Handle pending requests as well
$requests = $this->pendingRequests + $requests;
$this->clearRequests();

Expand Down
26 changes: 19 additions & 7 deletions src/HttpClient/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

namespace Firstred\PostNL\HttpClient;

use Exception;
use Firstred\PostNL\Exception\HttpClientException;
use Firstred\PostNL\Exception\InvalidArgumentException;
use Firstred\PostNL\Exception\ResponseException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
Expand All @@ -38,6 +40,9 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LogLevel;
use function is_array;
use function user_error;
use const E_USER_DEPRECATED;

/**
* Class MockClient.
Expand Down Expand Up @@ -182,19 +187,26 @@ public function doRequest(RequestInterface $request)
* @param RequestInterface[] $requests
*
* @return ResponseInterface[]|HttpClientException[]
*
* @throws InvalidArgumentException
*/
public function doRequests($requests = [])
{
// If this is a single request, create the requests array
if (!is_array($requests)) {
if (!$requests instanceof RequestInterface) {
return [];
}

if ($requests instanceof RequestInterface) {
user_error(
'Passing a single request to HttpClientInterface::doRequests is deprecated',
E_USER_DEPRECATED
);
$requests = [$requests];
}
if (!is_array($requests)) {
throw new InvalidArgumentException('Invalid requests array passed');
}
if (!is_array($this->pendingRequests)) {
$this->pendingRequests = [];
}

// Handle pending requests
// Handle pending requests as well
$requests = $this->pendingRequests + $requests;
$this->clearRequests();

Expand Down
22 changes: 22 additions & 0 deletions src/HttpClient/SymfonyHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
namespace Firstred\PostNL\HttpClient;

use Composer\CaBundle\CaBundle;
use Exception;
use Firstred\PostNL\Exception\HttpClientException;
use Firstred\PostNL\Exception\InvalidArgumentException;
use Firstred\PostNL\Exception\NotSupportedException;
use GuzzleHttp\Psr7\Message as PsrMessage;
use Psr\Http\Message\RequestInterface;
Expand All @@ -45,6 +47,9 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface as SymfonyHttpClientResponseInterface;
use function array_merge;
use function is_array;
use function user_error;
use const E_USER_DEPRECATED;

/**
* Class SymfonyHttpClientInterface.
Expand Down Expand Up @@ -231,9 +236,26 @@ public function doRequest(RequestInterface $request)
* @param RequestInterface[] $requests
*
* @return HttpClientException[]|ResponseInterface[]
*
* @throws InvalidArgumentException
*/
public function doRequests($requests = [])
{
if ($requests instanceof RequestInterface) {
user_error(
'Passing a single request to HttpClientInterface::doRequests is deprecated',
E_USER_DEPRECATED
);
$requests = [$requests];
}
if (!is_array($requests)) {
throw new InvalidArgumentException('Invalid requests array passed');
}
if (!is_array($this->pendingRequests)) {
$this->pendingRequests = [];
}

// Handle pending requests as well
$requests = $this->pendingRequests + $requests;
$httpClient = $this->getClient();
$responses = [];
Expand Down
1 change: 1 addition & 0 deletions src/PostNL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,7 @@ function ($barcode) {
* @throws PsrCacheInvalidArgumentException
* @throws ResponseException
* @throws NotFoundException
* @throws ShipmentNotFoundException
*
* @since 1.2.0
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Service/BarcodeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function generateBarcodeSOAP(GenerateBarcode $generateBarcode)
* @throws HttpClientException
* @throws ResponseException
* @throws InvalidConfigurationException
* @throws \Firstred\PostNL\Exception\InvalidArgumentException
*
* @since 1.0.0
*/
Expand Down Expand Up @@ -176,6 +177,7 @@ public function generateBarcodesREST(array $generateBarcodes)
* @throws CifException
* @throws HttpClientException
* @throws ResponseException
* @throws \Firstred\PostNL\Exception\InvalidArgumentException
*
* @since 1.0.0
*/
Expand Down
1 change: 1 addition & 0 deletions src/Service/ConfirmingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public function confirmShipmentSOAP(Confirming $confirming)
* @throws CifException
* @throws HttpClientException
* @throws ResponseException
* @throws InvalidArgumentException
*
* @since 1.0.0
*/
Expand Down
1 change: 1 addition & 0 deletions src/Service/LabellingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public function generateLabelSOAP(GenerateLabel $generateLabel, $confirm = true)
* @throws HttpClientException
* @throws PsrCacheInvalidArgumentException
* @throws ResponseException
* @throws PostNLInvalidArgumentException
*
* @since 1.0.0
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ public static function getShippingDaysRemaining($shippingDate, $preferredDeliver
* @throws Exception
* @throws Exception
* @throws Exception
* @throws Exception
* @throws Exception
* @throws Exception
* @throws Exception
*/
protected static function getHolidaysForYear($year)
{
Expand Down
23 changes: 12 additions & 11 deletions tests/Service/DeliveryDateServiceRestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,18 @@ public function testGetDeliveryDateRequestRest()

$query = Query::parse($request->getUri()->getQuery());

$this->assertEquals([
'ShippingDate' => '29-06-2016 14:00:00',
'ShippingDuration' => '1',
'CountryCode' => 'NL',
'Options' => 'Daytime',
'CutOffTime' => '14:00:00',
'PostalCode' => '2132WT',
'City' => 'Hoofddorp',
'HouseNr' => '42',
'HouseNrExt' => 'A',
],
$this->assertEquals(
[
'ShippingDate' => '29-06-2016 14:00:00',
'ShippingDuration' => '1',
'CountryCode' => 'NL',
'Options' => 'Daytime',
'CutOffTime' => '14:00:00',
'PostalCode' => '2132WT',
'City' => 'Hoofddorp',
'HouseNr' => '42',
'HouseNrExt' => 'A',
],
$query
);
$this->assertEquals('test', $request->getHeaderLine('apikey'));
Expand Down

0 comments on commit 0873386

Please sign in to comment.