Update
This commit is contained in:
111
src/CrawlshotClient.php
Normal file
111
src/CrawlshotClient.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace Crawlshot\Laravel;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class CrawlshotClient
|
||||
{
|
||||
protected string $baseUrl;
|
||||
protected ?string $token;
|
||||
|
||||
public function __construct(string $baseUrl, ?string $token = null)
|
||||
{
|
||||
$this->baseUrl = rtrim($baseUrl, '/');
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/crawl - Create crawl job
|
||||
*/
|
||||
public function createCrawl(string $url, array $options = []): array
|
||||
{
|
||||
return $this->makeRequest('POST', '/api/crawl', array_merge(['url' => $url], $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/crawl/{uuid} - Get crawl status
|
||||
*/
|
||||
public function getCrawlStatus(string $uuid): array
|
||||
{
|
||||
return $this->makeRequest('GET', "/api/crawl/{$uuid}");
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/crawl - List all crawl jobs
|
||||
*/
|
||||
public function listCrawls(): array
|
||||
{
|
||||
return $this->makeRequest('GET', '/api/crawl');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/shot - Create screenshot job
|
||||
*/
|
||||
public function createShot(string $url, array $options = []): array
|
||||
{
|
||||
return $this->makeRequest('POST', '/api/shot', array_merge(['url' => $url], $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/shot/{uuid} - Get screenshot status
|
||||
*/
|
||||
public function getShotStatus(string $uuid): array
|
||||
{
|
||||
return $this->makeRequest('GET', "/api/shot/{$uuid}");
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/shot/{uuid}/download - Download screenshot file
|
||||
*/
|
||||
public function downloadShot(string $uuid): string
|
||||
{
|
||||
$response = Http::when($this->token, function ($http) {
|
||||
return $http->withToken($this->token);
|
||||
})->get($this->baseUrl . "/api/shot/{$uuid}/download");
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new \Exception("Failed to download screenshot: " . $response->body());
|
||||
}
|
||||
|
||||
return $response->body();
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/shot - List all screenshot jobs
|
||||
*/
|
||||
public function listShots(): array
|
||||
{
|
||||
return $this->makeRequest('GET', '/api/shot');
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/health - Health check
|
||||
*/
|
||||
public function health(): array
|
||||
{
|
||||
return $this->makeRequest('GET', '/api/health', [], false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make HTTP request to API
|
||||
*/
|
||||
protected function makeRequest(string $method, string $endpoint, array $data = [], bool $requiresAuth = true): array
|
||||
{
|
||||
$http = Http::when($requiresAuth && $this->token, function ($http) {
|
||||
return $http->withToken($this->token);
|
||||
});
|
||||
|
||||
$response = match (strtoupper($method)) {
|
||||
'GET' => $http->get($this->baseUrl . $endpoint),
|
||||
'POST' => $http->post($this->baseUrl . $endpoint, $data),
|
||||
default => throw new \InvalidArgumentException("Unsupported HTTP method: {$method}")
|
||||
};
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new \Exception("API request failed: " . $response->body());
|
||||
}
|
||||
|
||||
return $response->json();
|
||||
}
|
||||
}
|
||||
39
src/CrawlshotServiceProvider.php
Normal file
39
src/CrawlshotServiceProvider.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Crawlshot\Laravel;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class CrawlshotServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
// Merge package configuration
|
||||
$this->mergeConfigFrom(__DIR__ . '/config/crawlshot.php', 'crawlshot');
|
||||
|
||||
// Register the client
|
||||
$this->app->singleton('crawlshot', function ($app) {
|
||||
return new CrawlshotClient(
|
||||
$app['config']['crawlshot']['base_url'],
|
||||
$app['config']['crawlshot']['token']
|
||||
);
|
||||
});
|
||||
|
||||
// Register facade alias
|
||||
$this->app->alias('crawlshot', CrawlshotClient::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
// Publish configuration
|
||||
$this->publishes([
|
||||
__DIR__ . '/config/crawlshot.php' => config_path('crawlshot.php'),
|
||||
], 'crawlshot-config');
|
||||
}
|
||||
}
|
||||
28
src/Facades/Crawlshot.php
Normal file
28
src/Facades/Crawlshot.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Crawlshot\Laravel\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @method static array createCrawl(string $url, array $options = [])
|
||||
* @method static array getCrawlStatus(string $uuid)
|
||||
* @method static array listCrawls()
|
||||
* @method static array createShot(string $url, array $options = [])
|
||||
* @method static array getShotStatus(string $uuid)
|
||||
* @method static string downloadShot(string $uuid)
|
||||
* @method static array listShots()
|
||||
* @method static array health()
|
||||
*
|
||||
* @see \Crawlshot\Laravel\CrawlshotClient
|
||||
*/
|
||||
class Crawlshot extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*/
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return 'crawlshot';
|
||||
}
|
||||
}
|
||||
32
src/config/crawlshot.php
Normal file
32
src/config/crawlshot.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Crawlshot API Configuration
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Configuration for the Crawlshot API client package.
|
||||
|
|
||||
*/
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Base URL
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The base URL of your Crawlshot API service.
|
||||
|
|
||||
*/
|
||||
'base_url' => env('CRAWLSHOT_BASE_URL', 'https://crawlshot.test'),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Authentication Token
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Your Sanctum authentication token for the Crawlshot API.
|
||||
|
|
||||
*/
|
||||
'token' => env('CRAWLSHOT_TOKEN'),
|
||||
];
|
||||
Reference in New Issue
Block a user