Skip to content

Attachment

Defines the attachment schema.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id)

References

NONE

Attachment

Bases: BaseModel

Defines a file attachment schema.

Attributes:

Name Type Description
data bytes

The content data of the file attachment.

filename str

The filename of the file attachment.

mime_type str

The mime type of the file attachment.

extension str

The extension of the file attachment.

url str | None

The URL of the file attachment. Defaults to None.

metadata dict[str, Any]

The metadata of the file attachment. Defaults to an empty dictionary.

__repr__()

Return string representation of the Attachment.

Returns:

Name Type Description
str str

The string representation of the Attachment.

__str__()

Return string representation of the Attachment.

Returns:

Name Type Description
str str

The string representation of the Attachment.

from_base64(base64_data, filename=None, metadata=None) classmethod

Creates an Attachment from a base64 string.

Parameters:

Name Type Description Default
base64_data str

The base64 string of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the mime type.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_bytes(bytes, filename=None, metadata=None) classmethod

Creates an Attachment from bytes.

Parameters:

Name Type Description Default
bytes bytes

The bytes of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the extension.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_data_url(data_url, filename=None, metadata=None) classmethod

Creates an Attachment from a data URL (data:[mime/type];base64,[bytes]).

Parameters:

Name Type Description Default
data_url str

The data URL of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the mime type.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_path(path, filename=None, metadata=None) classmethod

Creates an Attachment from a path.

Parameters:

Name Type Description Default
path str

The path to the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the path.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_url(url, filename=None, metadata=None) classmethod

Creates an Attachment from a URL.

Parameters:

Name Type Description Default
url str

The URL of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the URL.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

write_to_file(path=None)

Writes the Attachment to a file.

Parameters:

Name Type Description Default
path str | None

The path to the file. Defaults to None, in which case the filename will be used as the path.

None