Skip to content

Commit

Permalink
Merge pull request #233 from OGAWAHirofumi/use-json
Browse files Browse the repository at this point in the history
Use json
  • Loading branch information
ThomasWaldmann authored Nov 7, 2020
2 parents 434701d + f991fb7 commit 63a3115
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/bepasty/apis/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@
# "/lodgeit/" in the same blueprint and there is no way to exclude
# from using the same error handler ("/lodgeit/" is not REST api).
def rest_errorhandler(func):
def error_message(description, code):
return jsonify({
'error': {'code': code, 'message': description},
}), code

def handler(*args, **kwargs):
try:
return func(*args, **kwargs)
except HTTPException as exc:
return exc.description, exc.code
return error_message(exc.description, exc.code)
except Exception:
if current_app.propagate_exceptions:
# if testing/debug mode, re-raise
raise
exc = InternalServerError()
return exc.description, exc.code
return error_message(exc.description, exc.code)

return handler

Expand Down Expand Up @@ -79,6 +84,8 @@ def update_item(self, item, name):

# Make a Response and create Transaction-ID from ItemName
response = make_response()
response.headers['Content-Type'] = 'application/json'
response.data = '{}'
name_b = name if isinstance(name, bytes_type) else name.encode()
trans_id_b = base64.b64encode(name_b)
trans_id_s = trans_id_b if isinstance(trans_id_b, str) else trans_id_b.decode()
Expand Down
10 changes: 6 additions & 4 deletions src/bepasty/tests/test_rest_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ def check_response(response, code, ftype='application/json', check_data=True):


def check_err_response(response, code, check_data=True):
check_response(response, code, 'text/html; charset=utf-8', check_data)
check_response(response, code, check_data=check_data)
if check_data:
assert code == response.json['error']['code']
# check if doesn't have html tag
assert not re.match(r'<.+>', response.data.decode())

Expand All @@ -183,14 +185,14 @@ def check_data_response(response, meta, data, offset=0, total_size=None,
assert data == response.data


def check_json_response(response, metas, check_data=True):
check_response(response, 200, check_data=check_data)
def check_json_response(response, metas, code=200, check_data=True):
check_response(response, code, check_data=check_data)
if check_data:
assert metas == response.json


def check_upload_response(response, code=201, check_data=True):
check_response(response, code, 'text/html; charset=utf-8', check_data)
check_json_response(response, {}, code=code, check_data=check_data)

if code == 200:
assert len(response.headers[TRANSACTION_ID]) > 0
Expand Down

0 comments on commit 63a3115

Please sign in to comment.