9 changed files with 69 additions and 73 deletions
@ -0,0 +1,36 @@
|
||||
from django.http import JsonResponse |
||||
from lemoncurry.middleware import ResponseException |
||||
from typing import Optional |
||||
|
||||
|
||||
def forbidden() -> ResponseException: |
||||
return res('forbidden', 403) |
||||
|
||||
|
||||
def unauthorized() -> ResponseException: |
||||
return res('unauthorized', 401) |
||||
|
||||
|
||||
def bad_req(msg: str) -> ResponseException: |
||||
return res('invalid_request', msg=msg) |
||||
|
||||
|
||||
def bad_type(type: str) -> ResponseException: |
||||
msg = 'unsupported request type {0}'.format(type) |
||||
return res('invalid_request', 415, msg) |
||||
|
||||
|
||||
def bad_scope(scope: str) -> ResponseException: |
||||
return res('insufficient_scope', 401, scope=scope) |
||||
|
||||
|
||||
def res(error: str, |
||||
status: Optional[int]=400, |
||||
msg: Optional[str]=None, |
||||
scope: Optional[str]=None): |
||||
content = {'error': error} |
||||
if msg is not None: |
||||
content['error_description'] = msg |
||||
if scope: |
||||
content['scope'] = scope |
||||
return ResponseException(JsonResponse(content, status=status)) |
@ -1,31 +0,0 @@
|
||||
from django.http import JsonResponse |
||||
|
||||
|
||||
def forbidden(): |
||||
return res('forbidden', 403) |
||||
|
||||
|
||||
def unauthorized(): |
||||
return res('unauthorized', 401) |
||||
|
||||
|
||||
def bad_req(msg): |
||||
return res('invalid_request', msg=msg) |
||||
|
||||
|
||||
def bad_type(type): |
||||
msg = 'unsupported request type {0}'.format(type) |
||||
return res('invalid_request', 415, msg) |
||||
|
||||
|
||||
def bad_scope(scope): |
||||
return res('insufficient_scope', 401, scope=scope) |
||||
|
||||
|
||||
def res(error, status=400, msg=None, scope=None): |
||||
content = {'error': error} |
||||
if msg: |
||||
content['error_description'] = msg |
||||
if scope: |
||||
content['scope'] = scope |
||||
return JsonResponse(content, status=status) |
Loading…
Reference in new issue