|
1 | 1 | import random |
2 | | -import requests |
3 | 2 | import re |
4 | 3 | import os |
5 | 4 | import sys |
6 | 5 | import json |
7 | 6 | import datetime |
| 7 | +import urllib.error |
| 8 | +import urllib.request |
8 | 9 | import logging as log |
9 | | -import requests |
10 | 10 | from copy import copy |
11 | 11 |
|
12 | 12 | from tom.utils import pretty, write_json |
@@ -36,15 +36,16 @@ def get(self, path): |
36 | 36 | if path in self.get_cache: |
37 | 37 | log.debug("Found in cache") |
38 | 38 | return self.get_cache[path] |
39 | | - r = requests.get(path, headers=self.headers) |
40 | | - log.debug("RESPONSE {}".format(r.status_code)) |
41 | 39 |
|
42 | | - if not (200 <= r.status_code < 300): |
43 | | - sys.exit("Non-success API response {} for '{}'".format(r.status_code, path)) |
| 40 | + req = urllib.request.Request(path, headers=self.headers) |
| 41 | + try: |
| 42 | + with urllib.request.urlopen(req) as r: |
| 43 | + data = json.load(r) |
| 44 | + log.debug("RESPONSE {}".format(r.code)) |
| 45 | + except urllib.error.HTTPError as e: |
| 46 | + sys.exit("Non-success API response {} for '{}'".format(e.code, path)) |
44 | 47 |
|
45 | | - assert r.status_code >= 200 and r.status_code < 300 |
46 | | - data = r.json() |
47 | | - log.debug(pretty(data)) |
| 48 | + log.debug(json.dumps(data)) |
48 | 49 | self.get_cache[path] = data |
49 | 50 | if ( |
50 | 51 | not isinstance(data, list) |
@@ -74,12 +75,19 @@ def post(self, path, data, check_status_code=True): |
74 | 75 | return None |
75 | 76 | self.api_log("POST {} {}".format(path, data)) |
76 | 77 | path = self.path(path) |
77 | | - r = requests.post(path, headers=self.headers, json=data) |
78 | | - log.debug("RESPONSE {}".format(r.status_code)) |
79 | | - if check_status_code: |
80 | | - assert r.status_code >= 200 and r.status_code < 300, r.text |
81 | | - data = r.json() |
82 | | - log.debug(pretty(data)) |
| 78 | + |
| 79 | + json_data = json.dumps(data).encode("utf-8") |
| 80 | + req = urllib.request.Request(path, headers=self.headers, data=json_data) |
| 81 | + try: |
| 82 | + with urllib.request.urlopen(req) as r: |
| 83 | + log.debug("RESPONSE {}".format(r.status_code)) |
| 84 | + data = json.load(r) |
| 85 | + log.debug(r.read().decode()) |
| 86 | + except urllib.error.HTTPError as e: |
| 87 | + log.error(e.headers) |
| 88 | + log.error(e.read().decode()) |
| 89 | + raise AssertionError("HTTP response {} from GitHub".format(e.code)) |
| 90 | + |
83 | 91 | return data |
84 | 92 |
|
85 | 93 | def create_pr( |
@@ -388,7 +396,7 @@ def create_prs_from_slack(self): |
388 | 396 | last_branch = self.find_last_branch_in_repo(username, repo) |
389 | 397 | log.info("last branch: " + last_branch) |
390 | 398 | # now, try to find a parent for it. |
391 | | - (parent_repo, parent_branch) = self.find_parent(username, repo, last_branch) |
| 399 | + parent_repo, parent_branch = self.find_parent(username, repo, last_branch) |
392 | 400 | log.info("parent branch: " + parent_branch) |
393 | 401 | pr_text = self.github.create_pr( |
394 | 402 | parent_repo, parent_branch, username, last_branch, last_branch + " PR", "" |
|
0 commit comments