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

LayoutApi alway returns HTTP error 400 #244

Open
gjae opened this issue Mar 19, 2024 · 1 comment
Open

LayoutApi alway returns HTTP error 400 #244

gjae opened this issue Mar 19, 2024 · 1 comment

Comments

@gjae
Copy link

gjae commented Mar 19, 2024

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

  • Novu version: last version (using api.novu.co)
  • Python version: 3.10.13
  • Novu-python version: 1.14.0

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

@ryshu
Copy link
Collaborator

ryshu commented May 12, 2024

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:

  • Via code. The idea here is to intercept HTTPError type errors to obtain the details of the latter

    from requests import HTTPError
    
    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 HTTPError as exc:
        # Do some stuff with the error, for exemple print server response when an error 400 is returned.
        if exc.response.status_code == 400:
            print(exc.response.json())
        raise exc
  • If you use Sentry, the program automatically adds an extra field called 'error_details' that you can find in your errors.

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.

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