Skip to content

Commit

Permalink
Merge pull request #18 from innobraingmbh/feat/request-middleware
Browse files Browse the repository at this point in the history
Feat/request middleware
  • Loading branch information
Katalam authored Oct 11, 2024
2 parents 8e0f285 + 20546d6 commit 1dc3373
Show file tree
Hide file tree
Showing 12 changed files with 532 additions and 102 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog
## main
- Refactored the passing of the request to the on office service
- Added a method to get the last request response pair from the repository
- Added die and dump functions to the queries
- Fixed ImprintBuilder cannot return imprint with find

## v1.3.0
- Added a throw to stub responses
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ Config::set('onoffice.secret', 'secret');
Config::set('onoffice.api_claim', 'api_claim');
```

### Middlewares
```php
use Illuminate\Support\Facades\Log;
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::query()
->before(static function (OnOfficeRequest $request) {
Log::info('About to send request', [
'request' => $request->toArray(),
]);
})
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));
```

### Debugging
```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::query()
->dd()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));
```
```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::record();

BaseRepository::query()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

$result = BaseRepository::lastRecorded();

/*
$result = [
OnOfficeRequest,
OnOfficeResponse,
];
*/
```

### Usage in tests
```php
use Innobrain\OnOfficeAdapter\Facades\EstateRepository;
Expand Down
94 changes: 94 additions & 0 deletions docs/BaseRepository.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,97 @@ BaseRepository::query()
},
);
```

## dd, dump
```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::query()
->dd()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

// → will dump the request and die
```
```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::query()
->dump()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

// → will dump the request
```

```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::query()
->raw()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

// → will dump the request exactly as fired and die
```

```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::record();

BaseRepository::query()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

$result = BaseRepository::lastRecorded();

/*
$result = [
OnOfficeRequest,
OnOfficeResponse,
];
*/
```
```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::record();

BaseRepository::query()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

$result = BaseRepository::lastRecordedRequest();

/*
$result = OnOfficeRequest;
*/
```
```php
use Innobrain\OnOfficeAdapter\Facades\BaseRepository;

BaseRepository::record();

BaseRepository::query()
->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

$result = BaseRepository::lastRecordedResponse();

