From d4a34d223e623fa89241123263de87663d9a7aa4 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Mon, 6 Apr 2020 16:35:11 +0200
Subject: [PATCH 2/3] api_entreprise: factor endpoint parameters (#41315)

---
 passerelle/apps/api_entreprise/models.py | 171 ++++++++---------------
 1 file changed, 59 insertions(+), 112 deletions(-)

diff --git a/passerelle/apps/api_entreprise/models.py b/passerelle/apps/api_entreprise/models.py
index 81562552..01c8a55f 100644
--- a/passerelle/apps/api_entreprise/models.py
+++ b/passerelle/apps/api_entreprise/models.py
@@ -128,27 +128,46 @@ class APIEntreprise(BaseResource):
             'data': data,
         }
 
+    # description of common endpoint parameters
+    ASSOCIATION_PARAM = {
+        'description': _('association SIREN or RNA/WALDEC number'),
+        'example_value': '44317013900036'
+    }
+
+    CONTEXT_PARAM = {
+        'description': _('request context: MPS, APS...'),
+        'example_value': 'APS'
+    }
+
+    OBJECT_PARAM = {
+        'description': _('request object: form number, file identifier...'),
+        'example_value': '42'
+    }
+
+    RECIPIENT_PARAM = {
+        'description': _('request recipient: usually customer number'),
+        'example_value': '44317013900036'
+    }
+
+    SIREN_PARAM = {
+        'description': _('firm SIREN number'),
+        'example_value': '44317013900036',
+    }
+
+    SIRET_PARAM = {
+        'description': _('firms SIRET number'),
+        'example_value': '44317013900036'
+    }
+
     @endpoint(perm='can_access',
               pattern=r'(?P<association_id>\w+)/$',
               example_pattern='{association_id}/',
               description=_('Get association\'s documents'),
               parameters={
-                  'association_id': {
-                      'description': _('association SIREN or RNA/WALDEC number'),
-                      'example_value': '44317013900036',
-                  },
-                  'object': {
-                      'description': _('request object: form number, file identifier...'),
-                      'example_value': '42'
-                  },
-                  'context': {
-                      'description': _('request context: MPS, APS...'),
-                      'example_value': 'APS'
-                  },
-                  'recipient': {
-                      'description': _('request recipient: usually customer number'),
-                      'example_value': '44317013900036'
-                  }
+                  'association_id': ASSOCIATION_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM
               })
     def documents_associations(self, request, association_id, **kwargs):
         data = []
@@ -182,26 +201,14 @@ class APIEntreprise(BaseResource):
               example_pattern='{association_id}/{document_id}/',
               description=_('Get association\'s document'),
               parameters={
-                  'association_id': {
-                      'description': _('association SIREN or RNA/WALDEC number'),
-                      'example_value': '44317013900036',
-                  },
+                  'association_id': ASSOCIATION_PARAM,
                   'document_id': {
                       'description': _('document id'),
                       'example_value': 'A1500660325',
                   },
-                  'object': {
-                      'description': _('request object: form number, file identifier...'),
-                      'example_value': '42'
-                  },
-                  'context': {
-                      'description': _('request context: MPS, APS...'),
-                      'example_value': 'APS'
-                  },
-                  'recipient': {
-                      'description': _('request recipient: usually customer number'),
-                      'example_value': '44317013900036'
-                  }
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
               })
     def document(self, request, association_id, document_id, **kwargs):
         try:
@@ -218,26 +225,14 @@ class APIEntreprise(BaseResource):
               example_pattern='{association_id}/get-last/',
               description=_('Get association\'s last document of type'),
               parameters={
-                  'association_id': {
-                      'description': _('association SIREN or RNA/WALDEC number'),
-                      'example_value': '44317013900036',
-                  },
+                  'association_id': ASSOCIATION_PARAM,
                   'document_type': {
                       'description': _('document type'),
                       'example_value': 'Statuts',
                   },
-                  'object': {
-                      'Description': _('request object: form number, file identifier...'),
-                      'example_value': '42'
-                  },
-                  'context': {
-                      'description': _('request context: MPS, APS...'),
-                      'example_value': 'APS'
-                  },
-                  'recipient': {
-                      'description': _('request recipient: usually customer number'),
-                      'example_value': '44317013900036'
-                  }
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
               })
     def get_last_document_of_type(self, request, association_id, document_type, **kwargs):
         document = None
