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

REST dans le monde Symfony (Bdx.io)

REST dans le monde Symfony (Bdx.io)

Après vous avoir convaincu que vous n'avez probablement jamais créé d'APIs "REST" de votre vie, je vous propose un tour d'horizon des outils disponibles pour concevoir de vraies APIs REST en PHP (et oui !) grâce à l'écosystème du framework Symfony.
Cette présentation, mêlant théorie et pragmatisme, fera l'état des lieux de "REST" dans le monde Symfony en passant différentes couches en revue telles le routing, la sérialisation, la sécurité mais également la documentation de vos APIs.

Online slides: http://friendsofsymfony.github.io/slides/rest-dans-le-monde-symfony.html
Sources: https://github.com/FriendsOfSymfony/friendsofsymfony.github.com

William Durand

October 17, 2014
Tweet

More Decks by William Durand

Other Decks in Programming

Transcript

  1. REpresentational State Transfer REST is the underlying architectural principle of

    the web. It is formalized as a set of constraints, described in Roy Fielding's dissertation.
  2. Level 0 - The Swamp of POX HTTP as a

    tunneling mechanism RPC style system (SOAP, XML-RPC)
  3. HTTP Verbs Method Safe? Idempotent? GET yes yes HEAD yes

    yes POST no no PUT no yes DELETE no yes (*) ... no no Safe: means cacheable. Idempotent: result independent on the number of executions.
  4. HTTP Status Codes Code range Description Example 1xx Information 100

    - Continue 2xx Successful 201 - Created 3xx Redirection 301 - Moved Permanently 4xx Client Error 404 - Not Found 5xx Server Error 501 - Not Implemented
  5. Level 3 - Hypermedia Controls Service discovery via link relations

    Hypermedia formats (ATOM, HAL, JSON-LD, etc.)
  6. Content Type Negotiation Content Type Negotiation is the principle of

    finding appropriate response formats based on client requirements. No standardized algorithm available Apache algorithm is documented though Also covers encoding (Accept-Encoding) and language (Accept-Language) negotiation mod_negotiation
  7. Example Accept: application/json, application/xml;q=0.9, text/html;q=0.8, text/*;q=0.7, */*;q=0.5 Priority Description q=1.0

    application/json q=0.9 application/xml q=0.8 text/html q=0.7 text/* (ie. any text) q=0.5 */* (ie. any media type)
  8. HATEOAS Hypermedia As The Engine Of Application State. It means

    that hypermedia should be used to find your way through the API. It is all about state transitions. Your application is just a big state machine. <?xml version="1.0" encoding="UTF-8"?> <collection page="1" limit="10" pages="1"> <user id="123"></user> <user id="456"></user> <link rel="self" href="/api/users?page=1&amp;limit=10" /> <link rel="first" href="/api/users?page=1&amp;limit=10" /> <link rel="last" href="/api/users?page=1&amp;limit=10" /> </collection>
  9. In order to comply with RMM Level 1, I want

    to turn business objects/data to resources.
  10. In A Nutshell Enables (de) serialization of object graphs Implements

    visitor pattern to enable flexibility Fully leverage native JSON and XML Custom exclusion strategies to determine what to serialize Quite easy to extend
  11. Usage use JMS\SerializerBundle\Annotation as Serializer; /** @Serializer\XmlRoot("response") */ class MyResponse

    { /** * @Serializer\XmlList(inline=true, entry="article") */ protected $articles; /** * @Serializer\XmlAttribute() */ protected $page; public function __construct(Collection $articles, $page) { $this->articles = $articles; $this->page = $page; } }
  12. In order to comply with RMM Level 2, I want

    to use proper HTTP verbs and status codes .
  13. But I need to understand what the client wants and

    says. And I also need to rely on a serializer. And I will have to deal with filters, versioning, etc. I need a framework!
  14. In A Nutshell Toolbox of services and listeners to build

    RESTful APIs Generate HTML, XML, and JSON from a single action Automatic generation of routes from actions GET parameter parsing and validation Integration with Symfony serializer and JMS Serializer Accept header negotiation (thx to the lib) Request body decoding API versioning Negotiation
  15. Usage class RestController { /** * route name: liip_hello_rest_get_articles *

    pattern: /liip/hello/rest/articles.{_format} * http method requirement: GET * * @View() * @QueryParam(name="page", requirements="\d+", default="1") */ public function getArticlesAction($page) { //$page = $request->query->get('page'); //$page = preg_match('\d+', $page) ? $page : 1; $articles = [ 'bim', 'bam', 'bingo' ]; return new \Acme\MyBundle\MyResponse($articles, $page); } }
  16. Content Type Negotiation fos_rest: format_listener: rules: - path: ^/ priorities:

    [ html, json, xml ] fallback_format: ~ prefer_extension: true Accept: application/json, application/xml;q=0.9, text/html;q=0.8, text/*;q=0.7, */*;q=0.5 ­ willdurand/Negotiation
  17. Version Listener fos_rest: view: mime_types: json: - 'application/json' - 'application/json;version=1.0'

    - 'application/json;version=1.1' format_listener: media_type: version_regex: '/(v|version)=(?P<version>[0-9\.]+)/' Accept: application/json;version=1.1 class User { /** @Until("1.0") */ private $name; /** @Since("1.1") */ private $lastname; }
  18. Message Decoding Decode request body from XML, JSON, etc. into

    the request Integration with converters to turn parameters to objects GET parameter validation and normalization POST /users HTTP/1.1 Accept: application/json,*/*;q=0.8 Content-Type: application/json {"name":"Don Johnson"} public functon postAction(Request $request) { $name = $request->request->get('name'); }
  19. Allowed Methods Listener Examines all available routes to determine what

    HTTP methods are available for the request URI Automatically sets an according Allow header in the response fos_rest: allowed_methods_listener: true HTTP/1.1 200 OK Allow: GET, POST
  20. MIME Type Listener Register MIME types that Symfony2's Request class

    does not support by default fos_rest: view: mime_types: json: ['application/json', 'application/vnd.example-com.foo+json'] rss: 'application/rss+xml' jpg: 'image/jpeg' png: 'image/png' jsonp_handler: ~ p JSONP callback validation thanks to the library willdurand/JsonpCallbackValidator
  21. Error/Exception Handling Return correct HTTP status code Determine for which

    exception to expose the exception message Automatically extract errors from a Form instance fos_rest: exception: codes: 'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404 'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT messages: 'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
  22. Usage $invalidArgumentExceptionMapping = new ExceptionMapping(array( 'exceptionClassName' => '\InvalidArgumentException', 'factory' =>

    'default', 'httpStatusCode' => 400, 'errorCode' => 400101, 'errorMessage' => null, 'errorExtendedMessage' => 'Extended message', 'errorMoreInfoUrl' => 'http://api.my.tld/doc/error/400101', )); $exceptionMap = new ExceptionMap(); $exceptionMap->add($invalidArgumentExceptionMapping); $errorResolver = new ErrorResolver($exceptionMap); $error = $errorResolver->resolve( new \InvalidArgumentException('This is an invalid argument exception.') );
  23. Symfony Bundle Configuration tbbc_rest_util: error: exception_mapping: InvalidArgumentException: class: "InvalidArgumentException" factory:

    default http_status_code: 400 error_code: 400101 error_message: ~ extended_message: "Extended message" more_info_url: "http://api.my.tld/doc/error/400101"
  24. Example print_r($error->toArray()); Array ( [http_status_code] => 400 [code] => 400101

    [message] => This is an invalid argument exception. [extended_message] => Extended message [more_info_url] => http://api.my.tld/doc/error/400101 ) echo json_encode($error->toArray()); { "http_status_code": 400, "code": 400101, "message": "This is an invalid argument exception.", "extended_message": "Extended message", "more_info_url": "http:\/\/api.my.tld\/doc\/error\/400101" }
  25. In order to comply with RMM Level 3, I want

    to HATEOAS ALL THE THINGS!
  26. In A Nutshell Leverages the JMS Serializer library Relies on

    the Symfony2 ExpressionLanguage component Supports JSON and XML Allows to configure links and embedded resources in XML, YAML, PHP, or Annotations Dynamic relations (relation providers) Exclusion strategies
  27. Usage use JMS\Serializer\Annotation as Serializer; use Hateoas\Configuration\Annotation as Hateoas; /**

    * @Serializer\XmlRoot("user") * * @Hateoas\Relation("self", href = "expr('/api/users/' ~ object.getId())") */ class User { /** @Serializer\XmlAttribute */ private $id; private $firstName; private $lastName; public function getId() {} }
  28. JSON $hateoas = HateoasBuilder::create()->build(); $json = $hateoas->serialize(new User(123, 'John', 'Doe'),

    'json'); { "id": 123, "first_name": "John", "last_name": "Doe", "_links": { "self": { "href": "/api/users/123" } } }
  29. XML $hateoas = HateoasBuilder::create()->build(); $xml = $hateoas->serialize(new User(123, 'John', 'Doe'),

    'xml'); <user id="123"> <first_name> </first_name> <last_name> </last_name> <link rel="self" href="/api/users/123"/> </user> <![CDATA[John]]> <![CDATA[Doe]]>
  30. RFC 6570: URI Template A compact sequence of characters for

    describing a range of URIs through variable expansion. URIs URI Template http://example.com/~fred/ http://example.com/~ {username}/ http://example.com/~mark/
  31. Usage demo_route: pattern: /demo/{page} $templateLink = (new Rfc6570Generator($routes)) ->generate('demo_route', array(

    'page' => '{page}', 'sort' => '{sort}', 'filter' => array('{filter}'), )); /demo/{page}?{&sort}{&filter%5B%5D*} p Leverages the . Symfony Routing component
  32. In A Nutshell Designed for Symfony Generates documentation for your

    REST APIs Gathers information from PHPDoc Supports FOSRestBundle, SensioFrameworkExtraBundle, JMSSerializerBundle and JMSSecurityExtraBundle Supports your own annotations and you own parsers Sandbox (Killer Feature 쁥) Swagger compliant
  33. Usage /** * List all notes. * * @ApiDoc( *

    resource = true, * statusCodes = { 200 = "Returned when successful" } * ) * @QueryParam( * name="offset", requirements="\d+", nullable=true, * description="Offset from which to start listing notes." * ) * @QueryParam( * name="limit", requirements="\d+", default="5", * description="How many notes to return." * ) */ public function getNotesAction() {}
  34. In order to not burn too many servers, I want

    to add and manage a caching layer.
  35. In A Nutshell Set path-based cache expiration headers via app

    config Set up an invalidation scheme without writing PHP code Tag your responses and invalidate cache based on tags Send invalidation requests with minimal performance impact ( Varnish and Nginx supported out of the box) Differentiate caches based on user type (e.g. roles) Easily implement HTTP cache client
  36. What Is OAuth? OAuth is an open protocol to allow

    secure authorization in a simple and standard method from web, mobile and desktop applications. It is an authorization framework that enables a third-party application to obtain limited access to an HTTP service. http://edu.williamdurand.fr/security-slides/#slide85
  37. FOSOAuthServerBundle Server-side implementation of OAuth2 Supports Doctrine ORM|ODM, Propel Highly

    configurable Thank you for helping us! Alan Gabriel Bem ­ ­ (OAuth1.0a) FriendsOfSymfony/FOSOAuthServerBundle willdurand/BazingaOAuthServerBundle