sendinblue / APIv3-python-library

SendinBlue's python library for API v3
MIT License
82 stars 40 forks source link

Issue in Create Contact - `update_enabled` does not update existing user #43

Closed QuillyT closed 5 years ago

QuillyT commented 5 years ago

Hi,

I can't get create_contact() to update an existing user.

contact = CreateContact(email=user.email, 
        attributes=build_contact_attributes(user),
        list_ids=[MAIN_LIST],
        update_enabled=True)

# this works the first time
self.api_instance.create_contact(contact)

# update the contact attributes
# ...

# this fails
self.api_instance.create_contact(contact)

This will successfully create a new contact, but it won't update an existing user. I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "discotech/services/sendinblue.py", line 69, in add_user
    self.api_instance.create_contact(contact)
  File "/Users/quentintai/.virtualenvs/disco/lib/python2.7/site-packages/sib_api_v3_sdk/api/contacts_api.py", line 272, in create_contact
    (data) = self.create_contact_with_http_info(create_contact, **kwargs)  # noqa: E501
  File "/Users/quentintai/.virtualenvs/disco/lib/python2.7/site-packages/sib_api_v3_sdk/api/contacts_api.py", line 349, in create_contact_with_http_info
    collection_formats=collection_formats)
  File "/Users/quentintai/.virtualenvs/disco/lib/python2.7/site-packages/sib_api_v3_sdk/api_client.py", line 323, in call_api
    _preload_content, _request_timeout)
  File "/Users/quentintai/.virtualenvs/disco/lib/python2.7/site-packages/sib_api_v3_sdk/api_client.py", line 153, in __call_api
    _request_timeout=_request_timeout)
  File "discotech/services/sendinblue.py", line 41, in request
    return super(AdaptedApiClient, self).request(*args, **kwargs)
  File "/Users/quentintai/.virtualenvs/disco/lib/python2.7/site-packages/sib_api_v3_sdk/api_client.py", line 366, in request
    body=body)
  File "/Users/quentintai/.virtualenvs/disco/lib/python2.7/site-packages/sib_api_v3_sdk/rest.py", line 275, in POST
    body=body)
  File "/Users/quentintai/.virtualenvs/disco/lib/python2.7/site-packages/sib_api_v3_sdk/rest.py", line 228, in request
    raise ApiException(http_resp=r)
sib_api_v3_sdk.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Content-Length': '62', 'X-XSS-Protection': '1', 'X-Content-Type-Options': 'nosniff', 'X-Download-Options': 'noopen', 'X-Sib-Server': 'SENDINBLUE-web2-2', 'Set-Cookie': '__cfduid=dc17843d3834482948f2134c87b81fc471563300571; expires=Wed, 15-Jul-20 18:09:31 GMT; path=/; domain=.sendinblue.com; HttpOnly', 'Strict-Transport-Security': 'max-age=0; includeSubDomains', 'Vary': 'Origin, Accept-Encoding', 'Server': 'cloudflare', 'Connection': 'keep-alive', 'ETag': 'W/"3e-SzFgjSK+IXMEOELGg5nYGmtfxPk"', 'Access-Control-Allow-Credentials': 'true', 'Date': 'Tue, 16 Jul 2019 18:09:31 GMT', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Type': 'application/json; charset=utf-8', 'CF-RAY': '4f75ed7b4d18c17a-IAD'})
HTTP response body: {"code":"invalid_parameter","message":"Contact already exist"}

I know the server is handling the API correctly because I have successfully made changes to an existing user here: https://developers.sendinblue.com/reference#createcontact

Seems like the library is missing something before making the request.

QuillyT commented 5 years ago

I figured out the problem. I was passing in the same email to the attributes of the CreateContact object. Without setting the EMAIL key under attributes, I passes.

I now realize that the purpose of setting the EMAIL attribute is to update the email of an existing contact.

I think it would be helpful to include this in the documentation.