Skip to content

file_utils

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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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")

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