Last active
September 15, 2020 08:41
-
-
Save alexpirine/741a2a394d92648b970ea95cb3504cb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActivationCode: # facultatif | |
pass | |
class ActivationCodeGeneratorInterface: | |
def generate(self, key: str, counter: int, duration_days: int) -> ActivationCode: | |
raise NotImplementedError | |
class RESTAPI: | |
def __init__(self, code_generator: ActivationCodeGeneratorInterface) -> None: | |
self.code_generator = code_generator | |
def generate(self, request) -> Response: | |
return Response.JSON({ | |
'activation_key': self.code_generator.generate(request.key, request.counter, request.duration_days), | |
}) | |
def run(self) -> None: | |
pass | |
class OpenPAYGOActivationCodeGenerator(ActivationCodeGeneratorInterface): | |
def generate(self, key, counter: int, duration_days: int) -> ActivationCode: | |
openpaygolib.generate_token(key, counter, duration_days) | |
class FakeActivationCodeGenerator(ActivationCodeGeneratorInterface): | |
def generate(self, key, counter: int, duration_days: int) -> ActivationCode: | |
return 123456 | |
# app/settings.py | |
CODE_GENERATOR_MODULE = env.str('CODE_GENERATOR_MODULE', 'OpenPAYGO') | |
# app/__init__.py | |
code_generator: ActivationCodeGeneratorInterface | |
if settings.CODE_GENERATOR_MODULE = 'OpenPAYGO': | |
code_generator = OpenPAYGOActivationCodeGenerator() | |
elif settings.CODE_GENERATOR_MODULE = 'fake': | |
code_generator = FakeActivationCodeGenerator() | |
else: | |
raise ValueError(f"CODE_GENERATOR_MODULE setting not valid") | |
rest_api = RESTAPI(code_generator=code_generator) | |
# start.py | |
import app | |
app.rest_api.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment