Skip to content

Commit 0f5c941

Browse files
author
Maelmin Henge
committed
introduce request model function in request handler
1 parent 93be94a commit 0f5c941

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

README.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ namespace App\Service;
6464

6565
use Hengebytes\WebserviceCoreAsyncBundle\Handler\AsyncRequestHandler;
6666
use Hengebytes\WebserviceCoreAsyncBundle\Response\AsyncResponse;
67-
use Hengebytes\WebserviceCoreAsyncBundle\Request\WSRequest;
67+
use Hengebytes\WebserviceCoreAsyncBundle\Request\WSRequest;use Hengebytes\WebserviceCoreAsyncBundle\Response\ModelPromise;
6868

6969
class MyService
7070
{
71-
public function __construct(private readonly AsyncRequestHandler) {
71+
public function __construct(private readonly AsyncRequestHandler $rh) {
7272
}
7373

7474
// sync example
@@ -112,6 +112,30 @@ class MyService
112112

113113
return $this->rh->request($request);
114114
}
115+
116+
117+
/**
118+
* async example with model
119+
* @param array $data
120+
* @return ModelPromise<SomeModel>
121+
* @throws \Hengebytes\WebserviceCoreAsyncBundle\Exception\ConnectionInitException
122+
*/
123+
public function executeAsyncModel(array $data): ModelPromise
124+
{
125+
$request = new WSRequest(
126+
'my_service',
127+
'/profile',
128+
RequestMethodEnum::POST,
129+
'sub_service',
130+
);
131+
$request->setAuthBasic('username', 'password');
132+
$request->setHeaders([
133+
'Content-Type' => 'application/json',
134+
]);
135+
$request->setBody(json_encode($data));
136+
137+
return $this->rh->requestModel($request, SomeModel::class);
138+
}
115139
}
116140
```
117141

@@ -154,6 +178,26 @@ class MyController extends AbstractController
154178

155179
return $this->json(['result' => array_merge($response1, $response2)]);
156180
}
181+
182+
183+
public function asyncWithModels(Request $request): JsonResponse
184+
{
185+
$requestParams = $request->request->all();
186+
$requestParams['page'] = 1;
187+
$result1 = $this->myService->executeAsyncModel($requestParams);
188+
189+
$requestParams['page'] = 2;
190+
$result2 = $this->myService->executeAsyncModel($requestParams);
191+
// do something else while the request is being processed
192+
193+
$model1 = $result->getModel();
194+
$model2 = $result->getModel();
195+
196+
return $this->json([
197+
'page1' => $model1,
198+
'page2' $model2
199+
]);
200+
}
157201
}
158202
```
159203

@@ -255,3 +299,30 @@ Higher priority will be executed first
255299
| `StoreToCacheResponseModifier` | -200 | `!$response->isCached` | With Cache |
256300
| `RequestUnlockResponseModifier` | -210 | `!$response->isCached && $response->WSRequest->isCachable()` | With Cache |
257301
| `InvalidateCacheResponseModifier` | -220 | `!$response->isCached && !$response->WSRequest->isGETRequestMethod()` | With Cache |
302+
303+
### You should create model provider for the model promise
304+
305+
it will be automatically registered based on interface implementation
306+
and will be automatically called when the promise is resolved
307+
308+
```php
309+
// src/Provider/MyModelProvider.php
310+
namespace App\Provider;
311+
312+
use Hengebytes\WebserviceCoreAsyncBundle\Provider\ModelProviderInterface;
313+
use App\Model\SomeModel;
314+
use App\Model\SomeOtherModel;
315+
316+
class MyModelProvider implements ModelProviderInterface
317+
{
318+
public function getModel(mixed $data, ModelProvider $modelProvider): object
319+
{
320+
$data = $data['data'] ?? [];
321+
$someOtherModel = $modelProvider->getModel(SomeOtherModel::class, $data['someOtherModel'] ?? []);
322+
$someModel = new SomeModel($data)
323+
$someModel->setSomeOtherModel($someOtherModel);
324+
325+
return $someModel;
326+
}
327+
}
328+
```

src/Handler/AsyncRequestHandler.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
<?php
22

3-
43
namespace Hengebytes\WebserviceCoreAsyncBundle\Handler;
54

65
use Hengebytes\WebserviceCoreAsyncBundle\Cache\CacheManager;
76
use Hengebytes\WebserviceCoreAsyncBundle\Exception\ConnectionInitException;
87
use Hengebytes\WebserviceCoreAsyncBundle\Middleware\RequestModification;
98
use Hengebytes\WebserviceCoreAsyncBundle\Middleware\ResponseModification;
9+
use Hengebytes\WebserviceCoreAsyncBundle\Provider\ModelProvider;
1010
use Hengebytes\WebserviceCoreAsyncBundle\Request\WSRequest;
1111
use Hengebytes\WebserviceCoreAsyncBundle\Response\AsyncResponse;
12+
use Hengebytes\WebserviceCoreAsyncBundle\Response\ModelPromise;
1213
use Symfony\Component\HttpFoundation\Response;
1314
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
1415
use Symfony\Contracts\HttpClient\HttpClientInterface;
1516

16-
class AsyncRequestHandler
17+
/**
18+
* @template T
19+
*/
20+
readonly class AsyncRequestHandler
1721
{
1822
public function __construct(
19-
protected readonly HttpClientInterface $client,
23+
protected HttpClientInterface $client,
2024
protected RequestModification $requestModification,
2125
protected ResponseModification $responseModification,
22-
protected readonly ?CacheManager $cacheManager = null,
26+
protected ?CacheManager $cacheManager = null,
27+
protected ModelProvider $modelProvider,
2328
) {
2429
}
2530

@@ -59,4 +64,15 @@ public function request(WSRequest $request): AsyncResponse
5964
}
6065
}
6166

67+
/**
68+
* @param WSRequest $request
69+
* @param class-string<T> $modelClass
70+
* @return ModelPromise<T>
71+
* @throws ConnectionInitException
72+
*/
73+
public function requestModel(WSRequest $request, string $modelClass): ModelPromise
74+
{
75+
return new ModelPromise($this->request($request), $modelClass, $this->modelProvider);
76+
}
77+
6278
}

0 commit comments

Comments
 (0)