Creating hypermedia APIs in a few minutes using the API Platform framework

50 %
50 %
Information about Creating hypermedia APIs in a few minutes using the API Platform framework

Published on December 13, 2016

Author: coopTilleuls

Source: slideshare.net

1. API PLATFORM The PHP framework to build modern web APIs.

2. Kévin Dunglas Founder of Les-Tilleuls.coop Symfony Core Team member API Platform creator Teacher at the University of Lille 1 @dunglas

3. Les-Tilleuls.coop Self-managed company since 2011 100% owned by employees All benefits are equitably shared between employees 18 people, 137% growth in 2015 We are hiring! => jobs@les-tilleuls.coop

4. The Promise Support for modern and future formats Batteries included: pagination, filtering, auth (JWT, OAuth), HTTP Cache, CORS… UI and automatic documentation (Swagger) Extensible, overridable, customizable A working API in a few minutes

5. API: State of the Art

6. HTTP + REST + JSON Works easily with all programming languages, on every platforms Lightweight (= fast) Stateless (= scale) Easy to cache (= faster) High quality tooling (cURL, Varnish…)

7. REST: a Pattern, Not a Format 1 project ~= 1 different implementation Tedious to create: pagination, filtering, validation, caching, content negotiation, CORS, data interoperability… Hard to reuse server-side tooling (1 API per project) Hard to reuse client-side tooling (1 client per API) Hard to query and aggregate data from different sources

8. Credit: Martin Fowler http://martinfowler.com/articles/richardsonMaturityModel.html

9. HATEOAS / Linked Data Hypermedia: IRIs (e.g. URLs) as identifiers Ability to reference external data (like hypertext links) Hypermedia controls (pagination, filtering…) The road to better server-side tooling and generic clients Hypermedia as the Engine of Application State

10. Standards Please!

11. Swagger / OpenAPI Describe only I/O formats and available operations for an API Most popular way to describe APIs Lot of tools available (UIs, code generators, validators…) Open standard (but not endorsed by the W3C) Not designed for HATEOAS APIs

12. JSON-LD Standard: W3C recommandation (since 2014) Easy to use: looks like a typical JSON document Already chosen by Google, BBC, Microsoft, US gov... Compliant with technologies of the semantic web: RDF, SPARQL, triple store... JSON for Linked Data

13. Schema.org Large set of elements: people, creative works, events, products, chemicals... Created (and understood by) Google, Bing, Yahoo! et Yandex Massively used, and hosted by the W3C (Web schemas group) Supports HTML’s microdata, RDFa and JSON-LD Open vocabulary for data interoperability at web scale Extension mechanism, compatible with proprietary/custom vocabularies (yours)

14. Hydra Write support (POST, PUT, PATCH…) Make APIs auto-discoverable (all available operations are documented) A standard for describing collections, paginations, filters, errors… Draft W3C (Work In Progress) Describe REST APIs in JSON-LD

15. Getting Started with API Platform A framework for the new web.

16. The Promise Support for modern and future formats (JSON-LD, Hydra, HAL, schema.org, API+Problem…) Batteries included: pagination, filtering, auth (JWT, OAuth), HTTP Cache, CORS… Awesome UI and automatic documentation (Swagger) Extensible, overridable, customizable A working API in a few minutes

17. Install Get Docker If you don’t already have it…

18. Install Start the LAMP stack (PHP7, Apache and MySQL) $ docker-compose up # Create the database $ docker-compose exec web bin/console doctrine:schema:create Go to api-platform.com then click « Download » Done! Browse http://localhost.

19. It’s Symfony… Configured with the most popular libraries for APIs Compatible with all existing bundles Use Doctrine ORM by default (but you can use the persistence system you want) Symfony full stack application …with something more

20. Create your Own Data Model Write some PHPDoc (optional) Add the @ApiResource annotation Map its properties using the Doctrine ORM Update the database schema $ docker-compose run web bin/console doctrine:schema:update --force Create a Plain Old PHP Object

21. Your 1st API Platform App is Up and Running!

22. Out of the Box Features JSON-LD + Hydra formats Swagger documentation Fully featured UI (working for all URLs of the API) Create (POST), Retrieve (GET item and lists), Update (PUT) and Delete (DELETE) resources Pagination for lists (30 items per page), fully configurable

23. Add validation rules Many validation constraints available Ability to create custom constraints Use the Symfony Validator component

24. The Schema Generator Pick an existing data model from (resources and properties) from schema.org: docker-compose run web vendor/bin/schema generate-types src/ app/config/schema.yml

25. The Schema Generator

26. The Schema Generator PHP classes, properties, getters and setters (PSR compliant) Doctrine ORM mapping (including relations and mapped superclasses) Validation constraints Full PHPDoc extracted from schema human-readable descriptions Mapping with schema.org's IRIs The generator uses schema.org data to automatically bootstrap: Relations between classes (supported by the API system too)

27. Hypermedia relations

28. Embedded relations

29. Embedded relations

30. Content Negotiation Adding a new format is as simple as creating a new Symfony Normalizer for it Built-in formats: JSON-LD, HAL, XML, YAML, CSV, JSON, HTML (UI) To retrieve a resource in a given format: add an Accept HTTP header or use the format name as file extension

31. Content Negotiation

32. Content Negotiation: HAL

33. Content Negotiation: XML

34. Filters Register the filter service (built-in or custom): Map the filter to the resource

35. Filters

36. The Event System

37. Events

38. There is Much More to See

39. Platform

40. A Growing Community 100+ awesome contributors (code and docs) 1.2k stars on GitHub 500+ followers on Twitter

41. Next Big Things (v2.1) JSONAPI support GraphQL support Native MongoDB support Your contribution?

42. Thanks! Any questions? api-platform/api-platform @ApiPlatform https://api-platform.com

Add a comment