nylas-python icon indicating copy to clipboard operation
nylas-python copied to clipboard

Create Draft Error

Open JohnBurnsVC opened this issue 2 years ago • 3 comments

Describe the bug When creating a draft, we are now using multipart/form-data content-type as opposed to application/json which was used in V2. It appears there may be a bug either in the API itself or in this repo. I played around locally to hit the endpoint with an application/json request and it worked fine, but when using the code as is (with data=_build_form_request(request_body)) I get nylas.models.errors.NylasApiError: provider returned error.

To Reproduce

from dotenv import load_dotenv
load_dotenv()

import os
import sys
from nylas import Client

nylas = Client(
    os.environ.get('NYLAS_API_KEY'),
    os.environ.get('NYLAS_API_URI')
)

grant_id = os.environ.get("NYLAS_GRANT_ID")
email = os.environ.get("EMAIL")

draft = nylas.drafts.create(
  grant_id,
  request_body={
    "to": [{ "name": "Name", "email": email }],
    "reply_to": [{ "name": "Name", "email": email }],
    "subject": "Your Subject Here",
    "body": "Your email body here.",
  }
)

print(draft)

Gets this error:

File "/usr/local/lib/python3.9/site-packages/nylas/resources/drafts.py", line 86, in create
    json_response = self._http_client._execute(
File "/usr/local/lib/python3.9/site-packages/nylas/handler/http_client.py", line 101, in _execute
    return _validate_response(response)
File "/usr/local/lib/python3.9/site-packages/nylas/handler/http_client.py", line 32, in _validate_response
    raise NylasApiError(parsed_error, response.status_code)
    nylas.models.errors.NylasApiError: provider returned error

BUT, if you go into the nylas.resources.drafts.create() method and force it to skip the multipart/form-data logic, it works fine

def create(
    self, identifier: str, request_body: CreateDraftRequest
) -> Response[Draft]:
        """
        Pass in request_body var to use 'application/json', instead of calling
        _build_form_request(request_body) and passing that into the data param
        """
        json_response = self._http_client._execute(
            method="POST",
            path=f"/v3/grants/{identifier}/drafts",
            request_body=request_body,
        )

        return Response.from_dict(json_response, Draft)

Expected behavior I would expect this should successfully create a draft.

SDK Version: v6.0.1 (with requests v2.31.0)

Additional context Additionally, when going to the V3 API docs for the create draft endpoint, scrolling down to the REQUEST BODY section, setting it to multipart/form-data, setting a valid grant-id, then trying the example request it errors out there as well! But switch over to the default application/json type and it works again. So not sure if it's the actual API or...?

JohnBurnsVC avatar Feb 26 '24 16:02 JohnBurnsVC

Thanks @JohnBurnsVC we will take a look and let you know once a fix is available 😉

atejada avatar Feb 26 '24 16:02 atejada

@JohnBurnsVC Thanks for opening this issue. As you've stated this error is linked to some instability on the API side with multipart usage on the messages API. We're working on moving our SDKs off of always defaulting to multipart and instead using json where applicable, reserving multipart for only large attachments. Once we have an update for you we'll share it, thanks!

mrashed-dev avatar Feb 27 '24 19:02 mrashed-dev

Hey @JohnBurnsVC just giving you an update, we've opened a PR where we default to application/json for any attachment-related requests, unless the size exceeds 3mb. Once merged it will be included in the next release.

mrashed-dev avatar Mar 01 '24 00:03 mrashed-dev