From f8713cd50f58e6cb05e802b187d676b001cf7316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20San=20Martin?= Date: Wed, 27 Mar 2024 14:43:39 -0300 Subject: [PATCH 1/6] Adds composer test script to run all tests --- composer.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b72e6ea..78a4ccb 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,13 @@ "scripts": { "test-cs": "vendor/bin/php-cs-fixer check --rules=@Symfony,@PER-CS2.0 .", "test-cs-fix": "vendor/bin/php-cs-fixer fix --rules=@Symfony,@PER-CS2.0 .", + "test-phpstan": "vendor/bin/phpstan analyse src -l 9", "test-phpunit": "vendor/bin/phpunit tests/unit/", "test-phpunit-coverage": "vendor/bin/phpunit --coverage-html tests/coverage --coverage-filter src/ tests/unit/", - "test-phpstan": "vendor/bin/phpstan analyse src -l 9" + "test": [ + "composer test-cs", + "composer test-phpstan", + "composer test-phpunit-coverage" + ] } } From a42ee87ca4c5fc563d5bb7f95c79a82063c86702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20San=20Martin?= Date: Wed, 27 Mar 2024 14:43:52 -0300 Subject: [PATCH 2/6] Fixes CS --- tests/unit/src/SlashIdSdkTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/src/SlashIdSdkTest.php b/tests/unit/src/SlashIdSdkTest.php index 499a07a..6775de1 100644 --- a/tests/unit/src/SlashIdSdkTest.php +++ b/tests/unit/src/SlashIdSdkTest.php @@ -226,5 +226,4 @@ public function testRequestException(string $targetUrl, int $responseHttpCode, s $sdk->get($targetUrl); } - } From 37e8b624e48d3b4aafc3ed0ef877353851b2a6b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20San=20Martin?= Date: Wed, 27 Mar 2024 15:01:43 -0300 Subject: [PATCH 3/6] Adds documentation about exceptions --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index a9197ad..adddd4e 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,39 @@ $sdk->delete('/organizations/webhooks/99999-99999-99999/triggers', [ ]); ``` +### Exceptions + +The following exceptions may be thrown in case of errors during the connection: + +* `\SlashId\Php\Exception\BadRequestException` when the API returns a **400** error, meaning that the data you've sent to the request is malformed or missing required information. +* `\SlashId\Php\Exception\BadRequestException` when the API returns a **401** error, meaning that either the Organization ID, API key or environment are wrong. In this case, check your credentials. +* `\SlashId\Php\Exception\AccessDeniedException` when the API returns a **403** error, meaning you are not allowed to perform an operation. +* `\SlashId\Php\Exception\InvalidEndpointException` when the API returns a **404** error due to you requesting an endpoint that does not exist. In this case, check the [API reference](https://developer.slashid.dev/docs/api). +* `\SlashId\Php\Exception\BadRequestException` when the API returns a **404** error on a valid endpoint, meaning the ID you've requested does not exist. This exception will happen on requests that include an ID in the URL, such as `/persons/1111-1111-1111`. +* `\SlashId\Php\Exception\ConflictException` when the API returns a **409** error, usually meaning you are trying to create a duplicated entity (e.g. a person with an email already belonging to an existing person). In this case, check the [API reference](https://developer.slashid.dev/docs/api) to see if there is an idempotent version of the endpoint. +* `\GuzzleHttp\Exception\ClientException` when the API returns any other **4xx** error. +* `\GuzzleHttp\Exception\BadResponseException` when the API returns any **5xx** error. +* Some implementation of `\GuzzleHttp\Exception\GuzzleException` if there is any other kind of error during the connection with the API server. + +All of `\SlashId\Php\Exception` exceptions are descendants of `\GuzzleHttp\Exception\BadResponseException`, which means that you can use the following methods to learn about the causes of the error: + +```php +// Gets an informative message about the error. +$exception->getMessage(); + +// Gets the request object, with information about the endpoint and the data in the request. +$request = $exception->getRequest(); + +// Gets the response object, with HTTP response code and the response contents. +$response = $exception->getResponse(); + +// Gets the response as text. +$responseText = (string) $exception->getResponse()->getBody(); + +// Gets the response as a parsed array. +$responseData = \json_decode((string) $exception->getResponse()->getBody(), true); +``` + ### Webhook Abstraction The webhook abstraction is a class to help working with webhooks, for creating, listing and deleting them, and also adding and removing triggers. From 7ae922a3a0faa13cf760e1ed269e7de881895e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20San=20Martin?= Date: Wed, 27 Mar 2024 15:03:48 -0300 Subject: [PATCH 4/6] Removes push trigger --- .github/workflows/phpcs.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml index df67d4e..aa7ac0f 100644 --- a/.github/workflows/phpcs.yml +++ b/.github/workflows/phpcs.yml @@ -1,5 +1,4 @@ name: PHP CS -on: [push] jobs: phpcs: runs-on: ubuntu-latest From 816ebfaabdac82f208ca00a71a4e3e02e4c85ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20San=20Martin?= Date: Wed, 27 Mar 2024 15:06:04 -0300 Subject: [PATCH 5/6] Fixes usage of client in the WebhookAbstraction --- src/Abstraction/WebhookAbstraction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Abstraction/WebhookAbstraction.php b/src/Abstraction/WebhookAbstraction.php index 4a8b237..d0408b5 100644 --- a/src/Abstraction/WebhookAbstraction.php +++ b/src/Abstraction/WebhookAbstraction.php @@ -289,7 +289,7 @@ public function decodeWebhookCall( ): array { $keySet = new CachedKeySet( $this->sdk->getApiUrl() . '/organizations/webhooks/verification-jwks', - $this->client, + $this->sdk->getClient(), new HttpFactory(), $cache, $expiresAfter, From c81fb0c16c89b865ac66b7e2a32e9f5eb3eeb653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20San=20Martin?= Date: Wed, 27 Mar 2024 15:16:00 -0300 Subject: [PATCH 6/6] Adds documentation regarding getClient() --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index adddd4e..b6e3b74 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,22 @@ $sdk->delete('/organizations/webhooks/99999-99999-99999/triggers', [ ]); ``` +#### `getClient()` + +The methods `$sdk->get()`, `$sdk->post()`, `$sdk->patch()`, `$sdk->put()` and `$sdk->delete()` expect sending and receiving JSON, but some endpoints have special requirements, e.g. `GET /persons/bulk-import` ([Fetch the import CSV template](https://developer.slashid.dev/docs/api/get-persons-bulk-import)) will return a CSV and requires a `Accept: text/csv` header. + +For those cases, you can use `$sdk->getClient()` to retrieve the underlying [GuzzlePHP](https://docs.guzzlephp.org) client with the proper credentials preset, for instance: + +```php +$response = $sdk->getClient()->request('GET', '/persons/bulk-import', [ + 'headers' => [ + 'Accept' => 'text/csv', + ], +]); + +$csvContents = (string) $response->getBody(); +``` + ### Exceptions The following exceptions may be thrown in case of errors during the connection: