From 673789ed77f74564dc94112cf5ab114ffcded931 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 5 Nov 2020 10:05:58 +0100 Subject: [PATCH] utils: add helper argument post_json_schema (#48308) --- passerelle/contrib/rsa13/models.py | 254 ++++++++++++----------------- passerelle/utils/api.py | 7 + 2 files changed, 110 insertions(+), 151 deletions(-) diff --git a/passerelle/contrib/rsa13/models.py b/passerelle/contrib/rsa13/models.py index c8cafcf5..5dd3ea20 100644 --- a/passerelle/contrib/rsa13/models.py +++ b/passerelle/contrib/rsa13/models.py @@ -297,35 +297,29 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=3, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'required': ['nom', 'prenom'], - 'properties': { - 'email': { - 'type': 'string', - 'maxLength': 78, - 'pattern': '^(.*@.*)?$', - }, - 'nom': { - 'type': 'string', - 'maxLength': 28, - }, - 'prenom': { - 'type': 'string', - 'maxLength': 32, - }, - 'tel': { - 'type': 'string', - 'maxLength': 10, - 'pattern': '^[0-9]{0,10}$', - }, - }, - }, + post_json_schema={ + 'type': 'object', + 'required': ['nom', 'prenom'], + 'properties': { + 'email': { + 'type': 'string', + 'maxLength': 78, + 'pattern': '^(.*@.*)?$', }, - } + 'nom': { + 'type': 'string', + 'maxLength': 28, + }, + 'prenom': { + 'type': 'string', + 'maxLength': 32, + }, + 'tel': { + 'type': 'string', + 'maxLength': 10, + 'pattern': '^[0-9]{0,10}$', + }, + }, }, json_schema_response=response_schema( { @@ -406,36 +400,30 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=3.5, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'required': ['nom', 'prenom', 'statut'], - 'properties': { - 'email': { - 'type': 'string', - 'maxLength': 78, - 'pattern': '^(.*@.*)?$', - }, - 'nom': { - 'type': 'string', - 'maxLength': 28, - }, - 'prenom': { - 'type': 'string', - 'maxLength': 32, - }, - 'tel': { - 'type': 'string', - 'maxLength': 10, - 'pattern': '^[0-9]{0,10}$', - }, - 'statut': {'enum': ['C', 'A']}, - }, - }, + post_json_schema={ + 'type': 'object', + 'required': ['nom', 'prenom', 'statut'], + 'properties': { + 'email': { + 'type': 'string', + 'maxLength': 78, + 'pattern': '^(.*@.*)?$', }, - } + 'nom': { + 'type': 'string', + 'maxLength': 28, + }, + 'prenom': { + 'type': 'string', + 'maxLength': 32, + }, + 'tel': { + 'type': 'string', + 'maxLength': 10, + 'pattern': '^[0-9]{0,10}$', + }, + 'statut': {'enum': ['C', 'A']}, + }, }, json_schema_response=response_schema(), ) @@ -789,25 +777,19 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=5.1, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'required': ['tel'], - 'properties': { - 'tel': { - 'type': 'string', - 'maxLength': 10, - 'pattern': '^[0-9]{0,10}$', - }, - 'commentaire': { - 'type': 'string', - }, - }, - }, + post_json_schema={ + 'type': 'object', + 'required': ['tel'], + 'properties': { + 'tel': { + 'type': 'string', + 'maxLength': 10, + 'pattern': '^[0-9]{0,10}$', }, - } + 'commentaire': { + 'type': 'string', + }, + }, }, json_schema_response=response_schema(), ) @@ -848,20 +830,14 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=5.2, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'required': ['commentaire'], - 'properties': { - 'commentaire': { - 'type': 'string', - }, - }, - }, + post_json_schema={ + 'type': 'object', + 'required': ['commentaire'], + 'properties': { + 'commentaire': { + 'type': 'string', }, - } + }, }, json_schema_response=response_schema(), ) @@ -906,7 +882,7 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=5.1, - post={ + post_json_schema={ 'request_body': { 'schema': { 'application/json': { @@ -962,20 +938,14 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=5.2, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'required': ['commentaire'], - 'properties': { - 'commentaire': { - 'type': 'string', - }, - }, - }, + post_json_schema={ + 'type': 'object', + 'required': ['commentaire'], + 'properties': { + 'commentaire': { + 'type': 'string', }, - } + }, }, json_schema_response=response_schema(), ) @@ -1266,20 +1236,14 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=10, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'properties': { - 'date_debut': DATE_SCHEMA, - 'date_fin': DATE_SCHEMA, - 'moticlodac': {'type': 'string'}, - 'commentaire_ref': {'type': 'string', 'maxLength': 1000}, - }, - }, - }, - } + post_json_schema={ + 'type': 'object', + 'properties': { + 'date_debut': DATE_SCHEMA, + 'date_fin': DATE_SCHEMA, + 'moticlodac': {'type': 'string'}, + 'commentaire_ref': {'type': 'string', 'maxLength': 1000}, + }, }, json_schema_response=response_schema( { @@ -1575,18 +1539,12 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=13.5, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'properties': { - 'motif': {'type': 'string'}, - 'commentaire_ref': {'type': 'string'}, - }, - }, - }, - } + post_json_schema={ + 'type': 'object', + 'properties': { + 'motif': {'type': 'string'}, + 'commentaire_ref': {'type': 'string'}, + }, }, json_schema_response=response_schema(), ) @@ -1628,33 +1586,27 @@ class RSA13Resource(BaseResource, HTTPResource): ), display_category=_('Platform'), display_order=14, - post={ - 'request_body': { - 'schema': { - 'application/json': { - 'type': 'object', - 'properties': { - 'rendezvous': { - 'type': 'object', - 'properties': { - 'date_prise': DATE_SCHEMA, - 'relance': { - 'type': 'object', - 'properties': { - 'date': DATE_SCHEMA, - 'motif': {'type': 'string'}, - }, - }, - 'date_reelle': DATE_SCHEMA, - 'resultat': {'type': 'string'}, - }, + post_json_schema={ + 'type': 'object', + 'properties': { + 'rendezvous': { + 'type': 'object', + 'properties': { + 'date_prise': DATE_SCHEMA, + 'relance': { + 'type': 'object', + 'properties': { + 'date': DATE_SCHEMA, + 'motif': {'type': 'string'}, }, - 'commentaire_ref': {'type': 'string'}, }, - 'unflatten': True, + 'date_reelle': DATE_SCHEMA, + 'resultat': {'type': 'string'}, }, }, - } + 'commentaire_ref': {'type': 'string'}, + }, + 'unflatten': True, }, json_schema_response=response_schema( { diff --git a/passerelle/utils/api.py b/passerelle/utils/api.py index 71a3c7ef..167c706e 100644 --- a/passerelle/utils/api.py +++ b/passerelle/utils/api.py @@ -59,6 +59,7 @@ class endpoint(object): display_category='', json_schema_response=None, datasource=False, + post_json_schema=None, ): self.perm = perm self.methods = methods @@ -83,6 +84,12 @@ class endpoint(object): self.example_pattern = example_pattern self.parameters = parameters or {} self.cache_duration = cache_duration + if post_json_schema: + # helper to define the POST json schema + post = post or {} + schema = post.setdefault('request_body', {}).setdefault('schema', {}) + assert not schema.get('application/json'), 'a json schema was already set in the post argument' + schema['application/json'] = post_json_schema self.post = post if post: self.methods = ['post'] -- 2.31.1