robertmrk / aiosfstream

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

No Message creation date found #5

Closed avinkris closed 5 years ago

avinkris commented 5 years ago

Hi Robert,

I am new to Python and APIs, after lot of struggle I managed to find your package and written this

` import asyncio

from aiosfstream import SalesforceStreamingClient

async def stream_events():

connect to Streaming API

async with SalesforceStreamingClient(
        consumer_key="XXX",
        consumer_secret="YYY",
        sandbox= True,
        username="hello@world.com",
        password="ABCDEFG") as client:

    # subscribe to topics
    await client.subscribe("/data/ChangeEvents")

    # listen for incoming messages
    async for message in client:
        print(message)

      #  topic = message["channel"]
      #  data = message["data"]
      #  print(f"{topic}: {data}")

if name == "main": loop = asyncio.get_event_loop() loop.run_until_complete(stream_events()) `

Sorry I am unable to get this code block working for the above code.

After making a change in Salesforce I am getting the below error.

Traceback (most recent call last): File "C:/Users/avellala/PycharmProjects/untitled2/SRC/cdcmain.py", line 30, in loop.run_until_complete(stream_events()) File "C:\Program Files (x86)\Python37-32\lib\asyncio\base_events.py", line 584, in run_until_complete return future.result() File "C:/Users/avellala/PycharmProjects/untitled2/SRC/cdcmain.py", line 20, in stream_events async for message in client: File "C:\Program Files (x86)\Python37-32\lib\site-packages\aiosfstream\client.py", line 240, in aiter async for message in super().aiter(): File "C:\Program Files (x86)\Python37-32\lib\site-packages\aiocometd\client.py", line 404, in aiter yield await self.receive() File "C:\Program Files (x86)\Python37-32\lib\site-packages\aiosfstream\exceptions.py", line 143, in async_wrapper return await func(*args, **kwargs) File "C:\Program Files (x86)\Python37-32\lib\site-packages\aiosfstream\client.py", line 224, in receive await self.replay_storage.extract_replay_id(response) File "C:\Program Files (x86)\Python37-32\lib\site-packages\aiosfstream\replay.py", line 111, in extract_replay_id marker = ReplayMarker(date=self.get_message_date(message), File "C:\Program Files (x86)\Python37-32\lib\site-packages\aiosfstream\replay.py", line 97, in get_message_date raise ReplayError("No message creation date found.") aiosfstream.exceptions.ReplayError: No message creation date found.

robertmrk commented 5 years ago

Hi @avinkris

I see that in the code example you provided, you were trying to hide all your authentication credentials. Which is fine, but I suspect that you've also changed the name of the channel you're subscribing to. I would need to know the exact name of the channel. You can use the library to subscribe to PushTopics (the channel names start with the /topic/ prefix), to subscribe to Generic Streaming Channels (the channel names start with the /u/ prefix) or to subscribe to Platform Events (the channel names start with the /event/ prefix). As far as I know, there is no entity on the Salesforce platform for which the name of the channel would start with a /data/ prefix. So are you trying to stream events from PushTopcis or from Generic Streaming Channels or Platform Events?

You mentioned that after you made a change on Salesforce the code example stopped working. What did you change exactly?

avinkris commented 5 years ago

Hi @robertmrk , I am trying to subscribe to Change Data Capture events in Salesforce. The change I made was to a record that belongs to the Case object. I haven't changed the channel, I used it as per this documentation https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/code_sample_java_add_source.htm please scroll down to the channel parameter.

robertmrk commented 5 years ago

Oh, I see. This is a totally new addition to the platform, it's was introduced in v44.0 These types of events are not yet supported by the library.

I'll make the necessary changes to be compatible with them in the next few days.

robertmrk commented 5 years ago

Hi @avinkris

I implemented the necessary changes in a feature branch. You can install it by running the following command:

pip install -e git+https://github.com/robertmrk/aiosfstream.git@feature/change-data-capture-events#egg=aiosfstream

Can you confirm that your application works correctly if you install aiosfstream with the above command?

avinkris commented 5 years ago

Hi @robertmrk ,

It is working now, I have received the payload

{'data': {'schema': 'vgsSoaoxU_cvltYFCwvDdg', 'payload': {'LastModifiedDate': '2019-03-07T20:28:26.000Z', 'Status': 'New', 'ChangeEventHeader': {'isTransactionEnd': True, 'commitNumber': 140727112603, 'commitUser': '005200000023W9BAAE', 'sequenceNumber': 1, 'entityName': 'Case', 'changeType': 'UPDATE', 'changeOrigin': '', 'transactionKey': '001eaeee-db03-9e4a-03ab-003e02aa68ea', 'commitTimestamp': 1551990501000, 'recordIds': ['5001000000qHA4NAAW']}, 'SEL_Status__c': 'New'}, 'event': {'replayId': 19605}}, 'channel': '/data/ChangeEvents'}

Thank you very much for the prompt response, great work! :)

robertmrk commented 5 years ago

You're welcome! The fix will be included in the next release.