@@ -64,11 +64,11 @@ namespace App\Service;
6464
6565use Hengebytes\WebserviceCoreAsyncBundle\Handler\AsyncRequestHandler;
6666use Hengebytes\WebserviceCoreAsyncBundle\Response\AsyncResponse;
67- use Hengebytes\WebserviceCoreAsyncBundle\Request\WSRequest;
67+ use Hengebytes\WebserviceCoreAsyncBundle\Request\WSRequest;use Hengebytes\WebserviceCoreAsyncBundle\Response\ModelPromise;
6868
6969class 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+ ```
0 commit comments