Skip to content

Commit 3f6d0a6

Browse files
authored
Merge pull request #141 from TTWShell/bugfix/response
fixed response inheritance: status is property and use with setter
2 parents 3b450c0 + 0ec9d32 commit 3f6d0a6

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

hobbit_core/response.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from mypy_extensions import TypedDict
33

44
from flask.json import dumps
5-
from flask import current_app
6-
from werkzeug import Response
5+
from flask import current_app, Response
76

87
RESP_MSGS = {
98
200: 'ok',
@@ -67,58 +66,59 @@ def gen_response(code: int, message: str = None, detail: Optional[str] = None,
6766
class Result(Response):
6867
"""Base json response.
6968
"""
70-
status = 200 # type: ignore
69+
_hobbit_status = 200 # type: ignore
7170

7271
def __init__(self, response=None, status=None, headers=None,
7372
mimetype='application/json', content_type=None,
7473
direct_passthrough=False):
7574
assert sorted(response.keys()) == [
7675
'code', 'data', 'detail', 'message'], \
7776
'Error response, must include keys: code, data, detail, message'
78-
super(Result, self).__init__(
77+
super().__init__(
7978
response=dumps(response, indent=0, separators=(',', ':')) + '\n',
80-
status=status if status is not None else self.status,
79+
status=status if status is not None else self._hobbit_status,
8180
headers=headers, mimetype=mimetype,
8281
content_type=content_type, direct_passthrough=direct_passthrough)
8382

8483

8584
class SuccessResult(Result):
8685
"""Success response. Default status is 200, you can cover it by status arg.
8786
"""
88-
status = 200
87+
_hobbit_status = 200
8988

9089
def __init__(self, message: str = '', code: Optional[int] = None,
9190
detail: Any = None, status: Optional[int] = None, data=None):
92-
super(SuccessResult, self).__init__(
93-
gen_response(code if code is not None else self.status,
91+
super().__init__(
92+
gen_response(code if code is not None else self._hobbit_status,
9493
message, detail, data),
95-
status or self.status)
94+
status or self._hobbit_status)
9695

9796

9897
class FailedResult(Result):
9998
"""Failed response. status always 400.
10099
"""
101-
status = 400
100+
_hobbit_status = 400
102101

103102
def __init__(self, message: str = '', code: Optional[int] = None,
104103
detail: Any = None):
105-
super(FailedResult, self).__init__(
104+
super().__init__(
106105
gen_response(
107-
code if code is not None else self.status, message, detail),
108-
self.status)
106+
code if code is not None else self._hobbit_status,
107+
message, detail),
108+
self._hobbit_status)
109109

110110

111111
class UnauthorizedResult(FailedResult):
112-
status = 401
112+
_hobbit_status = 401
113113

114114

115115
class ForbiddenResult(FailedResult):
116-
status = 403
116+
_hobbit_status = 403
117117

118118

119119
class ValidationErrorResult(FailedResult):
120-
status = 422
120+
_hobbit_status = 422
121121

122122

123123
class ServerErrorResult(FailedResult):
124-
status = 500
124+
_hobbit_status = 500

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def gen_data(data_root='static'):
3232

3333
setup(
3434
name='hobbit-core',
35-
version='3.0.0.rc2',
35+
version='3.0.0.rc3',
3636
python_requires='>=3.7, <4',
3737
description='Hobbit - A flask project generator.',
3838
long_description=long_description,

tests/test_err_handler.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ class TestErrHandler(BaseTest):
1212

1313
def test_assertion_error(self, app):
1414
resp = ErrHandler.handler(AssertionError('message'))
15-
assert resp.status == 422
15+
assert resp.status_code == 422
1616
data = json.loads(resp.get_data())
1717
assert data['message'] == 'message'
1818

1919
def test_sqlalchemy_orm_exc(self, app):
2020
resp = ErrHandler.handler(orm_exc.NoResultFound())
21-
assert resp.status == 404, resp.get_json()
21+
assert resp.status_code == 404, resp.get_json()
2222
data = json.loads(resp.get_data())
2323
assert data['message'] == u'源数据未找到'
2424

2525
resp = ErrHandler.handler(orm_exc.UnmappedError())
26-
assert resp.status == 500
26+
assert resp.status_code == 500
2727
data = json.loads(resp.get_data())
2828
assert data['message'] == u'服务器内部错误'
2929

3030
def test_werkzeug_exceptions(self, app):
3131
resp = ErrHandler.handler(wkz_exc.Unauthorized())
32-
assert resp.status == 401
32+
assert resp.status_code == 401
3333
data = json.loads(resp.get_data())
3434
assert data['message'] == u'未登录'
3535

3636
def test_hobbit_exception(self, app):
3737
resp = ErrHandler.handler(HobbitException('msg'))
38-
assert resp.status == 400
38+
assert resp.status_code == 400
3939
data = json.loads(resp.get_data())
4040
assert data['message'] == 'msg'
4141
# py27,py36 == "Exception('msg',)"
@@ -44,7 +44,7 @@ def test_hobbit_exception(self, app):
4444

4545
def test_others(self, app):
4646
resp = ErrHandler.handler(Exception('msg'))
47-
assert resp.status == 500
47+
assert resp.status_code == 500
4848
data = json.loads(resp.get_data())
4949
assert data['message'] == u'服务器内部错误'
5050
# py27,py36 == "Exception('msg',)"

tests/test_response.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ def test_result(self):
3030
'code': '1', 'message': u'unknown', 'detail': None, 'data': None}
3131
result = Result(response)
3232
print(result.__dict__)
33-
assert result.status == 200
33+
assert result.status_code == 200
3434

3535
result = Result(response, status=201)
36-
assert result.status == 201
36+
assert result.status_code == 201
3737

3838
def test_success_result(self, app):
3939
# assert status can rewrite
4040
excepted = b'{\n"code":"200",\n"data":null,\n"detail":null,\n"message":"message"\n}\n' # NOQA
4141
result = SuccessResult('message', status=301)
42-
assert result.status == 301
42+
assert result.status_code == 301
4343
assert excepted == result.data
4444

4545
# assert default is 200
4646
result = SuccessResult()
47-
assert result.status == 200
47+
assert result.status_code == 200
4848

4949
app.config['HOBBIT_USE_CODE_ORIGIN_TYPE'] = True
5050
result = SuccessResult(code=0)
@@ -61,7 +61,7 @@ def test_success_result(self, app):
6161

6262
def test_failed_result(self):
6363
result = FailedResult()
64-
assert result.status == 400
64+
assert result.status_code == 400
6565

6666
@pytest.mark.parametrize('result, excepted_status', [
6767
(UnauthorizedResult, 401),
@@ -70,4 +70,4 @@ def test_failed_result(self):
7070
(ServerErrorResult, 500),
7171
])
7272
def test_results(self, result, excepted_status):
73-
assert result().status == excepted_status
73+
assert result().status_code == excepted_status

0 commit comments

Comments
 (0)