From f3802e36b32d3a2758ee834753a8dcc7ddce1fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 18 May 2021 19:58:29 +0200 Subject: [PATCH] documentation for api --- docs/source/development/api.rst | 286 +++++++++++++++++++++----------- 1 file changed, 189 insertions(+), 97 deletions(-) diff --git a/docs/source/development/api.rst b/docs/source/development/api.rst index feb4fd86a..ea807e090 100644 --- a/docs/source/development/api.rst +++ b/docs/source/development/api.rst @@ -13,6 +13,9 @@ API Chill provides a basic framework to build REST api. +Basic configuration +******************* + Configure a route ================= @@ -34,7 +37,7 @@ You can also: * `How to create your custom normalizer `_ Auto-loading the routes -*********************** +======================= Ensure that those lines are present in your file `app/config/routing.yml`: @@ -47,14 +50,9 @@ Ensure that those lines are present in your file `app/config/routing.yml`: Create your model -***************** +================= -Create your model on the usual way. - -If you do not use a `custom normalizer `_, you must add `annotations to create serialization groups `_. Those groups are, by defaults: - -* :code:`read` for GET requests; -* :code:`write` for POST, PUT and PATCH requests +Create your model on the usual way: .. code-block:: php @@ -62,7 +60,6 @@ If you do not use a `custom normalizer scopes[] = $scope; + + return $this; + } + + public function removeScope(Scope $scope): void + { + $this->scopes->removeElement($scope); + } + + +Create your route into the configuration: + +.. code-block:: yaml + + chill_main: + apis: + - + class: Chill\PersonBundle\Entity\AccompanyingPeriod + name: accompanying_course + base_path: /api/1.0/person/accompanying-course + controller: Chill\PersonBundle\Controller\AccompanyingCourseApiController + actions: + scope: + methods: + POST: true + DELETE: true + GET: false + HEAD: false + PUT: false + PATCH: false + roles: + POST: CHILL_PERSON_ACCOMPANYING_PERIOD_SEE + DELETE: CHILL_PERSON_ACCOMPANYING_PERIOD_SEE + GET: null + HEAD: null + PUT: null + PATCH: null + controller_action: null + path: null + single-collection: single + +This will create a new route, which will accept two methods: DELETE and POST: + +.. code-block:: raw + + +--------------+---------------------------------------------------------------------------------------+ + | Property | Value | + +--------------+---------------------------------------------------------------------------------------+ + | Route Name | chill_api_single_accompanying_course_scope | + | Path | /api/1.0/person/accompanying-course/{id}/scope.{_format} | + | Path Regex | {^/api/1\.0/person/accompanying\-course/(?P[^/]++)/scope\.(?P<_format>[^/]++)$}sD | + | Host | ANY | + | Host Regex | | + | Scheme | ANY | + | Method | POST|DELETE | + | Requirements | {id}: \d+ | + | Class | Symfony\Component\Routing\Route | + | Defaults | _controller: csapi_accompanying_course_controller:scopeApi | + | Options | compiler_class: Symfony\Component\Routing\RouteCompiler | + +--------------+---------------------------------------------------------------------------------------+ + + + +Then, create the controller action. Call the method: + +.. code-block:: php + + namespace Chill\PersonBundle\Controller; + + use Chill\MainBundle\CRUD\Controller\ApiController; + use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\HttpFoundation\Response; + use Chill\MainBundle\Entity\Scope; + + class MyController extends ApiController + { + public function scopeApi($id, Request $request, string $_format): Response + { + return $this->addRemoveSomething('scope', $id, $request, $_format, 'scope', Scope::class, [ 'groups' => [ 'read' ] ]); + } + } + +This will allow to add a scope by his id, and delete them. + +Curl requests: + +.. code-block:: bash + + # add a scope with id 5 + curl -X 'POST' \ + 'http://localhost:8001/api/1.0/person/accompanying-course/2868/scope.json' \ + -H 'accept: */*' \ + -H 'Content-Type: application/json' \ + -d '{ + "type": "scope", + "id": 5 + }' + + # remove a scope with id 5 + curl -X 'DELETE' \ + 'http://localhost:8001/api/1.0/person/accompanying-course/2868/scope.json' \ + -H 'accept: */*' \ + -H 'Content-Type: application/json' \ + -d '{ + "id": 5, + "type": "scope" + }' + + Serialization for collection -============================ +**************************** A specific model has been defined for returning collection: @@ -432,8 +558,9 @@ A specific model has been defined for returning collection: } } +Where this is relevant, this model should be re-used in custom controller actions. -This can be achieved quickly by assembling results into a :code:`Chill\MainBundle\Serializer\Model\Collection`. The pagination information is given by using :code:`Paginator` (see :ref:`Pagination `). +In custom actions, this can be achieved quickly by assembling results into a :code:`Chill\MainBundle\Serializer\Model\Collection`. The pagination information is given by using :code:`Paginator` (see :ref:`Pagination `). .. code-block:: php @@ -454,7 +581,7 @@ This can be achieved quickly by assembling results into a :code:`Chill\MainBundl .. _api_full_configuration: Full configuration example -========================== +************************** .. code-block:: yaml @@ -501,39 +628,4 @@ Full configuration example single-collection: single base_role: null -Maintaining an OpenApi -====================== - -.. note:: - - This is experimental and may change. Keep reading this part. - -Accessing swagger ------------------ - -The swagger UI is accessible through ``_ - -This is possible only in development mode. - -You must be authenticated with a valid user to access it. - -Maintaining specs ------------------ - -Each bundle should have an `open api 3.0 spec file `_ at the bundle's root, and name :code:`chill.api.specs.yaml`. - -.. warning:: - - Update the command :code:`specs-build` into `package.json` when adding a new api file. - -The specs may be compiled from the different bundles filed, and validated using docker node: - -.. code-block:: bash - - ./docker-node.sh - # build the openapi - yarn specs-build - # validate - yarn specs-validate -