-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestTask.php
178 lines (158 loc) · 5.74 KB
/
RequestTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/RestProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\RestProcessBundle\Task;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\RestProcessBundle\Exception\MissingClientException;
use CleverAge\RestProcessBundle\Registry\ClientRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\Exception\AccessException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* @phpstan-type Options array{
* 'client': string,
* 'url': string,
* 'method': string,
* 'headers': array<mixed>,
* 'url_parameters': array<mixed>,
* 'data': array<mixed>|string|null,
* 'sends': string,
* 'expects': string,
* 'valid_response_code': array<int>,
* 'log_response': bool,
* }
* @phpstan-type RequestOptions array{
* 'url': string,
* 'method': string,
* 'headers': array<mixed>,
* 'url_parameters': array<mixed>,
* 'sends': string,
* 'expects': string,
* 'data': array<mixed>|string|null
* }
*/
class RequestTask extends AbstractConfigurableTask
{
public function __construct(protected LoggerInterface $logger, protected ClientRegistry $registry)
{
}
/**
* @throws MissingClientException
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws \Throwable
*/
public function execute(ProcessState $state): void
{
/** @var Options $options */
$options = $this->getOptions($state);
$requestOptions = $this->getRequestOptions($state);
$this->logger->debug(
"Sending request {$requestOptions['method']} to '{$requestOptions['url']}'",
['requestOptions' => $requestOptions]
);
$response = $this->registry->getClient($options['client'])->call($requestOptions);
if ($options['log_response']) {
$this->logger->debug(
"Response received from '{$options['url']}'",
[
'requestOptions' => $requestOptions,
'result' => $response,
]
);
}
// Handle empty results
try {
if (!\in_array($response->getStatusCode(), $options['valid_response_code'], false)) {
$state->setErrorOutput($response->getContent());
if (TaskConfiguration::STRATEGY_SKIP === $state->getTaskConfiguration()->getErrorStrategy()) {
$state->setSkipped(true);
} elseif (TaskConfiguration::STRATEGY_STOP === $state->getTaskConfiguration()->getErrorStrategy()) {
$state->setStopped(true);
}
throw new \Exception('Invalid response code');
}
$state->setOutput($response->getContent());
} catch (\Throwable $e) {
$this->logger->error(
'REST request failed',
[
'client' => $options['client'],
'options' => $options,
'message' => $e->getMessage(),
'raw_headers' => $response->getHeaders(false),
'raw_body' => $response->getContent(false),
]
);
throw $e;
}
}
/**
* @throws UndefinedOptionsException
* @throws AccessException
*/
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(
[
'client',
'url',
'method',
]
);
$resolver->setDefaults(
[
'headers' => [],
'url_parameters' => [],
'data' => null,
'sends' => 'application/json',
'expects' => 'application/json',
'valid_response_code' => [200, 201, 204],
'log_response' => false,
]
);
$resolver->setAllowedTypes('client', ['string']);
$resolver->setAllowedTypes('url', ['string']);
$resolver->setAllowedTypes('method', ['string']);
$resolver->setAllowedTypes('valid_response_code', ['array']);
$resolver->setAllowedTypes('log_response', ['bool']);
}
/**
* @return RequestOptions
*/
protected function getRequestOptions(ProcessState $state): array
{
/** @var Options $options */
$options = $this->getOptions($state);
$requestOptions = [
'url' => $options['url'],
'method' => $options['method'],
'headers' => $options['headers'],
'url_parameters' => $options['url_parameters'],
'sends' => $options['sends'],
'expects' => $options['expects'],
'data' => $options['data'],
];
/** @var array<mixed> $input */
$input = $state->getInput() ?: [];
/** @var RequestOptions $mergedOptions */
$mergedOptions = array_merge($requestOptions, $input);
return $mergedOptions;
}
}