Skip to content

Commit

Permalink
Intercept PendingRequest (#34)
Browse files Browse the repository at this point in the history
* implement 'body_format' setting for changing the bodyFormat of the requests.

* Revert "implement 'body_format' setting for changing the bodyFormat of the requests."

This reverts commit 81fd21b.

* implement the intecept method.

With this method you can change the headers and settings of the used PendingRequest object for the current cycle.

* Small adjustments on the interceptor, tests and SCA

* Adjust test

---------

Co-authored-by: Vincent Boon <[email protected]>
  • Loading branch information
mennotempelaar and VincentBean authored Jan 26, 2024
1 parent 981ad1b commit 09ac9de
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/Client/Magento.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JustBetter\MagentoClient\Client;

use Closure;
use Generator;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
Expand All @@ -16,6 +17,8 @@ class Magento

public ?string $storeCode = null;

public ?Closure $interceptor;

public function __construct(
protected BuildsRequest $request
) {
Expand All @@ -42,7 +45,7 @@ public function graphql(string $query, array $variables = []): Response
$endpoint = config("magento.connections.{$this->connection}.graphql_path");

/** @var Response $response */
$response = $this->request->build($this->connection)
$response = $this->request()
->when($this->storeCode !== null, fn (PendingRequest $request): PendingRequest => $request->withHeaders(['Store' => $this->storeCode]))
->post($endpoint, [
'query' => $query,
Expand All @@ -54,104 +57,103 @@ public function graphql(string $query, array $variables = []): Response

public function get(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->get($this->getUrl($path), $data);
$response = $this->request()->get($this->getUrl($path), $data);

return $response;
}

public function post(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->post($this->getUrl($path), $data);
$response = $this->request()->post($this->getUrl($path), $data);

return $response;
}

public function postAsync(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->post($this->getUrl($path, true), $data);
$response = $this->request()->post($this->getUrl($path, true), $data);

return $response;
}

public function postBulk(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->post($this->getUrl($path, true, true), $data);
$response = $this->request()->post($this->getUrl($path, true, true), $data);

return $response;
}

public function patch(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->patch($this->getUrl($path), $data);
$response = $this->request()->patch($this->getUrl($path), $data);

return $response;
}

public function patchAsync(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->patch($this->getUrl($path, true), $data);
$response = $this->request()->patch($this->getUrl($path, true), $data);

return $response;
}

public function patchBulk(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->patch($this->getUrl($path, true, true), $data);
$response = $this->request()->patch($this->getUrl($path, true, true), $data);

return $response;
}

public function put(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->put($this->getUrl($path), $data);
$response = $this->request()->put($this->getUrl($path), $data);

return $response;
}

public function putAsync(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->put($this->getUrl($path, true), $data);
$response = $this->request()->put($this->getUrl($path, true), $data);

return $response;
}

public function putBulk(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->put($this->getUrl($path, true, true), $data);
$response = $this->request()->put($this->getUrl($path, true, true), $data);

return $response;
}

public function delete(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->delete($this->getUrl($path), $data);
$response = $this->request()->delete($this->getUrl($path), $data);

return $response;
}

public function deleteAsync(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->delete($this->getUrl($path, true), $data);
$response = $this->request()->delete($this->getUrl($path, true), $data);

return $response;
}

public function deleteBulk(string $path, array $data = []): Response
{
/** @var Response $response */
$response = $this->request->build($this->connection)->delete($this->getUrl($path, true, true), $data);
$response = $this->request()->delete($this->getUrl($path, true, true), $data);

return $response;
}
Expand Down Expand Up @@ -206,6 +208,25 @@ public function getUrl(string $path, bool $async = false, bool $bulk = false): s
return implode('/', $options);
}

public function intercept(Closure $callable): static
{
$this->interceptor = $callable;

return $this;
}

protected function request(): PendingRequest
{
$request = $this->request->build($this->connection);

if (isset($this->interceptor)) {
call_user_func($this->interceptor, $request);
$this->interceptor = null;
}

return $request;
}

public static function fake(): void
{
config()->set('magento.connection', 'default');
Expand Down
65 changes: 65 additions & 0 deletions tests/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JustBetter\MagentoClient\Tests\Client;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -504,4 +505,68 @@ public function test_it_can_set_store_code(): void
return $request->url() == 'magento/rest/store/V1/products';
});
}

public function test_it_can_intersect_the_client_and_adjust_its_body_format(): void
{
Http::fake([
'magento/rest/all/V1/products*' => Http::response(['items' => []]),
]);

/** @var Magento $magento */
$magento = app(Magento::class);

$result = $magento->intercept(function (PendingRequest $request) {
$request->asForm();
});

$this->assertInstanceOf(Magento::class, $result);

$response = $magento->get('products', [
'searchCriteria[pageSize]' => 10,
'searchCriteria[currentPage]' => 0,
]);

$this->assertTrue($response->ok());
$this->assertCount(0, $response->json('items'));

Http::assertSent(function (Request $request) {
return $request->method() === 'GET' &&
$request->header('Content-Type')[0] === 'application/x-www-form-urlencoded' &&
$request->url() == 'magento/rest/all/V1/products?searchCriteria%5BpageSize%5D=10&searchCriteria%5BcurrentPage%5D=0';
});
}

public function test_it_resets_the_interceptor(): void
{
Http::fake([
'magento/rest/all/V1/products' => Http::response([
'product' => [
'entity_id' => 1,
'sku' => '::some-sku::',
],
]),
]);

/** @var Magento $magento */
$magento = app(Magento::class);

$magento->intercept(function (PendingRequest $request) {
$request->withHeaders(['some-header' => '::test::']);
})->post('products', [
'product' => [
'sku' => '::some-sku::',
],
]);

$magento->post('products', [
'product' => [
'sku' => '::some-sku::',
],
]);

Http::assertSentInOrder([
fn (Request $request) => $request->hasHeader('some-header'),
fn (Request $request) => ! $request->hasHeader('some-header'),
]);
}
}

0 comments on commit 09ac9de

Please sign in to comment.