refactored renders, added Datetime to typesystem

email
androiddrew 7 years ago
parent 3cd05013ab
commit 3a6ae5df43

@ -8,11 +8,11 @@ from apistar.interfaces import Router, Injector, Auth
from apistar_jwt.authentication import JWTAuthentication
from apistar_jwt.exceptions import AuthenticationFailed
from cookie_api.render import JSONRenderer
from cookie_api.renders import JSONRenderer
from cookie_api.commands import commands
from cookie_api.models.schema import Base, Cookie
from .auth import auth_routes, auth_components
from cookie_api.auth import auth_routes, auth_components
@annotate(authentication=[JWTAuthentication()])

@ -0,0 +1,17 @@
import datetime
import dateutil
from apistar.typesystem import TypeSystemError
class Datetime(datetime.datetime):
native_type = datetime.datetime
def __new__(cls, *args, **kwargs) -> datetime:
if args and isinstance(args[0], cls.native_type):
return args[0]
if args and isinstance(args[0], str):
try:
return dateutil.parser.parse(args[0])
except ValueError:
raise TypeSystemError(cls=cls, code='type') from None
return cls.native_type(*args, **kwargs)

@ -1,10 +0,0 @@
import datetime as dt
import decimal
def alchemyencoder(obj):
"""JSON encoder function with support for ISO 8601 datetime serialization and Decimal to float casting"""
if isinstance(obj, dt.datetime):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return float(obj)

@ -1,14 +0,0 @@
import json
from cookie_api.models.util import alchemyencoder
from apistar import http
from apistar.renderers import Renderer
class JSONRenderer(Renderer):
media_type = 'application/json'
charset = None
def render(self, data: http.ResponseData) -> bytes:
return json.dumps(data, default=alchemyencoder).encode('utf-8')

@ -0,0 +1,23 @@
import datetime as dt
import decimal
import json
from apistar import http
from apistar.renderers import Renderer
def extended_encoder(obj):
"""JSON encoder function with support for ISO 8601 datetime serialization and Decimal to float casting"""
if isinstance(obj, dt.datetime):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return float(obj)
class JSONRenderer(Renderer):
"""JSON Render with support for ISO 8601 datetime serialization and Decimal to float casting"""
media_type = 'application/json'
charset = None
def render(self, data: http.ResponseData) -> bytes:
return json.dumps(data, default=extended_encoder).encode('utf-8')

@ -16,6 +16,7 @@ py==1.4.34
pycparser==2.18
PyJWT==1.5.3
pytest==3.2.3
python-dateutil==2.6.1
requests==2.18.4
six==1.11.0
SQLAlchemy==1.1.14

@ -76,5 +76,5 @@ def test_valid_blacklisted_token_logout():
pass
def test_valid_blacklisted_token_user(self):
def test_valid_blacklisted_token_user():
""" Test for user status with a blacklisted valid token """

Loading…
Cancel
Save