Use mixin for type

This commit is contained in:
Adrien Ferrand 2019-12-28 22:41:57 +01:00
parent b71fcdb0e1
commit 5a1709c85f
2 changed files with 28 additions and 13 deletions

View file

@ -10,7 +10,7 @@ import requests
import six
from acme import fields
from acme.mixins import ResourceMixin
from acme.mixins import ResourceMixin, TypeMixin
logger = logging.getLogger(__name__)
@ -29,19 +29,13 @@ class Challenge(jose.TypedJSONObjectWithFields):
return UnrecognizedChallenge.from_json(jobj)
class ChallengeResponse(ResourceMixin, jose.TypedJSONObjectWithFields):
class ChallengeResponse(ResourceMixin, TypeMixin, jose.TypedJSONObjectWithFields):
# _fields_to_partial_json
"""ACME challenge response."""
TYPES = {} # type: dict
resource_type = 'challenge'
resource = fields.Resource(resource_type)
def to_partial_json(self):
jobj = super(ChallengeResponse, self).to_partial_json()
if self.le_auto_version == 2:
jobj.pop('type', None)
return jobj
class UnrecognizedChallenge(Challenge):
"""Unrecognized challenge.

View file

@ -1,3 +1,13 @@
from josepy import JSONObjectWithFields
from acme.magic_typing import TYPE_CHECKING # pylint: disable=unused-import, no-name-in-module
if TYPE_CHECKING:
_Base = JSONObjectWithFields
else:
_Base = object
class VersionedLEACMEMixin(object):
@property
def le_auto_version(self):
@ -18,12 +28,23 @@ class VersionedLEACMEMixin(object):
super(VersionedLEACMEMixin, self).__setattr__(key, value)
class ResourceMixin(VersionedLEACMEMixin):
def fields_to_partial_json(self):
if hasattr(super(ResourceMixin, self), 'fields_to_partial_json'):
jobj = super(ResourceMixin, self).fields_to_partial_json()
class ResourceMixin(VersionedLEACMEMixin, _Base):
def to_partial_json(self):
if hasattr(super(ResourceMixin, self), 'to_partial_json'):
jobj = super(ResourceMixin, self).to_partial_json()
if self.le_auto_version == 2:
jobj.pop('resource', None)
return jobj
raise AttributeError('This class does not implement method fields_to_partial_json().')
raise AttributeError('This class does not implement method to_partial_json().')
class TypeMixin(VersionedLEACMEMixin, _Base):
def to_partial_json(self):
if hasattr(super(TypeMixin, self), 'to_partial_json'):
jobj = super(TypeMixin, self).to_partial_json()
if self.le_auto_version == 2:
jobj.pop('type', None)
return jobj
raise AttributeError('This class does not implement method to_partial_json().')