Slide 1

Slide 1 text

Leçon n°139, API-Platform Ce n’est bon qu’à faire un POC ! Faux !

Slide 2

Slide 2 text

Grégoire Hébert ‣Senior Developper & Trainer @ Les-Tilleuls.coop ‣CEO of MasterClass by Les-Tilleuls.coop @gheb_dev @gregoirehebert

Slide 3

Slide 3 text

Self Managed since 2011 35 people, + 1000% in 6 years [email protected] 13 full courses & custom courses [email protected]

Slide 4

Slide 4 text

R.A.D.

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

R.A.D.

Slide 11

Slide 11 text

Let’s see why it’s worth it!

Slide 12

Slide 12 text

Richardson’s Maturity Chart Graphql I see you…

Slide 13

Slide 13 text

LvL 0: Always the same URL in POST with a 200 return status code. POST: /api

Slide 14

Slide 14 text

Richardson’s Maturity Chart

Slide 15

Slide 15 text

LvL 1: Each type of data has its own URL. POST: /books POST: /customers

Slide 16

Slide 16 text

Richardson’s Maturity Chart

Slide 17

Slide 17 text

LvL 2: We use specific verbs for specific actions with status codes according to the result. GET: /books POST: /books PUT: /books/12

Slide 18

Slide 18 text

Richardson’s Maturity Chart

Slide 19

Slide 19 text

LvL 3: Hateoas - Hypermedia As The Engine Of Application State. We add links to describe how to get to the information in the answer. { "@context": { "comments": "http://api.example.com/vocab#comments" }, "@id": "http://api.example.com/an-issue", "title": "An exemplary issue linking to its comments", "comments": { "@id": "http://api.example.com/an-issue/comments" } }

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

V1 - DunglasJsonLdApiBundle Mars 2015

Slide 22

Slide 22 text

V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website

Slide 23

Slide 23 text

V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website

Slide 24

Slide 24 text

V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website

Slide 25

Slide 25 text

V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website Septembre 2015 V2 Annonced

Slide 26

Slide 26 text

V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available

Slide 27

Slide 27 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available

Slide 28

Slide 28 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available

Slide 29

Slide 29 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available

Slide 30

Slide 30 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available

Slide 31

Slide 31 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available V2.2 February 2018

Slide 32

Slide 32 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available V2.2 February 2018

Slide 33

Slide 33 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available V2.2 February 2018

Slide 34

Slide 34 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available V2.2 February 2018

Slide 35

Slide 35 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available February 2018 V2.2 February 2019 V2.4 (beta2)

Slide 36

Slide 36 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available February 2018 V2.2 February 2019 V2.4

Slide 37

Slide 37 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available February 2018 V2.2 February 2019 V2.4

Slide 38

Slide 38 text

Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available January 2017 V2 Stable Available February 2018 V2.2 February 2019 V2.4

Slide 39

Slide 39 text

First ApiResource

Slide 40

Slide 40 text

You’ve got the power

Slide 41

Slide 41 text

Some attributes

Slide 42

Slide 42 text

Any Public, Protected, Private property will be accessible through your API 
 As long as you define the means to access it ! THANKS TO… ?

Slide 43

Slide 43 text

Restricting the operations

Slide 44

Slide 44 text

Restricting the operations

Slide 45

Slide 45 text

Restricting the operations

Slide 46

Slide 46 text

Differentiating the expected income and the outcome

Slide 47

Slide 47 text

Source: https://symfony.com/doc/current/components/serializer.html

Slide 48

Slide 48 text

Differentiating the expected income and the outcome

Slide 49

Slide 49 text

ORM mapping

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

R.A.D.

Slide 52

Slide 52 text

Ok, but not everything is about CRUD or REST !

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Activate Mercure for a resource

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

source: Samuel Roze « Symfony Messenger: Messages, Queues, Workers and More »

Slide 58

Slide 58 text

Activate messenger for a resource

Slide 59

Slide 59 text

Defining a Handler

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Configuring Messenger component

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Going Further

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Defining states constants

Slide 71

Slide 71 text

Configuration using constants

Slide 72

Slide 72 text

Configuration using constants

Slide 73

Slide 73 text

How to get for any resource a way to update the state ?

Slide 74

Slide 74 text

Leverage the extensibility of 
 API Platform

Slide 75

Slide 75 text

Decorating the ResourceMetadataFactory

Slide 76

Slide 76 text

Inject the supported resources

Slide 77

Slide 77 text

Decorating the OperationPathResolver

Slide 78

Slide 78 text

Change the state on post deserialize

Slide 79

Slide 79 text

Change the state on post deserialize

Slide 80

Slide 80 text

I get a new operation to update the state

Slide 81

Slide 81 text

Going Further (again)

Slide 82

Slide 82 text

Let’s consider we have Holder resource
 which is used as a User, with a JWT Authentication

Slide 83

Slide 83 text

Reverse side Linking a WaitingLine to a Holder

Slide 84

Slide 84 text

What if I want to set/remove ownership right after the change of state 
 with API Platform?

Slide 85

Slide 85 text

YOU DON’T.

Slide 86

Slide 86 text

Setting the ownership

Slide 87

Slide 87 text

Don’t forget to leverage the power of the Symfony components you use. They are highly tested !

Slide 88

Slide 88 text

A little demo maybe?

Slide 89

Slide 89 text

Going Further (again, again)
 promised, this is the last time

Slide 90

Slide 90 text

Sometimes, you need to split up your code. Let’s imagine a password request scenario

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Get a resource

Slide 94

Slide 94 text

Get a resource

Slide 95

Slide 95 text

Create a resource

Slide 96

Slide 96 text

Create a resource

Slide 97

Slide 97 text

Source: https://symfony.com/doc/current/components/serializer.html

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

Setting RabbitMQ as transporter

Slide 101

Slide 101 text

Setting reset routes accessible anonymously

Slide 102

Slide 102 text

Creating the resource

Slide 103

Slide 103 text

Set messenger

Slide 104

Slide 104 text

Define the reset request operation, input and output

Slide 105

Slide 105 text

Define the reset operation

Slide 106

Slide 106 text

Define the input class

Slide 107

Slide 107 text

I can now reset my password

Slide 108

Slide 108 text

Create a resource Output is set to false, nothing to do We need to create the transformers

Slide 109

Slide 109 text

Transform the post input into a ressource

Slide 110

Slide 110 text

Maybe using the incoming AutoMapper there?

Slide 111

Slide 111 text

Encode the new password on PUT request

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Create the Handler

Slide 114

Slide 114 text

Handle the token creation

Slide 115

Slide 115 text

Handle the password update and remove the token

Slide 116

Slide 116 text

Add a custom constraint to check the expiration

Slide 117

Slide 117 text

You’ve got a fully decoupled reset password procedure !

Slide 118

Slide 118 text

Spice all this with your favorites Testing frameworks

Slide 119

Slide 119 text

Plug-in your favorite CI / CD system

Slide 120

Slide 120 text

And you’ve got a robust API For middle to large applications

Slide 121

Slide 121 text

THANK YOU ! I am so thirsty right now… But if you’ve got any question I can make an effort :)