From 5a1709c85f3f9929f63348ba6f34efa152f5bf75 Mon Sep 17 00:00:00 2001 From: Adrien Ferrand Date: Sat, 28 Dec 2019 22:41:57 +0100 Subject: [PATCH] Use mixin for type --- acme/acme/challenges.py | 10 ++-------- acme/acme/mixins.py | 31 ++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/acme/acme/challenges.py b/acme/acme/challenges.py index fbaebcbc3..3886180c4 100644 --- a/acme/acme/challenges.py +++ b/acme/acme/challenges.py @@ -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. diff --git a/acme/acme/mixins.py b/acme/acme/mixins.py index 89cf450b2..050155dc3 100644 --- a/acme/acme/mixins.py +++ b/acme/acme/mixins.py @@ -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().')