Skip to content

file_utils

MAXIMUM_JSON_ATTACHMENT_SIZE = 3 * 1024 * 1024 module-attribute

The maximum size of an attachment that can be sent using json.

attach_file_request_builder(file_path)

Build a request to attach a file.

Attributes:

Name Type Description
file_path

The path to the file to attach.

Returns:

Type Description
CreateAttachmentRequest

A properly-formatted request to attach the file.

Source code in nylas/utils/file_utils.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def attach_file_request_builder(file_path) -> CreateAttachmentRequest:
    """
    Build a request to attach a file.

    Attributes:
        file_path: The path to the file to attach.

    Returns:
        A properly-formatted request to attach the file.
    """
    path = Path(file_path)
    filename = path.name
    size = os.path.getsize(file_path)
    content_type = mimetypes.guess_type(file_path)[0]
    file_stream = open(file_path, "rb")  # pylint: disable=consider-using-with

    return {
        "filename": filename,
        "content_type": content_type if content_type else "application/octet-stream",
        "content": file_stream,
        "size": size,
    }