magento2-docker/app/code/IpSupply/ChatMessage/Helper/Request/MessageRequest.php

43 lines
1.1 KiB
PHP
Executable File

<?php
namespace IpSupply\ChatMessage\Helper\Request;
use GuzzleHttp\ClientInterface;
use IpSupply\ChatMessage\Helper\Authentication;
class MessageRequest extends RequestAbstract
{
use SimpleValidationTrait;
/**
* @var ClientInterface
*/
private $client;
public function __construct(ClientInterface $client)
{
$this->client = $client;
}
public function initialize($serverUrl, ParametersInterface $params, Authentication $defaultAuthentication)
{
$this->validate($params, ['type', 'to', 'subject', 'content']);
$auth = $params->getAuthentication();
if (empty($auth)) {
$auth = $defaultAuthentication;
}
$response = $this->client->request('POST',
$serverUrl . '/api/v1/messages',
[
'auth' => [
$auth->getUsername(),
$auth->getApiKey(),
],
'form_params' => $params->getData()
]
);
return MessageResponse::fromHttpResponse($response);
}
}