Skip to content

Commit

Permalink
Properly handle the multipart request
Browse files Browse the repository at this point in the history
  • Loading branch information
JBassett authored Oct 8, 2019
1 parent c4260ae commit e99a7a5
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions custom_components/plex_webhooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ async def handle_webhook(hass, webhook_id, request):
data = {}

try:
body = await request.text()
# This is bad, but I don't see a better way to extract just the json...
raw_json = body[body.index('{'):body.rindex('}')+1]
data = json.loads(raw_json) if body else {}
reader = await request.multipart()
# https://docs.aiohttp.org/en/stable/multipart.html#aiohttp-multipart
while True:
part = await reader.next()
if part is None:
break
if part.name == 'payload':
data = await part.json()
except ValueError:
logging.warn('Issue decoding webhook: ' + raw_json)
return None
except:
logging.debug('Ignoring webhook, must be a photo?')
logging.warn('Issue decoding webhook: ' + part.text())
return None

data['playerUuid'] = data['Player']['uuid']
Expand Down

0 comments on commit e99a7a5

Please sign in to comment.