Add generic api schema and scripts model
This commit is contained in:
82
app/Bots/GenericApi.php
Normal file
82
app/Bots/GenericApi.php
Normal 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',
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user