mailjet / mailjet-apiv3-python

[API v3] Python Mailjet wrapper
https://dev.mailjet.com
MIT License
109 stars 41 forks source link

`client.contact.get` does not filter on email address #76

Closed hubbs5 closed 3 years ago

hubbs5 commented 3 years ago

I'm trying to filter on email addresses to get IDs to make updates to my contacts, but the function is returning the top 10 emails by default.

This also appears to be an issue missed in the test.py file (see lines 68-71 here). Building off of the test example, this code shows that all three emails are returned each time. As long as the test is only checking for the email in the 0 position, it will pass.

import os
from mailjet_rest import Client

MAILJET_KEY = os.environ.get("MAILJET_KEY")
MAILJET_SECRET = os.environ.get("MAILJET_SECRET")

client = Client(auth=(MAILJET_KEY, MAILJET_SECRET), version='v3')

emails = ['a@mailjet.com', 'b@mailjet.com', 'c@mailjet.com']
# Add emails
for email in emails:
    data = dict(IsExcludedFromCampaigns='false',
        Name=email.split('@')[0],
        Email=email)
    result = client.contact.create(data=data)
    print(f'Status for {email}:\t{result.status_code}')

# Filter by emails
for email in emails:
    result = client.contact.get(filter={'Email': email}).json()
    match = result['Data'][0]['Email']==email
    print(f'Emails match:\t{match}')

I want to be able to return the info associated with one email address, but with this filter behaviour, it will force me to get all of my emails at sort through those each time I query it. I can do this with a small list, but would much rather have the filter argument return only one email address so I can get my IDs. Is there another way to do this?

diskovod commented 3 years ago

Hi, You can try next code:

for email in emails:
    result = client.contact.get(id=email).json()

And as a result, you will get:

{'Count': 1, 'Data': [{'CreatedAt': '2021-06-30T15:14:29Z', 'DeliveredCount': 0, 'Email': 'a@mailjet.com', 'ExclusionFromCampaignsUpdatedAt': '', 'ID': 3735154751, 'IsExcludedFromCampaigns': False, 'IsOptInPending': False, 'IsSpamComplaining': False, 'LastActivityAt': '', 'LastUpdateAt': '', 'Name': 'a', 'UnsubscribedAt': '', 'UnsubscribedBy': ''}], 'Total': 1}
{'Count': 1, 'Data': [{'CreatedAt': '2021-06-30T15:14:29Z', 'DeliveredCount': 0, 'Email': 'b@mailjet.com', 'ExclusionFromCampaignsUpdatedAt': '', 'ID': 3735154752, 'IsExcludedFromCampaigns': False, 'IsOptInPending': False, 'IsSpamComplaining': False, 'LastActivityAt': '', 'LastUpdateAt': '', 'Name': 'b', 'UnsubscribedAt': '', 'UnsubscribedBy': ''}], 'Total': 1}
{'Count': 1, 'Data': [{'CreatedAt': '2021-06-30T15:14:29Z', 'DeliveredCount': 0, 'Email': 'c@mailjet.com', 'ExclusionFromCampaignsUpdatedAt': '', 'ID': 3735154753, 'IsExcludedFromCampaigns': False, 'IsOptInPending': False, 'IsSpamComplaining': False, 'LastActivityAt': '', 'LastUpdateAt': '', 'Name': 'c', 'UnsubscribedAt': '', 'UnsubscribedBy': ''}], 'Total': 1}

Is this a thing what are you looking for?

hubbs5 commented 3 years ago

Yes! That's the output. Still doesn't address the issue with .filter() but that at least solves the issue I was having.