Covers what is an open standard, what standards we use in our day to day work. Describes in detail what JSON-API is and how utilizing it can help us build more maintainable and easier to consume APIs.
OPEN STANDARDS What makes an open standard ? Cooperation Adherence to Principles Due process, Broad concensus, Transparency, Balance, Openness Collective Empowerment Availability Voluntary Adoption 4 . 1
OPEN STANDARDS AND APIS Auth and security Oauth 1/2, OpenID Connect, JWT Documentation / helper openapi/swagger, api blueprint, RAML, json- schema Representation / content types HAL, Problem JSON, Hydra and json-ld, JSON-API Other GraphQL 5
OVERVIEW well-de ned conventions for listing, creating, updating and deleting resources recommendations for details not covered in the main spec server and client libraries in most popular languages 8
THE MEDIA TYPE Client call Server response Accept: application/vnd.api+json Content-Type: application/vnd.api+json Content-type: application/vnd.api+json 11 . 1
A 404 ERROR RESPONSE GET /meetups/1234 Content-Type: application/vnd.api+json Accept: application/vnd.api+json HTTP/1.1 404 Not Found Content-Type: application/vnd.api+json { "errors": [ { "title": "Resource Not Found", "content": "There is no event with id 1234" } ] } 18 . 5
De ning includes use League\Fractal\TransformerAbstract; use League\Fractal\Resource\Collection; class MeetupTransformer extends TransformerAbstract { protected $availableIncludes = ['venues']; public function transform($meetup) { ... } public function includeVenue($meetup) { $venue = $meetup->venue; $transformer = new VenueTransformer; return $this->item($venue, $transformer, 'venues'); } } $data = new Collection($meetups, new MeetupTransformer); 21 . 3
Creating a resource use League\Fractal\Serializer\JsonApiSerializer; $manager = new League\Fractal\Manager(); $baseUrl = 'http://meetup.dev'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); // ORM call $meetup = Meetup::find(1); // Make a resource out of the data and $resource = new Item($meetup, new MeetupTransformer, 'meetups'); // Run all transformers $manager->createData($resource)->toArray(); 21 . 4
PAGINATION use League\Fractal\Resource\Collection; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use Acme\Model\Book; use Acme\Transformer\BookTransformer; $paginator = Meetup::paginate(); $meetups = $paginator->getCollection(); $resource = new Collection($meetups, new MeetupTransformer); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); 22 . 2
Use a base class class JsonApiError extends \Exception { protected $status; protected $title; protected $detail; protected $source; protected $meta; public function toArray() {...} } 24 . 1
RESOURCES http://jsonapi.org/ http://fractal.thephpleague.com/ https://www.ics.uci.edu/~ elding/pubs/dissertation/ e Steve Klabnik's talk: past, present and future of json-ap https://youtu.be/Foi54om6oGQ discussion: who's using json-api - https://github.com/jso api/issues/825 27