From f4390b16370f832ed2ac4df032be0bc98aa8320a Mon Sep 17 00:00:00 2001 From: Stig124 Date: Fri, 3 Sep 2021 14:10:24 +0200 Subject: [PATCH] send-to-bot.py: use requests instead of urllib (#6184) --- scripts/send-to-bot.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) mode change 100644 => 100755 scripts/send-to-bot.py diff --git a/scripts/send-to-bot.py b/scripts/send-to-bot.py old mode 100644 new mode 100755 index 785f0dccc..91fc3dd44 --- a/scripts/send-to-bot.py +++ b/scripts/send-to-bot.py @@ -3,8 +3,7 @@ import os import sys -import json -import urllib.request +import requests BOT_URL = 'https://tldr-bot.starbeamrainbowlabs.com' @@ -34,22 +33,18 @@ def post_comment(pr_id, body, once): if once: endpoint += '/once' - headers = {'Content-Type': 'application/json'} - data = json.dumps({'pr_id': pr_id, 'body': body}) - req = urllib.request.Request(endpoint, data.encode(), headers) + data = {'pr_id': pr_id, 'body': body} try: - resp = urllib.request.urlopen(req) - code = resp.getcode() - except Exception as e: + with requests.post(endpoint, json=data) as r: + if r.status_code != requests.codes.ok: + print('Error: tldr-bot responded with code', r.status_code, file=sys.stderr) + print(r.text, file=sys.stderr) + return False + except requests.exceptions.RequestException as e: print('Error sending data to tldr-bot:', str(e), file=sys.stderr) return False - - if code != 200: - print('Error: tldr-bot responded with code', code, file=sys.stderr) - print(resp.read(), file=sys.stderr) - return False - + return True def main(action):