瀏覽代碼

Move BottleContext in ext module, Closes #3

Bastien Sevajol 7 年之前
父節點
當前提交
3a48b3d34a
共有 5 個文件被更改,包括 62 次插入75 次删除
  1. 1 27
      hapic/__init__.py
  2. 0 48
      hapic/context.py
  3. 2 0
      hapic/ext/__init__.py
  4. 2 0
      hapic/ext/bottle/__init__.py
  5. 57 0
      hapic/ext/bottle/context.py

+ 1 - 27
hapic/__init__.py 查看文件

@@ -1,7 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2
-from hapic.context import BottleContext
3 2
 from hapic.hapic import Hapic
4 3
 from hapic.data import HapicData
4
+from hapic import ext
5 5
 
6 6
 _hapic_default = Hapic()
7 7
 
@@ -13,32 +13,6 @@ input_query = _hapic_default.input_query
13 13
 input_forms = _hapic_default.input_forms
14 14
 output_headers = _hapic_default.output_headers
15 15
 output_body = _hapic_default.output_body
16
-# with_api_doc_bis = _hapic_default.with_api_doc_bis
17 16
 generate_doc = _hapic_default.generate_doc
18 17
 set_context = _hapic_default.set_context
19 18
 handle_exception = _hapic_default.handle_exception
20
-
21
-# from hapic.hapic import with_api_doc
22
-# from hapic.hapic import with_api_doc_bis
23
-# from hapic.hapic import generate_doc
24
-# from hapic.hapic import output_body
25
-# from hapic.hapic import input_body
26
-# from hapic.hapic import input_query
27
-# from hapic.hapic import input_path
28
-# from hapic.hapic import input_headers
29
-# from hapic.context import BottleContext
30
-# from hapic.hapic import set_fake_default_context
31
-# from hapic.hapic import error_schema
32
-
33
-
34
-class FakeSetContext(object):
35
-    @property
36
-    def bottle_context(self):
37
-        return BottleContext()
38
-
39
-
40
-class FakeExt(object):
41
-    bottle = FakeSetContext()
42
-
43
-
44
-ext = FakeExt()

+ 0 - 48
hapic/context.py 查看文件

@@ -27,51 +27,3 @@ class ContextInterface(object):
27 27
         http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
28 28
     ) -> typing.Any:
29 29
         raise NotImplementedError()
30
-
31
-
32
-# TODO: In extension, see #3
33
-class BottleContext(ContextInterface):
34
-    def get_request_parameters(self, *args, **kwargs) -> RequestParameters:
35
-        return RequestParameters(
36
-            path_parameters=bottle.request.url_args,
37
-            query_parameters=bottle.request.params,
38
-            body_parameters=bottle.request.json,
39
-            form_parameters=bottle.request.forms,
40
-            header_parameters=bottle.request.headers,
41
-        )
42
-
43
-    def get_response(
44
-        self,
45
-        response: dict,
46
-        http_code: int,
47
-    ) -> bottle.HTTPResponse:
48
-        return bottle.HTTPResponse(
49
-            body=json.dumps(response),
50
-            headers=[
51
-                ('Content-Type', 'application/json'),
52
-            ],
53
-            status=http_code,
54
-        )
55
-
56
-    def get_validation_error_response(
57
-        self,
58
-        error: ProcessValidationError,
59
-        http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
60
-    ) -> typing.Any:
61
-        # TODO BS 20171010: Manage error schemas, see #4
62
-        from hapic.hapic import _default_global_error_schema
63
-        unmarshall = _default_global_error_schema.dump(error)
64
-        if unmarshall.errors:
65
-            raise OutputValidationException(
66
-                'Validation error during dump of error response: {}'.format(
67
-                    str(unmarshall.errors)
68
-                )
69
-            )
70
-
71
-        return bottle.HTTPResponse(
72
-            body=json.dumps(unmarshall.data),
73
-            headers=[
74
-                ('Content-Type', 'application/json'),
75
-            ],
76
-            status=int(http_code),
77
-        )

+ 2 - 0
hapic/ext/__init__.py 查看文件

@@ -0,0 +1,2 @@
1
+# -*- coding: utf-8 -*-
2
+from hapic.ext import bottle

+ 2 - 0
hapic/ext/bottle/__init__.py 查看文件

@@ -0,0 +1,2 @@
1
+# -*- coding: utf-8 -*-
2
+from hapic.ext.bottle.context import BottleContext

+ 57 - 0
hapic/ext/bottle/context.py 查看文件

@@ -0,0 +1,57 @@
1
+# -*- coding: utf-8 -*-
2
+import json
3
+import typing
4
+from http import HTTPStatus
5
+
6
+import bottle
7
+
8
+from hapic.context import ContextInterface
9
+from hapic.exception import OutputValidationException
10
+from hapic.processor import RequestParameters, ProcessValidationError
11
+
12
+
13
+class BottleContext(ContextInterface):
14
+    def get_request_parameters(self, *args, **kwargs) -> RequestParameters:
15
+        return RequestParameters(
16
+            path_parameters=bottle.request.url_args,
17
+            query_parameters=bottle.request.params,
18
+            body_parameters=bottle.request.json,
19
+            form_parameters=bottle.request.forms,
20
+            header_parameters=bottle.request.headers,
21
+        )
22
+
23
+    def get_response(
24
+        self,
25
+        response: dict,
26
+        http_code: int,
27
+    ) -> bottle.HTTPResponse:
28
+        return bottle.HTTPResponse(
29
+            body=json.dumps(response),
30
+            headers=[
31
+                ('Content-Type', 'application/json'),
32
+            ],
33
+            status=http_code,
34
+        )
35
+
36
+    def get_validation_error_response(
37
+        self,
38
+        error: ProcessValidationError,
39
+        http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
40
+    ) -> typing.Any:
41
+        # TODO BS 20171010: Manage error schemas, see #4
42
+        from hapic.hapic import _default_global_error_schema
43
+        unmarshall = _default_global_error_schema.dump(error)
44
+        if unmarshall.errors:
45
+            raise OutputValidationException(
46
+                'Validation error during dump of error response: {}'.format(
47
+                    str(unmarshall.errors)
48
+                )
49
+            )
50
+
51
+        return bottle.HTTPResponse(
52
+            body=json.dumps(unmarshall.data),
53
+            headers=[
54
+                ('Content-Type', 'application/json'),
55
+            ],
56
+            status=int(http_code),
57
+        )