Add generic api schema and scripts model

This commit is contained in:
2025-08-30 18:20:24 +02:00
parent 774d1cf45f
commit f6993d463d
18 changed files with 493 additions and 192 deletions

82
app/Bots/GenericApi.php Normal file
View File

@@ -0,0 +1,82 @@
<?php
namespace App\Bots;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
class GenericApi implements BotContract
{
protected array $config;
protected Client $client;
public function __construct(array $config = [])
{
$this->config = $config;
}
public function run(): void
{
try {
$this->client = new Client();
$options = [];
if (!empty($this->config['headers'])) {
$options['headers'] = json_decode($this->config['headers'], true);
}
if (!empty($this->config['body'])) {
$options['json'] = json_decode($this->config['body'], true);
}
$this->client->request(
$this->config['method'] ?? 'GET',
$this->config['url'],
$options
);
} catch (GuzzleException $e) {
Log::error("Call to {$this->config['url']} failed" . $e->getMessage());
}
}
public static function configSchema(): array
{
return [
'url' => [
'type' => 'string',
'label' => 'API URL',
'rules' => [
'required',
'url',
]
],
'method' => [
'type' => 'string',
'label' => 'HTTP Method',
'rules' => [
'required',
'in:GET,POST,PUT,DELETE,PATCH',
]
],
'headers' => [
'type' => 'json',
'label' => 'Request Headers (JSON)',
'rules' => [
'nullable',
'json',
]
],
'body' => [
'type' => 'json',
'label' => 'Request Body (JSON)',
'rules' => [
'nullable',
'json',
]
],
];
}
}