mirror of
https://github.com/danog/telerpc.git
synced 2024-11-26 12:04:47 +01:00
Apply fixes from StyleCI
This commit is contained in:
parent
804d4068e8
commit
59021cc568
@ -4,4 +4,4 @@ chdir(__DIR__);
|
|||||||
|
|
||||||
require __DIR__.'/vendor/autoload.php';
|
require __DIR__.'/vendor/autoload.php';
|
||||||
|
|
||||||
(new Main)->run(($argv[1] ?? '') === 'serve');
|
(new Main())->run(($argv[1] ?? '') === 'serve');
|
||||||
|
27
src/Main.php
27
src/Main.php
@ -9,7 +9,6 @@ use Amp\Http\Server\Response;
|
|||||||
use Amp\Http\Server\SocketHttpServer;
|
use Amp\Http\Server\SocketHttpServer;
|
||||||
use Amp\Log\ConsoleFormatter;
|
use Amp\Log\ConsoleFormatter;
|
||||||
use Amp\Log\StreamHandler;
|
use Amp\Log\StreamHandler;
|
||||||
use Amp\Mysql\MysqlConfig;
|
|
||||||
use Amp\Mysql\MysqlConnectionPool;
|
use Amp\Mysql\MysqlConnectionPool;
|
||||||
use danog\MadelineProto\RPCErrorException;
|
use danog\MadelineProto\RPCErrorException;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
@ -83,22 +82,23 @@ final class Main implements RequestHandler
|
|||||||
];
|
];
|
||||||
|
|
||||||
private MysqlConnectionPool $pool;
|
private MysqlConnectionPool $pool;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->pool = new MysqlConnectionPool(include __DIR__.'/../db.php');
|
$this->pool = new MysqlConnectionPool(include __DIR__.'/../db.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
private const HEADERS = [
|
private const HEADERS = [
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
'access-control-allow-origin' => '*',
|
'access-control-allow-origin' => '*',
|
||||||
'access-control-allow-methods' => 'GET, POST, OPTIONS',
|
'access-control-allow-methods' => 'GET, POST, OPTIONS',
|
||||||
'access-control-expose-headers' => 'Content-Length,Content-Type,Date,Server,Connection'
|
'access-control-expose-headers' => 'Content-Length,Content-Type,Date,Server,Connection',
|
||||||
];
|
];
|
||||||
|
|
||||||
private static function error(string $message, int $code = HttpStatus::BAD_REQUEST): Response
|
private static function error(string $message, int $code = HttpStatus::BAD_REQUEST): Response
|
||||||
{
|
{
|
||||||
return new Response(
|
return new Response(
|
||||||
$code,
|
$code,
|
||||||
self::HEADERS,
|
self::HEADERS,
|
||||||
json_encode(['ok' => false, 'description' => $message])
|
json_encode(['ok' => false, 'description' => $message])
|
||||||
);
|
);
|
||||||
@ -107,7 +107,7 @@ final class Main implements RequestHandler
|
|||||||
private static function ok(mixed $result): Response
|
private static function ok(mixed $result): Response
|
||||||
{
|
{
|
||||||
return new Response(
|
return new Response(
|
||||||
HttpStatus::OK,
|
HttpStatus::OK,
|
||||||
self::HEADERS,
|
self::HEADERS,
|
||||||
json_encode(['ok' => false, 'result' => $result])
|
json_encode(['ok' => false, 'result' => $result])
|
||||||
);
|
);
|
||||||
@ -215,7 +215,7 @@ final class Main implements RequestHandler
|
|||||||
$q = $this->pool->prepare('SELECT method FROM bot_method_invalid');
|
$q = $this->pool->prepare('SELECT method FROM bot_method_invalid');
|
||||||
$r = [];
|
$r = [];
|
||||||
foreach ($q->execute() as $result) {
|
foreach ($q->execute() as $result) {
|
||||||
$r []= $result['method'];
|
$r[] = $result['method'];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $r;
|
return $r;
|
||||||
@ -227,7 +227,7 @@ final class Main implements RequestHandler
|
|||||||
$q = $this->pool->prepare('SELECT error, method FROM errors');
|
$q = $this->pool->prepare('SELECT error, method FROM errors');
|
||||||
$r = [];
|
$r = [];
|
||||||
foreach ($q->execute() as $result) {
|
foreach ($q->execute() as $result) {
|
||||||
$r[$result['error']][]= $result['method'];
|
$r[$result['error']][] = $result['method'];
|
||||||
}
|
}
|
||||||
foreach ($r as $error => $methods) {
|
foreach ($r as $error => $methods) {
|
||||||
$fixed = self::sanitize($error);
|
$fixed = self::sanitize($error);
|
||||||
@ -261,7 +261,7 @@ final class Main implements RequestHandler
|
|||||||
$q = $this->pool->prepare('SELECT error, method FROM errors');
|
$q = $this->pool->prepare('SELECT error, method FROM errors');
|
||||||
$r = [];
|
$r = [];
|
||||||
foreach ($q->execute() as $result) {
|
foreach ($q->execute() as $result) {
|
||||||
$r[$result['error']][]= $result['method'];
|
$r[$result['error']][] = $result['method'];
|
||||||
}
|
}
|
||||||
foreach (\array_merge($r, self::GLOBAL_CODES) as $error => $methods) {
|
foreach (\array_merge($r, self::GLOBAL_CODES) as $error => $methods) {
|
||||||
if (\is_int($methods)) {
|
if (\is_int($methods)) {
|
||||||
@ -328,7 +328,8 @@ final class Main implements RequestHandler
|
|||||||
\file_put_contents('data/core.json', \json_encode(['errors' => $r, 'descriptions' => $hr, 'user_only' => $bot, 'bot_only' => $bot_only]));
|
\file_put_contents('data/core.json', \json_encode(['errors' => $r, 'descriptions' => $hr, 'user_only' => $bot, 'bot_only' => $bot_only]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleRequest(Request $request): Response {
|
public function handleRequest(Request $request): Response
|
||||||
|
{
|
||||||
$form = Form::fromRequest($request);
|
$form = Form::fromRequest($request);
|
||||||
$error = $request->getQueryParameter('error') ?? $form->getValue('error');
|
$error = $request->getQueryParameter('error') ?? $form->getValue('error');
|
||||||
$code = $request->getQueryParameter('code') ?? $form->getValue('code');
|
$code = $request->getQueryParameter('code') ?? $form->getValue('code');
|
||||||
@ -342,6 +343,7 @@ final class Main implements RequestHandler
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
$error = self::sanitize($error);
|
$error = self::sanitize($error);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ($error === $code) {
|
if ($error === $code) {
|
||||||
$this->pool->prepare('REPLACE INTO code_errors VALUES (?);')->execute([$code]);
|
$this->pool->prepare('REPLACE INTO code_errors VALUES (?);')->execute([$code]);
|
||||||
@ -355,13 +357,16 @@ final class Main implements RequestHandler
|
|||||||
if ($row = $result->fetchRow()) {
|
if ($row = $result->fetchRow()) {
|
||||||
return self::ok($row['description']);
|
return self::ok($row['description']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::error('No description', 404);
|
return self::error('No description', 404);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return self::error($e->getMessage(), 500);
|
return self::error($e->getMessage(), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::ok(true);
|
return self::ok(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::error('API for reporting Telegram RPC errors. For localized errors see https://rpc.madelineproto.xyz, to report a new error use the `code`, `method` and `error` GET/POST parameters. Source code at https://github.com/danog/telerpc.');
|
return self::error('API for reporting Telegram RPC errors. For localized errors see https://rpc.madelineproto.xyz, to report a new error use the `code`, `method` and `error` GET/POST parameters. Source code at https://github.com/danog/telerpc.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user