api-client-python icon indicating copy to clipboard operation
api-client-python copied to clipboard

Attachment model missing __init__

Open Sorthos opened this issue 6 years ago • 2 comments

I was trying to programmatically build templates with attachments and found that I couldn't do it due to an error in the models.py file. I modified it on my own end and it works perfectly now. I will submit a pull request with the update.

Sorthos avatar Oct 24 '19 19:10 Sorthos

FYI -- this is still an issue. Just need to add this to the Attachment class in models.py

def __init__(self, **kwargs):
        for key, default in Page._valid_properties.items():
            setattr(self, key, kwargs.get(key, default))

bridge-four avatar Sep 14 '20 15:09 bridge-four

I think there is a littel typo on the message above. Here is the complete Attachment class :

class Attachment(Model):
    _valid_properties = {'content': None, 'type': None, 'name': None}

    def __init__(self, **kwargs):
            for key, default in Attachment._valid_properties.items():
                setattr(self, key, kwargs.get(key, default))

    @classmethod
    def parse(cls, json):
        attachment = cls()
        for key, val in json.items():
            if key in cls._valid_properties:
                setattr(attachment, key, val)
        return attachment

and here is a working example to put attachment using the python api :

        with open("attachment.png", 'rb') as bin_blob:
            b64_attachment_content = base64.b64encode(bin_blob.read())

        attachment_mimetype = mimetypes.guess_type(attachment.png)[0]

        template = Template(name="test",
            html="<h1> hello world </h1>",
            subject="click click bang bang",
            attachments=[Attachment.parse({"name":"attachment.png", "type":attachment_mimetype, "content":b64_attachment_content.decode()})])

        template = api.templates.post(template)

makim0n avatar Jun 20 '22 19:06 makim0n