novuhq / novu-python

Python SDK for Novu - The open-source notification infrastructure for engineers. 🚀
https://novu-python.readthedocs.io
MIT License
71 stars 32 forks source link

LayoutApi alway returns HTTP error 400 #244

Open gjae opened 5 months ago

gjae commented 5 months ago

I'm trying creates an layout using Layout Api always returns error 400. Current implementation:

body = "html readed from a file, contains {{{body}}}"
variables = [
    {"type": "String", "name": "first_name", "required": False, "defValue": ""},
    {"type": "String", "name": "lastname", "required": False, "defValue": ""},
    {"type": "String", "name": "email", "required": False, "defValue": ""},
    {"type": "String", "name": "cellphone", "required": False, "defValue": ""},
    {"type": "String", "name": "url", "required": False, "defValue": ""},
    {"type": "String", "name": "path", "required": False, "defValue": ""},
    {"type": "String", "name": "uuid", "required": False, "defValue": ""},
    {"type": "String", "name": "id", "required": False, "defValue": ""},
    {"type": "String", "name": "instagram", "required": False, "defValue": ""},
    {"type": "String", "name": "facebook", "required": False, "defValue": ""},
]
variables_dto = []
for variable in variables:
    variables_dto.append(LayoutVariableDto(
        name=variable["name"],
        type=TemplateVariableTypeEnum.STRING,
    ))

new_layout = LayoutDto(
    name=template_name,
    description="mail templates",
    content=body,
    is_default=False,
    variables=variables_dto,
)
response = None
try:
    response = LayoutApi(settings.NOVU_DOMAIN, settings.NOVU_API_KEY).create(new_layout)
    NotificationTemplate.objects.create(
        name=template_name,
        local_identifier=identifier,
        novu_id=response._id,
        variables=json.dumps(variables)
    )
except Exception as e:
    print(response, e)
    raise e

I need automate the layouts creation

image

Additional context I'm using django, the api key it's a environment variable

ryshu commented 3 months ago

Hi @gjae,

Sorry for the delay.

When you receive a 400 type error, the problem does not necessarily come from the SDK (it is still possible, but quite rare).

In this scenario, you can analyze the server response that is in the HTTP error (exc.response).

To analyze the latter, you have several options:

Sentry is mainly recommended for collecting and monitoring errors in Python programs. If you have other similar and Opensource collectors that you would like to integrate, do not hesitate to open an issue to request its implementation.