Upgrade to Pro — share decks privately, control downloads, hide ads and more …

leçon n°139, API Platform ce n'est que pour du POC, FAUX !

leçon n°139, API Platform ce n'est que pour du POC, FAUX !

La première chose qui est mise en avant avec API Platform c'est la capacité de faire du développement rapide d'application. C'est vrai. Ça ne signifie pas pour autant qu'il est impossible de réaliser une application complexe avec une séparation de la couche métier, de l'api publique. Voyons ensemble les mécanismes avancés d'API Platform et les outils qui en feront votre framework API en PHP.

Grégoire Hébert

March 29, 2019
Tweet

More Decks by Grégoire Hébert

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

  3. @gheb_dev
    Self Managed since 2011
    36 people, + 1000% in 6 years
    [email protected]
    13 full courses & custom courses
    [email protected]

    View Slide

  4. @gheb_dev
    R.A.D.

    View Slide

  5. @gheb_dev

    View Slide

  6. @gheb_dev

    View Slide

  7. @gheb_dev

    View Slide

  8. @gheb_dev

    View Slide

  9. @gheb_dev

    View Slide

  10. @gheb_dev
    R.A.D.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. @gheb_dev
    Richardson’s Maturity Chart

    View Slide

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

    View Slide

  16. @gheb_dev
    Richardson’s Maturity Chart

    View Slide

  17. @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

    View Slide

  18. @gheb_dev
    Richardson’s Maturity Chart

    View Slide

  19. @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" }
    }

    View Slide

  20. @gheb_dev

    View Slide

  21. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015

    View Slide

  22. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website

    View Slide

  23. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website

    View Slide

  24. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website

    View Slide

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

    View Slide

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

    View Slide

  27. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  39. @gheb_dev
    First ApiResource

    View Slide

  40. @gheb_dev
    You’ve got the power

    View Slide

  41. @gheb_dev
    Some attributes

    View Slide

  42. @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… ?

    View Slide

  43. @gheb_dev
    Restricting the operations

    View Slide

  44. @gheb_dev
    Restricting the operations

    View Slide

  45. @gheb_dev
    Restricting the operations

    View Slide

  46. @gheb_dev
    Differentiating the expected income and the outcome

    View Slide

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

    View Slide

  48. @gheb_dev
    Differentiating the expected income and the outcome

    View Slide

  49. @gheb_dev
    ORM mapping

    View Slide

  50. @gheb_dev

    View Slide

  51. @gheb_dev
    R.A.D.

    View Slide

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

    View Slide

  53. @gheb_dev

    View Slide

  54. @gheb_dev
    Activate Mercure for a resource

    View Slide

  55. @gheb_dev

    View Slide

  56. @gheb_dev

    View Slide

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

    View Slide

  58. @gheb_dev
    Activate messenger for a resource

    View Slide

  59. @gheb_dev
    Defining a Handler

    View Slide

  60. @gheb_dev

    View Slide

  61. @gheb_dev
    Configuring Messenger component

    View Slide

  62. @gheb_dev

    View Slide

  63. @gheb_dev

    View Slide

  64. @gheb_dev

    View Slide

  65. @gheb_dev

    View Slide

  66. @gheb_dev

    View Slide

  67. @gheb_dev
    Going Further

    View Slide

  68. @gheb_dev

    View Slide

  69. @gheb_dev
    Configure the workflow

    View Slide

  70. @gheb_dev
    Defining states constants

    View Slide

  71. @gheb_dev
    Configuration using constants

    View Slide

  72. @gheb_dev
    Configuration using constants

    View Slide

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

    View Slide

  74. @gheb_dev
    Leverage the extensibility of 

    API Platform

    View Slide

  75. @gheb_dev
    Decorating the ResourceMetadataFactory

    View Slide

  76. @gheb_dev
    Decorating the ResourceMetadataFactory

    View Slide

  77. @gheb_dev
    Inject the supported resources

    View Slide

  78. @gheb_dev
    Decorating the OperationPathResolver

    View Slide

  79. @gheb_dev
    Change the state on post deserialize

    View Slide

  80. @gheb_dev
    Change the state on post deserialize

    View Slide

  81. @gheb_dev
    I get a new operation to update the state

    View Slide

  82. @gheb_dev
    Going Further (again)

    View Slide

  83. @gheb_dev
    Let’s consider we have Holder resource

    which is used as a User, with a JWT
    Authentication

    View Slide

  84. @gheb_dev
    Reverse side
    Linking a WaitingLine to a Holder

    View Slide

  85. @gheb_dev
    What if I want to set/remove ownership
    right after the change of state 

    with API Platform?

    View Slide

  86. @gheb_dev
    YOU DON’T.

    View Slide

  87. @gheb_dev
    Setting the ownership

    View Slide

  88. @gheb_dev
    Setting the ownership

    View Slide

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

    View Slide

  90. @gheb_dev
    A little demo maybe?

    View Slide

  91. @gheb_dev
    Going Further (again, again)

    promised, this is the last time

    View Slide

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

    View Slide

  93. @gheb_dev

    View Slide

  94. @gheb_dev

    View Slide

  95. @gheb_dev
    Get a resource

    View Slide

  96. @gheb_dev
    Get a resource

    View Slide

  97. @gheb_dev
    Create a resource

    View Slide

  98. @gheb_dev
    Create a resource

    View Slide

  99. @gheb_dev

    View Slide

  100. @gheb_dev
    Setting RabbitMQ as transporter

    View Slide

  101. @gheb_dev
    Creating the resource

    View Slide

  102. @gheb_dev
    Set messenger

    View Slide

  103. @gheb_dev
    Define the operations

    View Slide

  104. @gheb_dev
    Define the input and output

    View Slide

  105. @gheb_dev
    Define the input class

    View Slide

  106. @gheb_dev
    I can now reset my password

    View Slide

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

    View Slide

  108. @gheb_dev
    Transform the post input into a ressource

    View Slide

  109. @gheb_dev
    Maybe using the incoming AutoMapper there?

    View Slide

  110. @gheb_dev
    Encode the new password on PUT request

    View Slide

  111. @gheb_dev

    View Slide

  112. @gheb_dev
    Create the Handler

    View Slide

  113. @gheb_dev
    Handle the token creation

    View Slide

  114. @gheb_dev
    Handle the password update and remove the token

    View Slide

  115. @gheb_dev
    Add a custom constraint to check the expiration

    View Slide

  116. @gheb_dev

    View Slide

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

    View Slide

  118. @gheb_dev
    Spice all this with your favorites
    Testing frameworks

    View Slide

  119. @gheb_dev
    Plug-in your favorite CI / CD
    system

    View Slide

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

    View Slide

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

    View Slide