Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from alnutile/webhook
Webhook
- Loading branch information
Showing
20 changed files
with
693 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
app/Http/Controllers/Sources/WebHookSourceController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
*/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.