@@ -253,22 +248,10 @@ class APIEntreprise(BaseResource):
               example_pattern='{siren}/',
               description=_('Get firm\'s data from Infogreffe'),
               parameters={
-                  'siren': {
-                      'description': _('firm SIREN number'),
-                      'example_value': '44317013900036',
-                  },
-                  'object': {
-                      'description': _('request object: form number, file identifier...'),
-                      'example_value': '42'
-                  },
-                  'context': {
-                      'description': _('request context: MPS, APS...'),
-                      'example_value': 'APS'
-                  },
-                  'recipient': {
-                      'description': _('request recipient: usually customer number'),
-                      'example_value': '44317013900036'
-                  }
+                  'siren': SIREN_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
               })
     def extraits_rcs(self, request, siren, **kwargs):
         return self.get('extraits_rcs_infogreffe/%s/' % siren, **kwargs)
@@ -278,22 +261,10 @@ class APIEntreprise(BaseResource):
               example_pattern='{association_id}/',
               description=_('Get association\'s related informations'),
               parameters={
-                  'association_id': {
-                      'description': _('association SIRET or RNA/WALDEC number'),
-                      'example_value': '44317013900036',
-                  },
-                  'object': {
-                      'description': _('request object: form number, file identifier...'),
-                      'example_value': '42'
-                  },
-                  'context': {
-                      'description': _('request context: MPS, APS...'),
-                      'example_value': 'APS'
-                  },
-                  'recipient': {
-                      'description': _('request recipient: usually customer number'),
-                      'example_value': '44317013900036'
-                  }
+                  'association_id': ASSOCIATION_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
               })
     def associations(self, request, association_id, **kwargs):
         return self.get('associations/%s/' % association_id, **kwargs)
@@ -303,22 +274,10 @@ class APIEntreprise(BaseResource):
               example_pattern='{siren}/',
               description=_('Get firm\'s related informations'),
               parameters={
-                  'siren': {
-                      'description': _('firm SIREN number'),
-                      'example_value': '44317013900036',
-                  },
-                  'object': {
-                      'description': _('request object: form number, file identifier...'),
-                      'example_value': '42'
-                  },
-                  'context': {
-                      'description': _('request context: MPS, APS...'),
-                      'example_value': 'APS'
-                  },
-                  'recipient': {
-                      'description': _('request recipient: usually customer number'),
-                      'example_value': '44317013900036'
-                  }
+                  'siren': SIREN_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
               })
     def entreprises(self, request, siren, **kwargs):
         return self.get('entreprises/%s/' % siren, **kwargs)
@@ -329,22 +288,10 @@ class APIEntreprise(BaseResource):
               example_pattern='{siret}/',
               description_get=_('Get firms\'s related informations'),
               parameters={
-                  'siret': {
-                      'description': _('firms SIRET number'),
-                      'example_value': '44317013900036',
-                  },
-                  'object': {
-                      'description': _('request object: form number, file identifier...'),
-                      'example_value': '42'
-                  },
-                  'context': {
-                      'description': _('request context: MPS, APS...'),
-                      'example_value': 'APS'
-                  },
-                  'recipient': {
-                      'description': _('request recipient: usually customer number'),
-                      'example_value': '44317013900036'
-                  }
+                  'siret': SIRET_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
               })
     def etablissements(self, request, siret, **kwargs):
         return self.get('etablissements/%s/' % siret, **kwargs)
-- 
2.20.1

