Skip to content

Commit

Permalink
updating get_messages method
Browse files Browse the repository at this point in the history
using requests sessions instead of sseclient
  • Loading branch information
workalitkp authored May 28, 2021
1 parent c7d8009 commit b037b25
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests
import sseclient
import json
import os
from time import sleep
Expand Down Expand Up @@ -39,25 +38,16 @@ def get_messages(self):

while True:
try:
response = requests.get(url, stream=True)
if 'Content-Type' in response.headers:
client = sseclient.SSEClient(response)

print('connected successfully\n')

for event in client.events():
try:
message_event = json.loads(event.data)
yield message_event
except Exception as e:
print(e.args[0])
continue
else:
print('Invalid bot token OR Invalid connection response from server')

print('retry to connect after 10 seconds...')
sleep(self.RETRY_DELAY)

session = requests.Session()
with session.get(url, headers=None, stream=True) as resp:
for line in resp.iter_lines():
if line:
try:
message_event = json.loads(line.decode("utf-8")[5::])["body"]
yield message_event
except Exception as e:
print(e.args[0])
continue
except Exception as e:
print(e.args[0])
print('retry to connect after 10 seconds...')
Expand Down

0 comments on commit b037b25

Please sign in to comment.