robertmrk / aiosfstream

Salesforce Streaming API client for asyncio
MIT License
47 stars 30 forks source link

Subscribing to /data/UserChangeEvent raises an exception. #6

Closed nbyrnes-acv closed 5 years ago

nbyrnes-acv commented 5 years ago

Hi There,

Thanks for putting this library together! It's really given us a leg up on some proof of concept work.

When I subscribe to the /data/ChangeEvents I do not get any errors (nor data, bit I suspect that is account related). But, when I subscribe to any standard object ChangeEvents stream, I get: 
aiosfstream.exceptions.TransportError: 0, message='Attempt to decode JSON with unexpected mimetype: '                                                                                        

Any ideas / suggestions?

Thanks and Regards,
Nate
robertmrk commented 5 years ago

Hi @nbyrnes-acv

According to the specs the payload of all the requests and responses should be in JSON format. However, sometimes if Salesforce denies access to some resource, the response contains HTML instead. I suspect this is the cause of the error.

I'll try to make some changes to make this clear to the users of the library. But first we would need to know exactly what happened.

Please configure DEBUG level logging for your application and send me the relevant log messages. You can achieve this by inserting the following lines of code at the top of the file which serves as your entry point

import logging

logging.basicConfig(level=logging.DEBUG)
nbyrnes-acv commented 5 years ago

Hi @robertmrk

Here you go:

DEBUG:asyncio:Using selector: EpollSelector
DEBUG:aiosfstream.client:Client created with replay storage: ConstantReplayId(default_id=<ReplayOption.NEW_EVENTS: -1>), replay fallback: None
DEBUG:aiosfstream.client:Authenticating using PasswordAuthenticator(consumer_key='<REDACTED>',consumer_secret='<REDACTED>', username='<REDACTED>', pas
sword='y40Sz!%BSnYN...DVCrTV5AvfuAt').
INFO:aiosfstream.client:Successful authentication. Instance URL: 'https://na91.salesforce.com'.
INFO:aiocometd.client:Opening client with connection types ['websocket', 'long-polling'] ...
INFO:aiocometd.client:Connection types supported by the server: ['long-polling']
DEBUG:aiocometd.transports.base:Connect task finished with: {'clientId': '62h0jtahnqrt9qiowjyjjea7dc', 'advice': {'interval': 0, 'timeout': 110000, 'reconnect': 'retry'}, 'channel': '/meta/c
onnect', 'id': '1', 'successful': True}
INFO:aiocometd.client:Client opened with connection_type 'long-polling'
Subscribing to CDC channel: /data/TaskChangeEvents
WARNING:aiocometd.transports.long_polling:Failed to send payload, 0, message='Attempt to decode JSON with unexpected mimetype: '
INFO:aiocometd.client:Closing client...
DEBUG:aiocometd.transports.base:Connect task finished with: CancelledError()
INFO:aiocometd.client:Client closed.
Traceback (most recent call last):
  File "/home/nate/Desktop/ACV/Dev/sf_cdc/lib/python3.6/site-packages/aiocometd/transports/long_polling.py", line 35, in _send_final_payload
    response_payload = await response.json(loads=self._json_loads)
  File "/home/nate/Desktop/ACV/Dev/sf_cdc/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 1027, in json
    headers=self.headers)
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: '
robertmrk commented 5 years ago

In this case the problem is that you're trying to subscribe to a non-existent channel. Instead of /data/TaskChangeEvents you should subscribe to /data/TaskChangeEvent. You can find more information on CDC channel naming conventions in the docs.

After correcting this, make sure that your user profile has the required permissions to receive change events for the Task objects.

nbyrnes-acv commented 5 years ago

Thanks, this gets me past the error.