Skip to content

Commit 2303b33

Browse files
Merge pull request #36 from cleverage/feature/34
Improve run process via http call feature
2 parents a6cc010 + 83e5b9d commit 2303b33

File tree

5 files changed

+56
-17
lines changed

5 files changed

+56
-17
lines changed

config/services/http_value_resolver.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ services:
55
public: false
66
arguments:
77
- '%upload_directory%'
8+
- '@serializer'
89

910
cleverage_ui_process.http_value_resolver.process_configuration:
1011
class: CleverAge\UiProcessBundle\Http\ValueResolver\ProcessConfigurationValueResolver

docs/index.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,25 @@ That's all, now you can launch a process via http post request
5252
***Curl sample***
5353
```bash
5454
make bash
55-
curl --location 'http://apache2/http/process/execute?code=demo.die' \
56-
--header 'Authorization: Bearer 3da8409b5f5b640fb0c43d68e8ac8d23' \
57-
--form 'input=@"/file.csv"' \
58-
--form 'context[context_1]="FOO"' \
59-
--form 'context[context_2]="BAR"'
55+
curl --location 'http://localhost/http/process/execute' \
56+
--header 'Authorization: Bearer myBearerToken' \
57+
--form 'input=@"/path/to/your/file.csv"' \
58+
--form 'code="demo.dummy"' \
59+
--form 'context="{\"foo\": \"bar\"}"'
6060
```
61+
62+
```bash
63+
make bash
64+
curl --location 'http://localhost/http/process/execute' \
65+
--header 'Content-Type: application/json' \
66+
--header 'Authorization: Bearer d641d254aed12733758a3a4247559868' \
67+
--header 'Cookie: PHPSESSID=m8l9s5sniknv1b0jb798f8sri7; main_auth_profile_token=2f3d24' \
68+
--data '{
69+
"code": "demo.die",
70+
"context": {"foo": "bar"}
71+
}'
72+
```
73+
6174
* Query string code parameter must be a valid process code
6275
* Header Authorization: Bearer is the previously generated token
6376
* input could be string or file representation

src/Controller/ProcessExecuteController.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use CleverAge\UiProcessBundle\Message\ProcessExecuteMessage;
1818
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1919
use Symfony\Component\HttpFoundation\JsonResponse;
20-
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
20+
use Symfony\Component\HttpKernel\Attribute\ValueResolver;
2121
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
2222
use Symfony\Component\Messenger\MessageBusInterface;
2323
use Symfony\Component\Routing\Attribute\Route;
@@ -27,7 +27,7 @@
2727
class ProcessExecuteController extends AbstractController
2828
{
2929
public function __invoke(
30-
#[MapRequestPayload] HttpProcessExecution $httpProcessExecution,
30+
#[ValueResolver('http_process_execution')] HttpProcessExecution $httpProcessExecution,
3131
ValidatorInterface $validator,
3232
MessageBusInterface $bus,
3333
): JsonResponse {
@@ -43,7 +43,9 @@ public function __invoke(
4343
new ProcessExecuteMessage(
4444
$httpProcessExecution->code ?? '',
4545
$httpProcessExecution->input,
46-
$httpProcessExecution->context
46+
\is_string($httpProcessExecution->context)
47+
? json_decode($httpProcessExecution->context, true)
48+
: $httpProcessExecution->context
4749
)
4850
);
4951

src/Http/Model/HttpProcessExecution.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
namespace CleverAge\UiProcessBundle\Http\Model;
1515

1616
use CleverAge\UiProcessBundle\Validator\IsValidProcessCode;
17+
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
18+
use Symfony\Component\Validator\Constraints\Json;
1719
use Symfony\Component\Validator\Constraints\NotNull;
1820
use Symfony\Component\Validator\Constraints\Sequentially;
21+
use Symfony\Component\Validator\Constraints\Type;
1922

2023
final readonly class HttpProcessExecution
2124
{
@@ -26,7 +29,8 @@ public function __construct(
2629
#[Sequentially(constraints: [new NotNull(message: 'Process code is required.'), new IsValidProcessCode()])]
2730
public ?string $code = null,
2831
public ?string $input = null,
29-
public array $context = [],
32+
#[AtLeastOneOf(constraints: [new Json(), new Type('array')])]
33+
public string|array $context = [],
3034
) {
3135
}
3236
}

src/Http/ValueResolver/HttpProcessExecuteValueResolver.php

+27-8
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
use Symfony\Component\HttpKernel\Attribute\AsTargetedValueResolver;
2121
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
2222
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
23+
use Symfony\Component\Serializer\SerializerInterface;
2324

2425
#[AsTargetedValueResolver('http_process_execution')]
2526
readonly class HttpProcessExecuteValueResolver implements ValueResolverInterface
2627
{
27-
public function __construct(private string $storageDir)
28+
public function __construct(private string $storageDir, private SerializerInterface $serializer)
2829
{
2930
}
3031

@@ -33,13 +34,31 @@ public function __construct(private string $storageDir)
3334
*/
3435
public function resolve(Request $request, ArgumentMetadata $argument): iterable
3536
{
36-
$input = $request->get('input', $request->files->get('input'));
37-
if ($input instanceof UploadedFile) {
38-
$uploadFileName = $this->storageDir.\DIRECTORY_SEPARATOR.date('YmdHis').'_'.uniqid().'_'.$input->getClientOriginalName();
39-
(new Filesystem())->dumpFile($uploadFileName, $input->getContent());
40-
$input = $uploadFileName;
41-
}
37+
$all = $request->request->all();
38+
try {
39+
if ([] === $all) {
40+
$httpProcessExecution = $this->serializer->deserialize(
41+
$request->getContent(),
42+
HttpProcessExecution::class,
43+
'json'
44+
);
45+
} else {
46+
$input = $request->get('input', $request->files->get('input'));
47+
if ($input instanceof UploadedFile) {
48+
$uploadFileName = $this->storageDir.\DIRECTORY_SEPARATOR.date('YmdHis').'_'.uniqid().'_'.$input->getClientOriginalName();
49+
(new Filesystem())->dumpFile($uploadFileName, $input->getContent());
50+
$input = $uploadFileName;
51+
}
52+
$httpProcessExecution = new HttpProcessExecution(
53+
$request->get('code'),
54+
$input,
55+
$request->get('context', [])
56+
);
57+
}
4258

43-
return [new HttpProcessExecution($request->get('code'), $input, $request->get('context', []))];
59+
return [$httpProcessExecution];
60+
} catch (\Throwable) {
61+
return [new HttpProcessExecution()];
62+
}
4463
}
4564
}

0 commit comments

Comments
 (0)