/*
$result = OnOfficeResponse;
*/
```
32 changes: 32 additions & 0 deletions src/Dtos/OnOfficeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Innobrain\OnOfficeAdapter\Dtos;

use Illuminate\Support\Carbon;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceId;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType;
use Innobrain\OnOfficeAdapter\Services\OnOfficeService;

class OnOfficeRequest
{
Expand All @@ -28,4 +30,34 @@ public function toArray(): array
'parameters' => $this->parameters,
];
}

public function toRequestArray(): array
{
/** @var OnOfficeService $onOfficeService */
$onOfficeService = app(OnOfficeService::class);

if (! empty($onOfficeService->getApiClaim())) {
$this->parameters = array_replace([OnOfficeService::EXTENDEDCLAIM => $onOfficeService->getApiClaim()], $this->parameters);
}

return [
'token' => $onOfficeService->getToken(),
'request' => [
'actions' => [
[
'actionid' => $this->actionId->value,
'resourceid' => $this->resourceId instanceof OnOfficeResourceId
? $this->resourceId->value
: $this->resourceId,
'resourcetype' => $this->resourceType->value,
'identifier' => $this->identifier,
'timestamp' => Carbon::now()->timestamp,
'hmac' => $onOfficeService->getHmac($this->actionId, $this->resourceType),
'hmac_version' => 2,
'parameters' => $this->parameters,
],
],
],
];
}
}
16 changes: 16 additions & 0 deletions src/Facades/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Innobrain\OnOfficeAdapter\Facades;

use Illuminate\Support\Facades\Facade;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponsePage;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
Expand Down Expand Up @@ -87,6 +88,21 @@ public static function stopRecording(): RootRepository
return static::getFacadeRoot()->stopRecording();
}

public static function lastRecorded(): ?array
{
return static::getFacadeRoot()->lastRecorded();
}

public static function lastRecordedRequest(): ?OnOfficeRequest
{
return static::getFacadeRoot()->lastRecordedRequest();
}

public static function lastRecordedResponse(): ?array
{
return static::getFacadeRoot()->lastRecordedResponse();
}

public static function assertSent(?callable $callback = null): void
{
static::getFacadeRoot()->assertSent($callback);
Expand Down
61 changes: 54 additions & 7 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Innobrain\OnOfficeAdapter\Repositories\BaseRepository;
use Innobrain\OnOfficeAdapter\Services\OnOfficeService;
use JsonException;
use Symfony\Component\VarDumper\VarDumper;
use Throwable;

class Builder implements BuilderInterface
Expand Down Expand Up @@ -97,6 +98,11 @@ class Builder implements BuilderInterface
*/
private ?OnOfficeResponse $responseCache = null;

/**
* The before sending middlewares.
*/
protected array $beforeSendingCallbacks = [];

public function __construct() {}

protected function getOnOfficeService(): OnOfficeService
Expand Down Expand Up @@ -130,24 +136,52 @@ public function preventStrayRequests(bool $value = true): static
return $this;
}

public function before(callable $callback): static
{
$this->beforeSendingCallbacks[] = $callback;

return $this;
}

public function dump(): static
{
return $this->before(static function (OnOfficeRequest $request) {
VarDumper::dump($request);
});
}

public function dd(): static
{
return $this->before(static function (OnOfficeRequest $request) {
VarDumper::dump($request);

exit(1);
});
}

public function raw(): static
{
return $this->before(static function (OnOfficeRequest $request) {
VarDumper::dump($request->toRequestArray());

exit(1);
});
}

/**
* @throws OnOfficeException
* @throws Throwable
*/
public function requestApi(OnOfficeRequest $request): Response
{
$request = $this->runBeforeSendingCallbacks($request);

$response = $this->getStubCallable($request);

if (is_null($response)) {
throw_if($this->preventStrayRequests, new StrayRequestException(request: $request));

$response = $this->getOnOfficeService()->requestApi(
$request->actionId,
$request->resourceType,
$request->resourceId,
$request->identifier,
$request->parameters,
);
$response = $this->getOnOfficeService()->requestApi($request);
} else {
$this->getOnOfficeService()->throwIfResponseIsFailed($response);
}
Expand All @@ -157,6 +191,19 @@ public function requestApi(OnOfficeRequest $request): Response
return $response;
}

protected function runBeforeSendingCallbacks(OnOfficeRequest $request): OnOfficeRequest
{
return tap($request, function (OnOfficeRequest &$request) {
collect($this->beforeSendingCallbacks)->each(function (callable $callback) use (&$request) {
$result = $callback($request);

if ($result instanceof OnOfficeRequest) {
$request = $result;
}
});
});
}

/**
* @throws OnOfficeException
*/
Expand Down
7 changes: 4 additions & 3 deletions src/Query/ImprintBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function first(): ?array
}

/**
* @throws OnOfficeException
* @throws Throwable<OnOfficeException>
*/
public function find(int $id): array
{
$response = $this->onOfficeService->requestApi(
$request = new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Impressum,
resourceId: $id,
Expand All @@ -69,6 +69,7 @@ public function find(int $id): array
]
);

return $response->json('response.results.0.data.records.0');
return $this->requestApi($request)
->json('response.results.0.data.records.0');
}
}
15 changes: 15 additions & 0 deletions src/Repositories/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ public function recordRequestResponsePair(OnOfficeRequest $request, array $respo
return $this;
}

public function lastRecorded(): ?array
{
return collect($this->recorded)->last();
}

public function lastRecordedRequest(): ?OnOfficeRequest
{
return collect($this->recorded)->last()[0] ?? null;
}

public function lastRecordedResponse(): ?array
{
return collect($this->recorded)->last()[1] ?? null;
}

/**
* Get a collection of the request / response pairs matching the given truth test.
*/
Expand Down
Loading

0 comments on commit 1dc3373

Please sign in to comment.