Skip to content

Commit

Permalink
Merge pull request #26 from alnutile/webhook
Browse files Browse the repository at this point in the history
Webhook
  • Loading branch information
alnutile committed Jun 12, 2023
2 parents 1c2b7b5 + 34e90f8 commit cb6248a
Show file tree
Hide file tree
Showing 20 changed files with 693 additions and 22 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -121,3 +121,9 @@ git push origin main
```

That's it! This will allow you to have a private "fork" of a public repository and to merge updates from the public repository into your private one.

## Test

```bash
composer test
```
100 changes: 100 additions & 0 deletions app/Http/Controllers/Sources/WebHookSourceController.php
@@ -0,0 +1,100 @@
<?php

namespace App\Http\Controllers\Sources;

use App\Events\SourceRunCompleteEvent;
use App\Models\Project;
use App\Models\Source;
use App\Source\SourceEnum;

class WebHookSourceController extends BaseSourceController
{
public function create(Project $project)
{
return inertia('Sources/WebHook/Create', [
'details' => config('larachain.sources.web_hook'),
'project' => $project,
'source' => [
'meta_data' => [],
],
]);
}

public function edit(Project $project, Source $source)
{
return inertia('Sources/WebHook/Edit', [
'details' => config('larachain.sources.web_hook'),
'project' => $project,
'source' => $source,
]);
}

public function store(Project $project)
{
$validated = request()->validate([
'name' => ['required'],
'description' => ['nullable'],
'meta_data' => ['nullable'],
]);

Source::create([
'project_id' => $project->id,
'name' => $validated['name'],
'description' => $validated['description'],
'type' => SourceEnum::WebHook,
'order' => 1,
'meta_data' => data_get($validated, 'meta_data'),
]);

request()->session()->flash('flash.banner', 'Source Created 🤘');

return to_route('projects.show', [
'project' => $project->id,
]);
}

public function update(Project $project, Source $source)
{
$validated = request()->validate([
'meta_data' => ['nullable'],
'name' => ['required'],
'description' => ['nullable'],
]);

$validated['project_id'] = $project->id;

$source->update([
'project_id' => $validated['project_id'],
'name' => $validated['name'],
'description' => $validated['description'],
'type' => SourceEnum::WebHook,
'order' => 1,
'meta_data' => data_get($validated, 'meta_data'),
]);

request()->session()->flash('flash.banner', 'Source Updated ✅');

return to_route('projects.show', [
'project' => $project->id,
]);
}

public function webhook(Project $project, Source $source)
{
/**
* @TODO
* Security
* Allow to dump into queue
* Allow to return any response
*/
$payload = request()->all();

logger('Webhook coming in', $payload);

$source->run($payload);

SourceRunCompleteEvent::dispatch($source);

return response()->json('OK', 200);
}
}
37 changes: 37 additions & 0 deletions app/Jobs/ProcessSourceListeners.php
@@ -0,0 +1,37 @@
<?php

namespace App\Jobs;

use App\Models\Document;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessSourceListeners implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct(public Document $document)
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
/**
* See if has attached Listeners
* If so queue those up
* else just return document
*
* @TODO how to trigger transformations
*/
}
}
24 changes: 24 additions & 0 deletions app/Listeners/RunTransformersListener.php
@@ -0,0 +1,24 @@
<?php

namespace App\Listeners;

use App\Events\SourceRunCompleteEvent;

class RunTransformersListener
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}

/**
* Handle the event.
*/
public function handle(SourceRunCompleteEvent $event): void
{
//
}
}
38 changes: 22 additions & 16 deletions app/Models/Source.php
Expand Up @@ -46,26 +46,13 @@ public function documents()
/**
* @throws SourceTypeMissingException
*/
public function run()
public function run(array $payload = []): Document
{
try {
$statusTypes = config('larachain.sources');
$statusType = $this->type->value;
$statusType = data_get($statusTypes, $statusType);
$class = data_get($statusType, 'class', null);
if (! $class) {
throw new \Exception('Source Missing Class');
}
$sourceType = $this->getSourceTypeClass();

logger('Running Source '.$class);
//@TODO make this check in uses BaseSourceType
$sourceType = app($class, [
'source' => $this,
]);
/** @var BaseSourceType $sourceType */
return $sourceType->handle();
return $sourceType->setPayload($payload)->handle();
} catch (\Exception $e) {
//@TODO This exception needs to be more specific
logger($e);
throw new SourceTypeMissingException();
}
Expand All @@ -75,4 +62,23 @@ public function getTypeFormattedAttribute()
{
return str($this->type->value)->headline();
}

protected function getSourceTypeClass(): BaseSourceType|\Exception
{
$statusTypes = config('larachain.sources');
$statusType = $this->type->value;
$statusType = data_get($statusTypes, $statusType);
$class = data_get($statusType, 'class', null);

if (! $class) {
throw new \Exception('Source Missing Class');
}

/** @var BaseSourceType $sourceType */
$sourceType = app($class, [
'source' => $this,
]);

return $sourceType;
}
}
5 changes: 5 additions & 0 deletions app/Providers/EventServiceProvider.php
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Events\SourceRunCompleteEvent;
use App\Listeners\RunTransformersListener;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -18,6 +20,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
SourceRunCompleteEvent::class => [
RunTransformersListener::class,
],
];

/**
Expand Down
1 change: 1 addition & 0 deletions app/Source/SourceEnum.php
Expand Up @@ -12,6 +12,7 @@ enum SourceEnum: string
use TypeTrait;

//case TemplateType = 'template_type'
case WebHook = 'web_hook';
case FileUploadSource = 'file_upload_source';
case ScrapeWebPage = 'scrape_web_page';
case WebFile = 'web_file';
Expand Down
17 changes: 16 additions & 1 deletion app/Source/Types/BaseSourceType.php
Expand Up @@ -8,11 +8,26 @@ abstract class BaseSourceType
{
protected Source $source;

public function __construct(Source $source)
protected array $payload = [];

public function __construct(
Source $source)
{
$this->source = $source;
}

public function setPayload(array $payload): self
{
$this->payload = $payload;

return $this;
}

public function getPayload(): array
{
return $this->payload;
}

abstract public function handle();

protected function getPath($fileName): string
Expand Down
30 changes: 30 additions & 0 deletions app/Source/Types/WebHook.php
@@ -0,0 +1,30 @@
<?php

namespace App\Source\Types;

use App\Ingress\StatusEnum;
use App\Jobs\ProcessSourceListeners;
use App\Models\Document;
use Ramsey\Uuid\Uuid;

class WebHook extends BaseSourceType
{
public function handle(): Document
{
$document = Document::create(
[
'guid' => Uuid::uuid4()->toString(),
'source_id' => $this->source->id,
'status' => StatusEnum::Complete,
'content' => json_encode($this->payload),
'meta_data' => [
'original_payload' => $this->payload,
],
]
);

ProcessSourceListeners::dispatch($document);

return $document;
}
}
11 changes: 6 additions & 5 deletions config/larachain.php
Expand Up @@ -18,11 +18,12 @@
'background' => 'bg-blue-500',
],
'web_hook' => [
'name' => 'Create a Webhook to Send Data',
'description' => 'Add a webhook here to send logs or other data.',
'icon' => 'Bars4Icon',
'active' => 0,
'background' => 'bg-blue-500',
'name' => 'WebHook',
'description' => 'You can take data from a webhook or trigger a listener',
'class' => 'App\\Source\\Types\\WebHook',
'requires' => [
],
'active' => 1,
],
's3_directory' => [
'name' => 'S3 Directory to Get Files From',
Expand Down
10 changes: 10 additions & 0 deletions database/factories/SourceFactory.php
Expand Up @@ -47,6 +47,16 @@ public function fileUpload()
});
}

public function webHook()
{
return $this->state(function (array $attributes) {
return [
'type' => SourceEnum::WebHook,
'meta_data' => [],
];
});
}

public function scrapeWebPage()
{
return $this->state(function (array $attributes) {
Expand Down

0 comments on commit cb6248a

Please sign in to comment.