Skip to content

Commit

Permalink
Merge branch 'release/3.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Marien committed Sep 27, 2018
2 parents 0d10354 + 4b6ee0e commit 7fa691d
Show file tree
Hide file tree
Showing 15 changed files with 582 additions and 1,288 deletions.
1 change: 0 additions & 1 deletion .sensiolabs.yml

This file was deleted.

9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
language: php

php:
- 7.0
- 7.1

env:
- JSON API Client
- 7.2

before_script:
- composer self-update
- composer install
- composer install

script: php vendor/bin/phpunit --coverage-text
64 changes: 13 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
JSON API Client
===============
[![Build Status](https://travis-ci.org/eosnewmedia/JSON-API-Client.svg?branch=master)](https://travis-ci.org/eosnewmedia/JSON-API-Client) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/d6c28b22-fa18-4a74-8204-0e91d205781a/mini.png)](https://insight.sensiolabs.com/projects/d6c28b22-fa18-4a74-8204-0e91d205781a)
[![Build Status](https://travis-ci.org/eosnewmedia/JSON-API-Client.svg?branch=master)](https://travis-ci.org/eosnewmedia/JSON-API-Client)

Abstract client-side PHP implementation of the [json api specification](http://jsonapi.org/format/), based on the [PSR-7 HTTP message interface](http://www.php-fig.org/psr/psr-7/).
Abstract client-side PHP implementation of the [json api specification](http://jsonapi.org/format/).

## Installation

Expand All @@ -27,62 +27,24 @@ If needed you can also implement the interface by yourself to use any HTTP clien
## Usage
First you should read the docs at [enm/json-api-common](https://eosnewmedia.github.io/JSON-API-Common/) where all basic structures are defined.

Your API client for sending requests to a JSON API and get validated responses as JSON API documents is an instance of
`Enm\JsonApi\Client\JsonApiClient`, which requires a HTTP client (`Enm\JsonApi\HttpClient\HttpClientInterface`) to execute
requests.

| Method | Return Type | Description |
|--------------------------------------------------------------------------------------------------------|-------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| createJsonApiRequest(string $type, string $id = '') | JsonApiRequestInterface | Create a new JSON API request object, needed for a delete request. |
| createFetchRequest(string $type, string $id = '') | FetchRequestInterface | Create a new fetch request object, needed for fetch requests. |
| createSaveSingleResourceRequest(ResourceInterface $resource, bool $patch = false) | SaveRequestInterface | Create a new save request object, needed for create or patch requests. |
| fetch(FetchRequestInterface $request) | DocumentInterface | Execute a fetch request for one or many resources and transform the server response into a JSON API document. |
| save(SaveRequestInterface $request) | DocumentInterface | Execute a save (create or patch) request and transform the server response into a JSON API document. |
| delete(JsonApiRequestInterface $request) | DocumentInterface | Execute a delete request and transform the server response into a JSON API document. |
| fetchRelationship(string $relationship, FetchRequestInterface $request, bool $onlyIdentifiers = false) | DocumentInterface | Execute a fetch request for a relationship and transform the server response into a JSON API document. |
| follow(LinkInterface $link, array $headers = []) | DocumentInterface | Execute a fetch request which is defined by a JSON API link object and transform the server response into a JSON API document. |

Your API client is an instance of `Enm\JsonApi\Client\JsonApiClient`, which requires a HTTP client (`Enm\JsonApi\HttpClient\HttpClientInterface`) to execute requests.

```php
// configure http client and api endpoint
$guzzle = new Client(); // if you are using guzzle htttp
$baseUri = 'http://example.com/api' // the base uri for all api requests

// create the api client
$apiClient = new JsonApiClient($baseUri, new GuzzleAdapter($guzzle));
$client = new JsonApiClient(
'http://example.com/api',
new GuzzleAdapter(new Client()), // with guzzle in this example...
new Serializer(),
new Deserializer()
);

// create a fetch request to retrieve a single resource
$request = $apiClient->createFetchRequest('myResourceType', 'myResource');
$request->include('myRelationship'); // include a relationship
$request = $client->createGetRequest(new Uri('/myResources/1')); // will fetch the resource at http://example.com/api/myResources/1
$request->requestInclude('myRelationship'); // include a relationship

// get a response from server
$document = $apiClient->fetch($request);
$response = $client->execute($request);

$document = $response->document();
$myResource = $document->data()->first(); // the resource fetched by this request
$myIncludedResources = $document->included()->all(); // the included resources fetched with the include parameter

##############

// create a fetch request to retrieve all resource of a type
$requestAll = $apiClient->createFetchRequest('myResourceType');

// get a response from server
$collectionDocument = $apiClient->fetch($request);
$resources = $collectionDocument->data()->all(); // all resources from the current response

##############

// create a new resource
$resource = $apiClient->resource('examples', 'example-1');

$request = $apiClient->createSaveSingleResourceRequest($resource);
$response = $apiClient->save($request);

// update the created resource
$resource = $response->data()->first(); // get the created resource from response
$resource->attributes()->set('title', 'Example');

$request = $apiClient->createSaveSingleResourceRequest($resource);
$response = $apiClient->save($request);

```
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@
}
},
"require": {
"enm/json-api-common": "^2.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0",
"guzzlehttp/psr7": "^1.0"
"enm/json-api-common": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^6.2",
"phpunit/phpunit": "^7.0",
"guzzlehttp/guzzle": "^6.0",
"kriswallsmith/buzz": "^0.16.0"
},
Expand Down
Loading

0 comments on commit 7fa691d

Please sign in to comment.