diff --git a/README.md b/README.md index 1415b8a..be9b3a6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,107 @@ # Replicate PHP client -This is a PHP client for the [Replicate](https://replicate.com/) . \ No newline at end of file +This is a simple PHP client for the [Replicate](https://replicate.com/). It covers the main prediction functionalities +of the [Replicate HTTP API](https://replicate.com/docs/reference/http). + +## Requirements + +- PHP 8.1 + +## Installation + +Install the package with composer: + +```bash +composer require replicate/replicate-php +``` + +## Usage + +### Initialize the replicate client with your API token + +Get your API token from your [Replicate account](https://replicate.com/account). + +```php +$replicate = new Replicate('token'); +``` + +### Create a prediction + +```php +try { + $prediction = $replicate->createPrediction( + version: 'v1', + input: ['text' => 'foo'], + ); + + echo $prediction->id; // take a look at Prediction data class for available fields +} catch (ReplicateException|ReplicateWebhookInputException|ResponseException $e) { + echo $e->getMessage(); +} +``` + +### Get a prediction + +```php +try { + $prediction = $this->replicate->prediction('prediction-id'); + + echo $prediction->id; +} catch (ReplicateException|ResponseException $e) { + // log error +} +``` + +### Get a list of predictions + +```php +try { + $predictions = $this->replicate->predictions(); + + // if you would like to paginate. + if ($predictions->next) { + + // extract the cursor from the next url + $nextUrl = $predictions->next; + $query = parse_url($nextUrl, PHP_URL_QUERY); + parse_str($query, $params); + $cursor = $params['cursor']; + + $predictions = $this->replicate->predictions($cursor); + } + + // take a look at the Predictions data class for available fields. +} catch (ReplicateException|ResponseException $e) { + // log error +} +``` + +### Cancel a prediction + +```php +try { + $response = $this->replicate->cancelPrediction('prediction-id'); + + echo $response->status; +} catch (ReplicateException|ResponseException $e) { + // log error +} +``` + +## Development + +### Install dependencies + +```bash +composer install +``` + +### Run tests + +```bash +composer test +``` + +## License + +MIT \ No newline at end of file diff --git a/examples/ExampleService.php b/examples/ExampleService.php index b6a2464..e127501 100644 --- a/examples/ExampleService.php +++ b/examples/ExampleService.php @@ -18,8 +18,6 @@ public function __construct() public function exampleCreateAPrediction(): void { - $this->replicate = new Replicate('token'); - try { $prediction = $this->replicate->createPrediction( version: 'v1', diff --git a/tests/Validators/ReplicateWebhookValidatorTest.php b/tests/Validators/ReplicateWebhookValidatorTest.php index db27e70..e1f2e1d 100644 --- a/tests/Validators/ReplicateWebhookValidatorTest.php +++ b/tests/Validators/ReplicateWebhookValidatorTest.php @@ -14,7 +14,6 @@ public function test_validate(): void { $validator = new ReplicateWebhookValidator(); - // Test valid input $input = [ 'webhook' => 'https://example.com', 'webhook_events_filter' => ['start', 'output', 'logs', 'completed']