creating hypermedia apis in a few minutes using the api platform framework

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

Upload: les-tilleulscoop

Post on 16-Apr-2017

3.787 views

Category:

Software


8 download

TRANSCRIPT

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

API PLATFORMThe PHP framework to buildmodern web APIs.

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

Kévin DunglasFounder of Les-Tilleuls.coop

Symfony Core Team member

API Platform creator

Teacher at the University of Lille 1

@dunglas

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

Les-Tilleuls.coopSelf-managed company since 2011100% owned by employees

All benefits are equitably shared between employees18 people, 137% growth in 2015

We are hiring! => [email protected]

gregcop1
Tu ne mettrais pas le logo d'angular plutôt que celui là ? Je pense qu'on en fait plus que du Go :p
Page 4: Creating hypermedia APIs in a few minutes using the API Platform framework

The PromiseSupport for modern and future formatsBatteries included: pagination, filtering, auth (JWT, OAuth), HTTP Cache, CORS…UI and automatic documentation (Swagger)Extensible, overridable, customizable

A working API in a few minutes

gregcop1
Features
Page 5: Creating hypermedia APIs in a few minutes using the API Platform framework

API: State of the Art

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

HTTP + REST + JSONWorks easily with all programming languages, on every platformsLightweight (= fast)

Stateless (= scale)

Easy to cache (= faster)

High quality tooling (cURL, Varnish…)

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

REST: a Pattern, Not a Format

1 project ~= 1 different implementationTedious 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

gregcop1
to develop?
Page 8: Creating hypermedia APIs in a few minutes using the API Platform framework

Credit: Martin Fowlerhttp://martinfowler.com/articles/richardsonMaturityModel.html

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

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

Page 10: Creating hypermedia APIs in a few minutes using the API Platform framework
Page 11: Creating hypermedia APIs in a few minutes using the API Platform framework

Standards Please!

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

Swagger / OpenAPI

Describe only I/O formats and available operations for an APIMost popular way to describe APIsLot of tools available (UIs, code generators, validators…)

Open standard (but not endorsed by the W3C)

Not designed for HATEOAS APIs

gregcop1
Endorsed c’est plus exact (c’est fait par un autre organisme)approved ?
Page 13: Creating hypermedia APIs in a few minutes using the API Platform framework

JSON-LDStandard: 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

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

Schema.org

Large set of elements: people, creative works, events, products, chemicals...Created (and understood by) Google, Bing, Yahoo! et YandexMassively 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)

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

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

gregcop1
auto-discoverable
Page 16: Creating hypermedia APIs in a few minutes using the API Platform framework
Page 17: Creating hypermedia APIs in a few minutes using the API Platform framework
Page 18: Creating hypermedia APIs in a few minutes using the API Platform framework

Getting Started with

API PlatformA framework for the new web.

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

The PromiseSupport 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

gregcop1
Features
Page 20: Creating hypermedia APIs in a few minutes using the API Platform framework

InstallGet Docker

If you don’t already have it…

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

InstallStart 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.

Page 22: Creating hypermedia APIs in a few minutes using the API Platform framework
Page 23: Creating hypermedia APIs in a few minutes using the API Platform framework

It’s Symfony…

Configured with the most popular librariesfor APIsCompatible with all existing bundlesUse Doctrine ORM by default (but you canuse the persistence system you want)

Symfony full stack application

…with something more

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

Create your Own Data Model

Write some PHPDoc (optional)

Add the @ApiResource annotation

Map its properties using theDoctrine ORM

Update the database schema

$ docker-compose run web bin/console doctrine:schema:update

--force

Create a Plain Old PHP Object

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

Your 1st API Platform App is

Up and Running!

Page 26: Creating hypermedia APIs in a few minutes using the API Platform framework
Page 27: Creating hypermedia APIs in a few minutes using the API Platform framework

Out of the Box Features

JSON-LD + Hydra formatsSwagger 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

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

Addvalidation rules

Many validation constraints available

Ability to create custom constraints

Use the Symfony Validatorcomponent

Page 29: Creating hypermedia APIs in a few minutes using the API Platform framework
Page 30: Creating hypermedia APIs in a few minutes using the API Platform framework

The Schema GeneratorPick an existing data model from (resources and properties) from schema.org:

docker-compose run webvendor/bin/schema generate-types src/ app/config/schema.yml

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

The Schema Generator

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

The Schema Generator

PHP classes, properties, getters and setters (PSR compliant)Doctrine ORM mapping (including relations and mapped superclasses)Validation constraintsFull PHPDoc extracted from schema human-readable descriptionsMapping with schema.org's IRIs

The generator uses schema.org data to automatically bootstrap:

Relations between classes (supported by the API system too)

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

Hypermedia relations

Page 34: Creating hypermedia APIs in a few minutes using the API Platform framework
Page 35: Creating hypermedia APIs in a few minutes using the API Platform framework

Embedded relations

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

Embedded relations

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

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

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

Content Negotiation

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

Content Negotiation: HAL

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

Content Negotiation: XML

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

FiltersRegister the filter service (built-in or custom):

Map the filter to the resource

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

Filters

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

The Event System

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

Events

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

There is Much More to See

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

They Already Use API Platform

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

A Growing Community100+ awesome contributors (code and docs)1.2k stars on GitHub

500+ followers on Twitter

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

Next Big Things (v2.1)

JSONAPI support

GraphQL support

Native MongoDB support

Your contribution?

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

Thanks!Any questions?

api-platform/api-platform

@ApiPlatform

https://api-platform.com