Skip to content

Commit

Permalink
send-to-bot.py: use requests instead of urllib (#6184)
Browse files Browse the repository at this point in the history
  • Loading branch information
signed-log authored Sep 3, 2021
1 parent 65456d0 commit f4390b1
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions scripts/send-to-bot.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import os
import sys
import json
import urllib.request
import requests

BOT_URL = 'https://tldr-bot.starbeamrainbowlabs.com'

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f4390b1

Please sign in to comment.