Browse Source

document output stream

Bastien Sevajol 5 years ago
parent
commit
54c691a439
3 changed files with 58 additions and 0 deletions
  1. 11 0
      hapic/buffer.py
  2. 17 0
      hapic/doc.py
  3. 30 0
      tests/ext/unit/test_aiohttp.py

+ 11 - 0
hapic/buffer.py View File

@@ -9,6 +9,7 @@ from hapic.description import InputHeadersDescription
9 9
 from hapic.description import InputFormsDescription
10 10
 from hapic.description import InputFilesDescription
11 11
 from hapic.description import OutputBodyDescription
12
+from hapic.description import OutputStreamDescription
12 13
 from hapic.description import OutputFileDescription
13 14
 from hapic.description import OutputHeadersDescription
14 15
 from hapic.description import ErrorDescription
@@ -96,6 +97,16 @@ class DecorationBuffer(object):
96 97
         self._description.output_body = description
97 98
 
98 99
     @property
100
+    def output_stream(self) -> OutputStreamDescription:
101
+        return self._description.output_stream
102
+
103
+    @output_stream.setter
104
+    def output_stream(self, description: OutputStreamDescription) -> None:
105
+        if self._description.output_stream is not None:
106
+            raise AlreadyDecoratedException()
107
+        self._description.output_stream = description
108
+
109
+    @property
99 110
     def output_file(self) -> OutputFileDescription:
100 111
         return self._description.output_file
101 112
 

+ 17 - 0
hapic/doc.py View File

@@ -158,6 +158,23 @@ def bottle_generate_operations(
158 158
                 )
159 159
             }
160 160
 
161
+    if description.output_stream:
162
+        # TODO BS 2018-07-31: Is that a correct way to re
163
+        # instanciate with .__class__ ... ?
164
+        method_operations.setdefault('responses', {})\
165
+            [int(description.output_stream.wrapper.default_http_code)] = {
166
+                'description': str(int(description.output_stream.wrapper.default_http_code)),  # nopep8
167
+                'schema': generate_schema_ref(
168
+                    spec,
169
+                    description
170
+                        .output_stream
171
+                        .wrapper
172
+                        .processor
173
+                        .schema
174
+                        .__class__(many=True),
175
+                )
176
+            }
177
+
161 178
     if description.output_file:
162 179
         method_operations.setdefault('produces', []).extend(
163 180
             description.output_file.wrapper.output_types

+ 30 - 0
tests/ext/unit/test_aiohttp.py View File

@@ -394,3 +394,33 @@ class TestAiohttpExt(object):
394 394
                        'description': '200',
395 395
                    }
396 396
                } == doc['paths']['/{username}']['get']['responses']
397
+
398
+    def test_unit__generate_output_stream_doc__ok__nominal_case(
399
+        self,
400
+        aiohttp_client,
401
+        loop,
402
+    ):
403
+        hapic = Hapic(async_=True)
404
+
405
+        class OuputStreamItemSchema(marshmallow.Schema):
406
+            name = marshmallow.fields.String(required=True)
407
+
408
+        @hapic.with_api_doc()
409
+        @hapic.output_stream(OuputStreamItemSchema())
410
+        async def get_users(request, hapic_data):
411
+            pass
412
+
413
+        app = web.Application(debug=True)
414
+        app.router.add_get('/', get_users)
415
+        hapic.set_context(AiohttpContext(app))
416
+
417
+        doc = hapic.generate_doc('aiohttp', 'testing')
418
+        assert '/' in doc.get('paths')
419
+        assert 'get' in doc['paths']['/']
420
+        assert 200 in doc['paths']['/']['get'].get('responses', {})
421
+        assert {
422
+            'items': {
423
+                '$ref': '#/definitions/OuputStreamItemSchema'
424
+            },
425
+            'type': 'array',
426
+        } == doc['paths']['/']['get']['responses'][200]['schema']