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

Fix python FileAttachment example content_bytes b64 padding #9457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stupoid
Copy link

@stupoid stupoid commented Nov 8, 2024

It seems that the python example content_bytes for FileAttachment is missing the expected b64 padding.

In [2]: content_bytes = base64.urlsafe_b64decode("R0lGODdhEAYEAA7")
---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
Cell In[2], line 1
----> 1 content_bytes = base64.urlsafe_b64decode("R0lGODdhEAYEAA7")

File ~/.pyenv/versions/3.12.7/lib/python3.12/base64.py:134, in urlsafe_b64decode(s)
    132 s = _bytes_from_decode_data(s)
    133 s = s.translate(_urlsafe_decode_translation)
--> 134 return b64decode(s)

File ~/.pyenv/versions/3.12.7/lib/python3.12/base64.py:88, in b64decode(s, altchars, validate)
     86     assert len(altchars) == 2, repr(altchars)
     87     s = s.translate(bytes.maketrans(altchars, b'+/'))
---> 88 return binascii.a2b_base64(s, strict_mode=validate)

Error: Incorrect padding

In [3]: content_bytes = base64.urlsafe_b64decode("R0lGODdhEAYEAA7=")

In [4]: content_bytes
Out[4]: b'GIF87a\x10\x06\x04\x00\x0e'

This can be easily fixed by adding the expected padding as shown.

This documentation is a little confusing as there are some SDKs where the b64 encoding has to be done explicitly, fixing this into a working examples helps to communicate that content_bytes expects raw data (not base64 encoded).

Hopefully there might be other additional steps that can be taken to communicate that as well. (e.g. comments, explicit mentions in the text).

It seems that the python example content_bytes for FileAttachment is missing the expected b64 padding.

```py
In [2]: content_bytes = base64.urlsafe_b64decode("R0lGODdhEAYEAA7")
---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
Cell In[2], line 1
----> 1 content_bytes = base64.urlsafe_b64decode("R0lGODdhEAYEAA7")

File ~/.pyenv/versions/3.12.7/lib/python3.12/base64.py:134, in urlsafe_b64decode(s)
    132 s = _bytes_from_decode_data(s)
    133 s = s.translate(_urlsafe_decode_translation)
--> 134 return b64decode(s)

File ~/.pyenv/versions/3.12.7/lib/python3.12/base64.py:88, in b64decode(s, altchars, validate)
     86     assert len(altchars) == 2, repr(altchars)
     87     s = s.translate(bytes.maketrans(altchars, b'+/'))
---> 88 return binascii.a2b_base64(s, strict_mode=validate)

Error: Incorrect padding

In [3]: content_bytes = base64.urlsafe_b64decode("R0lGODdhEAYEAA7=")

In [4]: content_bytes
Out[4]: b'GIF87a\x10\x06\x04\x00\x0e'
```

This can be easily fixed by adding the expected padding as shown.

This documentation is a little confusing as there are some SDKs where the b64 encoding has to be done
explicitly, fixing this into a working examples helps to communicate that `content_bytes` expects raw
data (not base64 encoded).

Hopefully there might be other additional steps that can be taken to communicate that as well.
(e.g. comments, explicit mentions in the text).
Copy link

Learn Build status updates of commit 5c16a87:

✅ Validation status: passed

File Status Preview URL Details
api-reference/v1.0/includes/snippets/python/create-file-attachment-from-message-v1-python-snippets.md ✅Succeeded

For more details, please refer to the build report.

For any questions, please:

@Lauragra
Copy link
Contributor

Lauragra commented Nov 9, 2024

@andrueastman , FYI, I filed Issue #951 in the Python SDK repo to track this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants