Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

@gheb_dev 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

@gheb_dev Self Managed since 2011 36 people, + 1000% in 6 years jobs@les-tilleuls.coop 13 full courses & custom courses masterclass@les-tilleuls.coop

Slide 4

Slide 4 text

@gheb_dev R.A.D.

Slide 5

Slide 5 text

@gheb_dev

Slide 6

Slide 6 text

@gheb_dev

Slide 7

Slide 7 text

@gheb_dev

Slide 8

Slide 8 text

@gheb_dev

Slide 9

Slide 9 text

@gheb_dev

Slide 10

Slide 10 text

@gheb_dev R.A.D.

Slide 11

Slide 11 text

@gheb_dev Let’s see why it’s worth it!

Slide 12

Slide 12 text

@gheb_dev Richardson’s Maturity Chart Graphql I see you…

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

@gheb_dev Richardson’s Maturity Chart

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

@gheb_dev Richardson’s Maturity Chart

Slide 17

Slide 17 text

@gheb_dev 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

@gheb_dev Richardson’s Maturity Chart

Slide 19

Slide 19 text

@gheb_dev 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

@gheb_dev

Slide 21

Slide 21 text

@gheb_dev V1 - DunglasJsonLdApiBundle Mars 2015

Slide 22

Slide 22 text

@gheb_dev V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website

Slide 23

Slide 23 text

@gheb_dev V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website

Slide 24

Slide 24 text

@gheb_dev V1 - DunglasJsonLdApiBundle Mars 2015 Juin 2015 Dedicated Website

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

@gheb_dev Septembre 2015 V2 Announced Mai 2016 V2 Alpha Available

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

@gheb_dev 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

@gheb_dev 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

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

Slide 39

Slide 39 text

@gheb_dev First ApiResource

Slide 40

Slide 40 text

@gheb_dev You’ve got the power

Slide 41

Slide 41 text

@gheb_dev Some attributes

Slide 42

Slide 42 text

@gheb_dev 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

@gheb_dev Restricting the operations

Slide 44

Slide 44 text

@gheb_dev Restricting the operations

Slide 45

Slide 45 text

@gheb_dev Restricting the operations

Slide 46

Slide 46 text

@gheb_dev Differentiating the expected income and the outcome

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

@gheb_dev Differentiating the expected income and the outcome

Slide 49

Slide 49 text

@gheb_dev ORM mapping

Slide 50

Slide 50 text

@gheb_dev

Slide 51

Slide 51 text

@gheb_dev R.A.D.

Slide 52

Slide 52 text

@gheb_dev Ok, but not everything is about CRUD or REST !

Slide 53

Slide 53 text

@gheb_dev

Slide 54

Slide 54 text

@gheb_dev Activate Mercure for a resource

Slide 55

Slide 55 text

@gheb_dev

Slide 56

Slide 56 text

@gheb_dev

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

@gheb_dev Activate messenger for a resource

Slide 59

Slide 59 text

@gheb_dev Defining a Handler

Slide 60

Slide 60 text

@gheb_dev

Slide 61

Slide 61 text

@gheb_dev Configuring Messenger component

Slide 62

Slide 62 text

@gheb_dev

Slide 63

Slide 63 text

@gheb_dev

Slide 64

Slide 64 text

@gheb_dev

Slide 65

Slide 65 text

@gheb_dev

Slide 66

Slide 66 text

@gheb_dev

Slide 67

Slide 67 text

@gheb_dev Going Further

Slide 68

Slide 68 text

@gheb_dev

Slide 69

Slide 69 text

@gheb_dev Configure the workflow

Slide 70

Slide 70 text

@gheb_dev Defining states constants

Slide 71

Slide 71 text

@gheb_dev Configuration using constants

Slide 72

Slide 72 text

@gheb_dev Configuration using constants

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

@gheb_dev Leverage the extensibility of 
 API Platform

Slide 75

Slide 75 text

@gheb_dev Decorating the ResourceMetadataFactory

Slide 76

Slide 76 text

@gheb_dev Decorating the ResourceMetadataFactory

Slide 77

Slide 77 text

@gheb_dev Inject the supported resources

Slide 78

Slide 78 text

@gheb_dev Decorating the OperationPathResolver

Slide 79

Slide 79 text

@gheb_dev Change the state on post deserialize

Slide 80

Slide 80 text

@gheb_dev Change the state on post deserialize

Slide 81

Slide 81 text

@gheb_dev I get a new operation to update the state

Slide 82

Slide 82 text

@gheb_dev Going Further (again)

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

@gheb_dev Reverse side Linking a WaitingLine to a Holder

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

@gheb_dev YOU DON’T.

Slide 87

Slide 87 text

@gheb_dev Setting the ownership

Slide 88

Slide 88 text

@gheb_dev Setting the ownership

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

@gheb_dev A little demo maybe?

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

@gheb_dev

Slide 94

Slide 94 text

@gheb_dev

Slide 95

Slide 95 text

@gheb_dev Get a resource

Slide 96

Slide 96 text

@gheb_dev Get a resource

Slide 97

Slide 97 text

@gheb_dev Create a resource

Slide 98

Slide 98 text

@gheb_dev Create a resource

Slide 99

Slide 99 text

@gheb_dev

Slide 100

Slide 100 text

@gheb_dev Setting RabbitMQ as transporter

Slide 101

Slide 101 text

@gheb_dev Creating the resource

Slide 102

Slide 102 text

@gheb_dev Set messenger

Slide 103

Slide 103 text

@gheb_dev Define the operations

Slide 104

Slide 104 text

@gheb_dev Define the input and output

Slide 105

Slide 105 text

@gheb_dev Define the input class

Slide 106

Slide 106 text

@gheb_dev I can now reset my password

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

@gheb_dev Transform the post input into a ressource

Slide 109

Slide 109 text

@gheb_dev Maybe using the incoming AutoMapper there?

Slide 110

Slide 110 text

@gheb_dev Encode the new password on PUT request

Slide 111

Slide 111 text

@gheb_dev

Slide 112

Slide 112 text

@gheb_dev Create the Handler

Slide 113

Slide 113 text

@gheb_dev Handle the token creation

Slide 114

Slide 114 text

@gheb_dev Handle the password update and remove the token

Slide 115

Slide 115 text

@gheb_dev Add a custom constraint to check the expiration

Slide 116

Slide 116 text

@gheb_dev

Slide 117

Slide 117 text

@gheb_dev You’ve got a fully decoupled reset password procedure !

Slide 118

Slide 118 text

@gheb_dev Spice all this with your favorites Testing frameworks

Slide 119

Slide 119 text

@gheb_dev Plug-in your favorite CI / CD system

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

@gheb_dev THANK YOU ! I am so thirsty right now… But if you’ve got any question :)