|
| 1 | +# Abstract client side php implementation of the JSON:API protocol. |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +```sh |
| 6 | +composer require dogado/json-api-client |
| 7 | +``` |
| 8 | + |
| 9 | +It's recommended to install `guzzlehttp/guzzle` version `^7.0` as http-client and `http-interop/http-factory-guzzle` for [PSR-17](https://www.php-fig.org/psr/psr-17/) compatible factories. |
| 10 | + |
| 11 | +```sh |
| 12 | +composer require guzzlehttp/guzzle http-interop/http-factory-guzzle |
| 13 | +``` |
| 14 | + |
| 15 | +You can also use any other HTTP client which implements [PSR-18](https://www.php-fig.org/psr/psr-18/). |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +First you should read the docs of [dogado/json-api-common](https://github.com/dogado-group/json-api-common/tree/main/docs) where all basic structures will be explained. |
| 20 | + |
| 21 | +Your API client is an instance of `Dogado\JsonApi\Client\JsonApiClient`, which requires a [PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP client (`Psr\Http\Client\ClientInterface`) to execute requests. |
| 22 | + |
| 23 | +```php |
| 24 | +use Dogado\JsonApi\Client\JsonApiClient; |
| 25 | +use Dogado\JsonApi\Client\Factory\RequestFactory; |
| 26 | +use Dogado\JsonApi\Client\Validator\ResponseValidator; |
| 27 | + |
| 28 | +$client = new JsonApiClient( |
| 29 | + $httpClient, // instance of Psr\Http\Client\ClientInterface |
| 30 | + $psrRequestFactory, // instance of Psr\Http\Message\RequestFactoryInterface |
| 31 | + $streamFactory, // instance of Psr\Http\Message\StreamFactoryInterface |
| 32 | + $serializer, // instance of Dogado\JsonApi\Serializer\DocumentSerializerInterface |
| 33 | + $responseFactory, // instance of Dogado\JsonApi\Client\Response\ResponseFactoryInterface |
| 34 | +); |
| 35 | + |
| 36 | +$baseUrl = new Uri('http://example.com/api'); |
| 37 | +$requestFactory = new RequestFactory($baseUrl); |
| 38 | + |
| 39 | +$request = $requestFactory->createGetRequest(new Uri('/myResource/1')); // will fetch the resource at http://example.com/api/myResource/1 |
| 40 | +$response = $client->execute($request); |
| 41 | + |
| 42 | +// OPTIONAL: Validate the response to match your needs. See the ResponseValidator class for all assertion methods |
| 43 | +(new ResponseValidator())->assertResourcesMatchTypeAndContainIds($response, 'myResource'); |
| 44 | + |
| 45 | +$document = $response->document(); |
| 46 | +$myResource = $document->data()->first(); // the resource fetched by this request |
| 47 | +$myIncludedResources = $document->included()->all(); // the included resources fetched with the include parameter |
| 48 | +``` |
| 49 | + |
| 50 | +### Action Pattern |
| 51 | + |
| 52 | +In most cases it's easier to capsule request scenarios into single classes since every request has its own requirements. |
| 53 | +In this package this is called `Action`. To make things easier, we already defined an `AbstractAction` class under the |
| 54 | +`Dogado\JsonApi\Client\Action` namespace. An example how to create such an action can be found |
| 55 | +[in the tests](./tests/Action/DummyAction.php). |
| 56 | + |
| 57 | +When fetching resources in actions it's also very common to filter, paginate and sort. To define these options within Actions, |
| 58 | +there are multiple Traits you can use, defined in the same namespace as the `AbstractAction` class. |
| 59 | + |
| 60 | +## Credits |
| 61 | + |
| 62 | +- [Chris Döhring](https://github.com/chris-doehring) |
| 63 | +- [Philipp Marien](https://github.com/pmarien) |
| 64 | +- [eosnewmedia team](https://github.com/eosnewmedia) |
| 65 | + |
| 66 | +This package contains code taken from [enm/json-api-client](https://github.com/eosnewmedia/JSON-API-Client). |
| 67 | + |
| 68 | +## License |
| 69 | + |
| 70 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments