Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom backend feature #122

Open
2tunnels opened this issue Feb 5, 2021 · 6 comments
Open

Custom backend feature #122

2tunnels opened this issue Feb 5, 2021 · 6 comments

Comments

@2tunnels
Copy link

2tunnels commented Feb 5, 2021

As I understand right now, this package supports only 3 MJML backends: cmd, tcpserver, and httpserver. Also, there is no way to extend this list because all variants are hardcoded.

https://github.com/liminspace/django-mjml/blob/master/mjml/tools.py#L156

I think it would be very nice to have a custom option to pass any path to some callable. This way, anyone can build their backend if needed.

For example:

# settings.py
MJML_BACKEND_MODE = 'myproject.mjml.backends.custom_renderer'

# myproject.mjml.backends.py
def custom_renderer(mjml):
    return requests.get('http://mjml-server.cluster.local', json={'mjml': mjml}).json()['html']

Now rendering part can be easily delegated anywhere we want.

I can create pull request, but I think I will need some help with testing 😄

@liminspace
Copy link
Owner

liminspace commented Feb 5, 2021

@2tunnels hi! it sounds ok, but your custom_renderer is already implemented as httpserver mode :)
It can be a minor feature in the future, but I don't see any reason to implement it right now. People usually uses one of the three modes and if they use something custom, the just implement their server for httpserver mode. I want to keep the library as simple and small as possible.

@2tunnels
Copy link
Author

2tunnels commented Feb 5, 2021

Yes, but HTTP server implementation is hardcoded. Let's say I want to use protobuf instead of JSON, or my request body looks a little different. Or I want to do custom authentication with another header.
This feature will not break anything because it's just an additional check (after cmd, tcpserver and httpserver), and the only thing we need to check if this new string is importable and callable.

@liminspace
Copy link
Owner

I think it will be better to implement class-based backend pattern. I'll think about it.

@2tunnels
Copy link
Author

2tunnels commented Feb 5, 2021

Sounds good! But will callable approach you can use both functions and classes (classes that implement __call__ method).
It's your decision, just saying that function is the simplest API abstraction you can have 😇

@liminspace
Copy link
Owner

I thing class is better because I want to support multiple backends and one backend will be able to initiate with different settings and it also allow the beckend to use instance variables to use caching, collect stats, keeping connections and so on.

@2tunnels
Copy link
Author

2tunnels commented Feb 5, 2021

If you want to separate configuration and execution steps, yes, class approach makes more sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants