From f8022b079e3a1c79f6b09810855d128ed93bf6be Mon Sep 17 00:00:00 2001 From: Julien Fraichot Date: Wed, 22 Jun 2022 11:43:36 -0500 Subject: [PATCH] refactor(JSONLD): centralize jsonld handler --- cert_issuer/certificate_handlers.py | 9 ++++----- cert_issuer/normalization_handler.py | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 cert_issuer/normalization_handler.py diff --git a/cert_issuer/certificate_handlers.py b/cert_issuer/certificate_handlers.py index 2bcbbf2d..857da667 100644 --- a/cert_issuer/certificate_handlers.py +++ b/cert_issuer/certificate_handlers.py @@ -1,19 +1,19 @@ import json import logging -from cert_schema import normalize_jsonld from cert_issuer import helpers from cert_issuer.proof_handler import ProofHandler from pycoin.serialize import b2h +from cert_issuer.normalization_handler import JSONLDHandler from cert_issuer.models import CertificateHandler, BatchHandler from cert_issuer.signer import FinalizableSigner + class CertificateV3Handler(CertificateHandler): def get_byte_array_to_issue(self, certificate_metadata): certificate_json = self._get_certificate_to_issue(certificate_metadata) - normalized = normalize_jsonld(certificate_json, detect_unmapped_fields=False) - return normalized.encode('utf-8') + return JSONLDHandler.normalize_to_utf8(certificate_json) def add_proof(self, certificate_metadata, merkle_proof): """ @@ -34,8 +34,7 @@ def _get_certificate_to_issue(self, certificate_metadata): class CertificateWebV3Handler(CertificateHandler): def get_byte_array_to_issue(self, certificate_json): - normalized = normalize_jsonld(certificate_json, detect_unmapped_fields=False) - return normalized.encode('utf-8') + return JSONLDHandler.normalize_to_utf8(certificate_json) def add_proof(self, certificate_json, merkle_proof): certificate_json = ProofHandler().add_proof(certificate_json, merkle_proof) diff --git a/cert_issuer/normalization_handler.py b/cert_issuer/normalization_handler.py new file mode 100644 index 00000000..ab3f9a5f --- /dev/null +++ b/cert_issuer/normalization_handler.py @@ -0,0 +1,8 @@ +from cert_schema import normalize_jsonld + + +class JSONLDHandler: + @staticmethod + def normalize_to_utf8(certificate_json): + normalized = normalize_jsonld(certificate_json, detect_unmapped_fields=False) + return normalized.encode('utf-8')