Skip to content

Commit a4b07e9

Browse files
author
Vasiliev.A
committed
Asynchronous WebAPI implementation
1 parent 28916b5 commit a4b07e9

33 files changed

+3022
-3
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\WebapiAsync\Api\Data\AsyncResponse;
8+
9+
/**
10+
* ItemStatusInterface interface
11+
* Temporary object with status of requested item.
12+
* Indicate if entity param was Accepted|Rejected to bulk schedule
13+
*
14+
* @api
15+
* @since 100.3.0
16+
*/
17+
interface ItemStatusInterface
18+
{
19+
const ENTITY_ID = 'entity_id';
20+
const DATA_HASH = 'data_hash';
21+
const STATUS = 'status';
22+
const ERROR_MESSAGE = 'error_message';
23+
const ERROR_CODE = 'error_code';
24+
25+
const STATUS_ACCEPTED = 'accepted';
26+
const STATUS_REJECTED = 'rejected';
27+
28+
/**
29+
* Get entity Id.
30+
*
31+
* @return int
32+
* @since 100.3.0
33+
*/
34+
public function getId();
35+
36+
/**
37+
* Sets entity Id.
38+
*
39+
* @param int $entityId
40+
* @return $this
41+
* @since 100.3.0
42+
*/
43+
public function setId($entityId);
44+
45+
/**
46+
* Get hash of entity data.
47+
*
48+
* @return string md5 hash of entity params array.
49+
* @since 100.3.0
50+
*/
51+
public function getDataHash();
52+
53+
/**
54+
* Sets hash of entity data.
55+
*
56+
* @param string $hash md5 hash of entity params array.
57+
* @return $this
58+
* @since 100.3.0
59+
*/
60+
public function setDataHash($hash);
61+
62+
/**
63+
* Get status.
64+
*
65+
* @return string accepted|rejected
66+
* @since 100.3.0
67+
*/
68+
public function getStatus();
69+
70+
/**
71+
* Sets entity status.
72+
*
73+
* @param string $status accepted|rejected
74+
* @return $this
75+
* @since 100.3.0
76+
*/
77+
public function setStatus($status = self::STATUS_ACCEPTED);
78+
79+
/**
80+
* Get error information.
81+
*
82+
* @return string|null
83+
* @since 100.3.0
84+
*/
85+
public function getErrorMessage();
86+
87+
/**
88+
* Sets error information.
89+
*
90+
* @param string|null|\Exception $error
91+
* @return $this
92+
* @since 100.3.0
93+
*/
94+
public function setErrorMessage($error = null);
95+
96+
/**
97+
* Get error code.
98+
*
99+
* @return int|null
100+
* @since 100.3.0
101+
*/
102+
public function getErrorCode();
103+
104+
/**
105+
* Sets error information.
106+
*
107+
* @param int|null|\Exception $errorCode Default: null
108+
* @return $this
109+
* @since 100.3.0
110+
*/
111+
public function setErrorCode($errorCode = null);
112+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\WebapiAsync\Api\Data\AsyncResponse;
8+
9+
/**
10+
* AsyncResponseItemListInterface interface
11+
* List of status requested entities to async router. Accepted|Rejected
12+
* Temporary data list for async router response.
13+
*
14+
* @api
15+
* @since 100.3.0
16+
*/
17+
interface ItemsListInterface
18+
{
19+
/**
20+
* Get list of statuses for requested entities.
21+
*
22+
* @return \Magento\WebapiAsync\Api\Data\AsyncResponse\ItemStatusInterface[]
23+
* @since 100.3.0
24+
*/
25+
public function getItems();
26+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\WebapiAsync\Api\Data;
8+
9+
/**
10+
* Interface AsyncResponseInterface
11+
* Temporary data object to give response from webapi async router
12+
*
13+
* @api
14+
* @since 100.3.0
15+
*/
16+
interface AsyncResponseInterface
17+
{
18+
const BULK_UUID = 'bulk_uuid';
19+
const REQUEST_ITEMS = 'request_items';
20+
21+
/**
22+
* Gets the bulk uuid.
23+
*
24+
* @return string Bulk Uuid.
25+
* @since 100.3.0
26+
*/
27+
public function getBulkUuid();
28+
29+
/**
30+
* Sets the bulk uuid.
31+
*
32+
* @param string $bulkUuid
33+
* @return $this
34+
* @since 100.3.0
35+
*/
36+
public function setBulkUuid($bulkUuid);
37+
38+
/**
39+
* Gets the list of request items with status data.
40+
*
41+
* @return \Magento\WebapiAsync\Api\Data\AsyncResponse\ItemsListInterface
42+
* @since 100.3.0
43+
*/
44+
public function getRequestItems();
45+
46+
/**
47+
* Sets the list of request items with status data.
48+
*
49+
* @param \Magento\WebapiAsync\Api\Data\AsyncResponse\ItemsListInterface $requestItems
50+
* @return $this
51+
* @since 100.3.0
52+
*/
53+
public function setRequestItems(\Magento\WebapiAsync\Api\Data\AsyncResponse\ItemsListInterface $requestItems);
54+
55+
/**
56+
* Retrieve existing extension attributes object.
57+
*
58+
* @return \Magento\WebapiAsync\Api\Data\AsyncResponseExtensionInterface|null
59+
* @since 100.3.0
60+
*/
61+
public function getExtensionAttributes();
62+
63+
/**
64+
* Set an extension attributes object.
65+
*
66+
* @param \Magento\WebapiAsync\Api\Data\AsyncResponseExtensionInterface $extensionAttributes
67+
* @return $this
68+
* @since 100.3.0
69+
*/
70+
public function setExtensionAttributes(
71+
\Magento\WebapiAsync\Api\Data\AsyncResponseExtensionInterface $extensionAttributes
72+
);
73+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\WebapiAsync\Code\Generator\Config\RemoteServiceReader;
8+
9+
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
10+
use Magento\WebapiAsync\Model\ConfigInterface as WebApiAsyncConfig;
11+
use Magento\Framework\Communication\Config\ReflectionGenerator;
12+
13+
/**
14+
* Remote service reader with auto generated configuration for communication.xml
15+
*/
16+
class Communication implements \Magento\Framework\Config\ReaderInterface
17+
{
18+
19+
/**
20+
* @var \Magento\WebapiAsync\Model\ConfigInterface
21+
*/
22+
private $webapiAsyncConfig;
23+
24+
/**
25+
* @var \Magento\Framework\Communication\Config\ReflectionGenerator
26+
*/
27+
private $reflectionGenerator;
28+
29+
/**
30+
* Initialize dependencies.
31+
*
32+
* @param \Magento\WebapiAsync\Model\ConfigInterface $webapiAsyncConfig
33+
* @param \Magento\Framework\Communication\Config\ReflectionGenerator $reflectionGenerator
34+
*/
35+
public function __construct(
36+
WebApiAsyncConfig $webapiAsyncConfig,
37+
ReflectionGenerator $reflectionGenerator
38+
) {
39+
$this->webapiAsyncConfig = $webapiAsyncConfig;
40+
$this->reflectionGenerator = $reflectionGenerator;
41+
}
42+
43+
/**
44+
* Generate communication configuration based on remote services declarations
45+
*
46+
* @param string|null $scope
47+
* @return array
48+
*/
49+
public function read($scope = null)
50+
{
51+
try {
52+
$asyncServicesData = $this->webapiAsyncConfig->getServices();
53+
} catch (\Exception $e) {
54+
return [];
55+
}
56+
$result = [];
57+
foreach ($asyncServicesData as $serviceData) {
58+
$topicName = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_TOPIC];
59+
$serviceClass = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_INTERFACE];
60+
$serviceMethod = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_METHOD];
61+
62+
$topicConfig = $this->reflectionGenerator->generateTopicConfigForServiceMethod(
63+
$topicName,
64+
$serviceClass,
65+
$serviceMethod,
66+
[
67+
WebApiAsyncConfig::DEFAULT_HANDLER_NAME => [
68+
CommunicationConfig::HANDLER_TYPE => $serviceClass,
69+
CommunicationConfig::HANDLER_METHOD => $serviceMethod,
70+
],
71+
]
72+
);
73+
$rewriteTopicParams = [
74+
CommunicationConfig::TOPIC_IS_SYNCHRONOUS => false,
75+
CommunicationConfig::TOPIC_RESPONSE => null,
76+
];
77+
$result[$topicName] = array_merge($topicConfig, $rewriteTopicParams);
78+
}
79+
$result[WebApiAsyncConfig::SYSTEM_TOPIC_NAME] = WebApiAsyncConfig::SYSTEM_TOPIC_CONFIGURATION;
80+
81+
return [CommunicationConfig::TOPICS => $result];
82+
}
83+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\WebapiAsync\Code\Generator\Config\RemoteServiceReader;
8+
9+
use Magento\WebapiAsync\Model\ConfigInterface as WebApiAsyncConfig;
10+
11+
/**
12+
* Remote service reader with auto generated configuration for queue_consumer.xml
13+
*/
14+
class Consumer implements \Magento\Framework\Config\ReaderInterface
15+
{
16+
/**
17+
* @var \Magento\WebapiAsync\Model\ConfigInterface
18+
*/
19+
private $webapiAsyncConfig;
20+
21+
/**
22+
* Initialize dependencies.
23+
*
24+
* @param \Magento\WebapiAsync\Model\ConfigInterface $webapiAsyncConfig
25+
*/
26+
public function __construct(
27+
WebApiAsyncConfig $webapiAsyncConfig
28+
) {
29+
$this->webapiAsyncConfig = $webapiAsyncConfig;
30+
}
31+
32+
/**
33+
* Generate consumer configuration based on remote services declarations
34+
*
35+
* @param string|null $scope
36+
* @return array
37+
*/
38+
public function read($scope = null)
39+
{
40+
try {
41+
$asyncServicesData = $this->webapiAsyncConfig->getServices();
42+
} catch (\Exception $e) {
43+
return [];
44+
}
45+
$result = [];
46+
foreach ($asyncServicesData as $serviceData) {
47+
$topicName = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_TOPIC];
48+
$serviceClass = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_INTERFACE];
49+
$serviceMethod = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_METHOD];
50+
51+
$result[$topicName] =
52+
[
53+
'name' => $topicName,
54+
'queue' => $topicName,
55+
'consumerInstance' => WebApiAsyncConfig::DEFAULT_CONSUMER_INSTANCE,
56+
'connection' => WebApiAsyncConfig::DEFAULT_CONSUMER_CONNECTION,
57+
'maxMessages' => WebApiAsyncConfig::DEFAULT_CONSUMER_MAX_MESSAGE,
58+
'handlers' => [
59+
WebApiAsyncConfig::DEFAULT_HANDLER_NAME => [
60+
'type' => $serviceClass,
61+
'method' => $serviceMethod,
62+
],
63+
],
64+
];
65+
}
66+
67+
return $result;
68+
}
69+
}

0 commit comments

Comments
 (0)