send-to-bot.py: use requests instead of urllib (#6184)

beep
Stig124 2021-09-03 14:10:24 +02:00 committed by GitHub
parent 65456d0941
commit f4390b1637
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 14 deletions

21
scripts/send-to-bot.py Normal file → Executable file
View File

@ -3,8 +3,7 @@
import os import os
import sys import sys
import json import requests
import urllib.request
BOT_URL = 'https://tldr-bot.starbeamrainbowlabs.com' BOT_URL = 'https://tldr-bot.starbeamrainbowlabs.com'
@ -34,20 +33,16 @@ def post_comment(pr_id, body, once):
if once: if once:
endpoint += '/once' endpoint += '/once'
headers = {'Content-Type': 'application/json'} data = {'pr_id': pr_id, 'body': body}
data = json.dumps({'pr_id': pr_id, 'body': body})
req = urllib.request.Request(endpoint, data.encode(), headers)
try: try:
resp = urllib.request.urlopen(req) with requests.post(endpoint, json=data) as r:
code = resp.getcode() if r.status_code != requests.codes.ok:
except Exception as e: print('Error: tldr-bot responded with code', r.status_code, file=sys.stderr)
print('Error sending data to tldr-bot:', str(e), file=sys.stderr) print(r.text, file=sys.stderr)
return False return False
except requests.exceptions.RequestException as e:
if code != 200: print('Error sending data to tldr-bot:', str(e), file=sys.stderr)
print('Error: tldr-bot responded with code', code, file=sys.stderr)
print(resp.read(), file=sys.stderr)
return False return False
return True return True