Changed way to connect to api
This commit is contained in:
@ -83,16 +83,25 @@ class Mailboxes
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
if (!isset($json->mailboxes)) {
|
||||
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
|
||||
throw new MissingArgument('mailboxes', 'array', 'mailboxes ids');
|
||||
}
|
||||
$output = [
|
||||
'mailboxes' => $json->mailboxes,
|
||||
'total' => count($json->mailboxes),
|
||||
'unregistered' => 0
|
||||
'unregistered' => [
|
||||
'total' => 0,
|
||||
'mailboxes' => []
|
||||
]
|
||||
];
|
||||
foreach ($json->mailboxes as $mailbox_name) {
|
||||
if ($service->unregister($mailbox_name)) {
|
||||
$output['unregistered'] ++;
|
||||
foreach ($json->mailboxes as $mailbox_id) {
|
||||
$mailbox = $service->getRepository()->fetchById($mailbox_id);
|
||||
if ($service->unregister($mailbox->getName())) {
|
||||
$output['unregistered']['total'] ++;
|
||||
$output['unregistered']['mailboxes'] []= [
|
||||
'id' => $mailbox->getId(),
|
||||
'name' => $mailbox->getName(),
|
||||
'unregistered' => true
|
||||
];
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
|
@ -118,6 +118,7 @@ class Mailboxes extends Base
|
||||
try {
|
||||
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
|
||||
} catch (BlankResult $e) {
|
||||
$this->getLogger()->error($e);
|
||||
// It's already unregistered
|
||||
return true;
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ class Message implements Model
|
||||
|
||||
public function doesHaveAttachments(): Message
|
||||
{
|
||||
if (!isset($this->getState()['has_attachments'])) {
|
||||
if (!isset($this->getStates()['has_attachments'])) {
|
||||
$this->newState('has_attachments');
|
||||
}
|
||||
$this->getState('has_attachments')->setValue(true);
|
||||
|
17
ui/common/Controller/Api.php
Normal file
17
ui/common/Controller/Api.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ProVM\Common\Service\Api as Service;
|
||||
|
||||
class Api
|
||||
{
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents(), JSON_OBJECT_AS_ARRAY);
|
||||
|
||||
return $service->sendRequest($json);
|
||||
}
|
||||
}
|
47
ui/common/Service/Api.php
Normal file
47
ui/common/Service/Api.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestFactoryInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Api
|
||||
{
|
||||
public function __construct(ClientInterface $client, RequestFactoryInterface $factory)
|
||||
{
|
||||
$this->setClient($client)
|
||||
->setFactory($factory);
|
||||
}
|
||||
|
||||
protected ClientInterface $client;
|
||||
protected RequestFactoryInterface $factory;
|
||||
|
||||
public function getClient(): ClientInterface
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
public function getFactory(): RequestFactoryInterface
|
||||
{
|
||||
return $this->factory;
|
||||
}
|
||||
public function setClient(ClientInterface $client): Api
|
||||
{
|
||||
$this->client = $client;
|
||||
return $this;
|
||||
}
|
||||
public function setFactory(RequestFactoryInterface $factory): Api
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sendRequest(array $request_data): ResponseInterface
|
||||
{
|
||||
$request = $this->getFactory()->createRequest(strtoupper($request_data['method']) ?? 'GET', $request_data['uri']);
|
||||
if (strtolower($request_data['method']) !== 'get') {
|
||||
$request->getBody()->write(\Safe\json_encode($request_data['data']));
|
||||
$request->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
return $this->getClient()->sendRequest($request);
|
||||
}
|
||||
}
|
4
ui/resources/routes/98_api.php
Normal file
4
ui/resources/routes/98_api.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
use ProVM\Common\Controller\Api;
|
||||
|
||||
$app->post('/api', Api::class);
|
@ -1,4 +1,4 @@
|
||||
<?php
|
||||
use ProVM\Common\Controller\Home;
|
||||
|
||||
$app->get('[/]', Home::class);
|
||||
$app->get('[/]', Home::class);
|
||||
|
@ -33,8 +33,8 @@
|
||||
).append(
|
||||
$('<button></button>').addClass('ui mini circular red icon button').append(
|
||||
$('<i></i>').addClass('remove icon')
|
||||
).click(() => {
|
||||
this.unregister()
|
||||
).click(e => {
|
||||
this.unregister($(e.currentTarget))
|
||||
})
|
||||
)
|
||||
} else {
|
||||
@ -42,7 +42,7 @@
|
||||
register.append(
|
||||
$('<button></button>').addClass('ui mini circular icon button').append(
|
||||
$('<i></i>').addClass('save icon')
|
||||
).click((e) => {
|
||||
).click(e => {
|
||||
this.register($(e.currentTarget))
|
||||
})
|
||||
)
|
||||
@ -72,20 +72,25 @@
|
||||
})
|
||||
})
|
||||
}
|
||||
unregister() {
|
||||
unregister(button) {
|
||||
const uri = '/mailboxes/unregister'
|
||||
const data = {
|
||||
mailboxes: [this.id]
|
||||
}
|
||||
button.html('').append(
|
||||
$('<i></i>').addClass('redo loading icon')
|
||||
)
|
||||
return Send.delete({
|
||||
uri,
|
||||
data
|
||||
}).then(response => {
|
||||
response.mailboxes.forEach(mb => {
|
||||
if (mb.id === this.id && mb.removed) {
|
||||
response.unregistered.mailboxes.forEach(mb => {
|
||||
if (mb.id === this.id && mb.unregistered) {
|
||||
this.id = null
|
||||
this.registered = false
|
||||
mailboxes.draw().table()
|
||||
const tr = button.parent().parent()
|
||||
tr.html('')
|
||||
this.draw(tr)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -1,38 +1,33 @@
|
||||
<script type="text/javascript">
|
||||
const Send = {
|
||||
base_url: '{{$urls->api}}',
|
||||
base: function({method, uri, data = null, dataType = 'json', contentType = 'application/json'}) {
|
||||
const url = this.base_url + '/' + uri.replace(/^\//g, '')
|
||||
base: function({method, uri, data = null}) {
|
||||
const request = {
|
||||
uri: uri.replace(/^\//g, ''),
|
||||
method
|
||||
}
|
||||
const options = {
|
||||
method,
|
||||
url,
|
||||
crossDomain: true,
|
||||
headers: {
|
||||
'Authorization': [
|
||||
'Bearer {{$api_key}}'
|
||||
]
|
||||
},
|
||||
dataType
|
||||
url: this.base_url,
|
||||
method: 'post',
|
||||
contentType: 'application/json'
|
||||
}
|
||||
if (method.toLowerCase() !== 'get' && data !== null) {
|
||||
if (!(typeof data === 'string' || data instanceof String)) {
|
||||
options['data'] = JSON.stringify(data)
|
||||
}
|
||||
options['contentType'] = contentType
|
||||
request['data'] = data
|
||||
}
|
||||
options['data'] = JSON.stringify(request)
|
||||
return $.ajax(options)
|
||||
},
|
||||
get: function(uri, dataType = 'json') {
|
||||
return this.base({method: 'get', uri, dataType})
|
||||
get: function(uri) {
|
||||
return this.base({method: 'get', uri})
|
||||
},
|
||||
post: function({uri, data, dataType = 'json', contentType = 'application/json'}) {
|
||||
return this.base({method: 'post', uri, data, dataType, contentType})
|
||||
post: function({uri, data}) {
|
||||
return this.base({method: 'post', uri, data})
|
||||
},
|
||||
put: function({uri, data, dataType = 'json', contentType = 'application/json'}) {
|
||||
return this.base({method: 'put', uri, data, dataType, contentType})
|
||||
put: function({uri, data}) {
|
||||
return this.base({method: 'put', uri, data})
|
||||
},
|
||||
delete: function({uri, data, dataType = 'json', contentType = 'application/json'}) {
|
||||
return this.base({method: 'delete', uri, data, dataType, contentType})
|
||||
delete: function({uri, data}) {
|
||||
return this.base({method: 'delete', uri, data})
|
||||
}
|
||||
}
|
||||
const _urls = JSON.parse('{!! Safe\json_encode($urls) !!}')
|
||||
|
@ -2,7 +2,12 @@
|
||||
return [
|
||||
'urls' => function() {
|
||||
$arr = ['base' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST']];
|
||||
$arr['api'] = 'http://localhost:8080';
|
||||
$arr['api'] = $_ENV['API_URI'];
|
||||
return (object) $arr;
|
||||
}
|
||||
},
|
||||
'view_urls' => function() {
|
||||
$arr = ['base' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST']];
|
||||
$arr['api'] = implode('/', [$arr['base'], 'api']);
|
||||
return (object) $arr;
|
||||
},
|
||||
];
|
||||
|
@ -11,5 +11,8 @@ return [
|
||||
]
|
||||
]
|
||||
]);
|
||||
}
|
||||
},
|
||||
Psr\Http\Message\RequestFactoryInterface::class => function(ContainerInterface $container) {
|
||||
return $container->get(Nyholm\Psr7\Factory\Psr17Factory::class);
|
||||
},
|
||||
];
|
@ -2,13 +2,13 @@
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
return [
|
||||
\Slim\Views\Blade::class => function(ContainerInterface $container) {
|
||||
return new \Slim\Views\Blade(
|
||||
Slim\Views\Blade::class => function(ContainerInterface $container) {
|
||||
return new Slim\Views\Blade(
|
||||
$container->get('folders')->views,
|
||||
$container->get('folders')->cache,
|
||||
null,
|
||||
[
|
||||
'urls' => $container->get('urls'),
|
||||
'urls' => $container->get('view_urls'),
|
||||
'api_key' => $container->get('api_key')
|
||||
]
|
||||
);
|
||||
|
Reference in New Issue
Block a user