From 83edf49e86d2db5d7a75f6c1068d3e62bda99923 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Tue, 5 Feb 2019 22:56:05 +0100 Subject: [PATCH] Switch to using requests for archives API This should fix some unicode issues --- pgcommitfest/commitfest/ajax.py | 48 +++++++++++++-------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/pgcommitfest/commitfest/ajax.py b/pgcommitfest/commitfest/ajax.py index b65de8d..3ec76ea 100644 --- a/pgcommitfest/commitfest/ajax.py +++ b/pgcommitfest/commitfest/ajax.py @@ -6,9 +6,7 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.db import transaction -import httplib -import socket -import urllib +import requests import json from pgcommitfest.auth import user_search @@ -24,36 +22,26 @@ from models import MailThreadAnnotation, PatchHistory def _archivesAPI(suburl, params=None): try: - socket.setdefaulttimeout(settings.ARCHIVES_TIMEOUT) - if settings.ARCHIVES_PORT != 443: - h = httplib.HTTPConnection(host=settings.ARCHIVES_SERVER, - port=settings.ARCHIVES_PORT, - strict=True, - timeout=settings.ARCHIVES_TIMEOUT) - else: - h = httplib.HTTPSConnection(host=settings.ARCHIVES_SERVER, - port=settings.ARCHIVES_PORT, - strict=True, - timeout=settings.ARCHIVES_TIMEOUT) - if params: - url = "%s?%s" % (suburl, urllib.urlencode(params)) - else: - url = suburl - h.request('GET', url, headers={ - 'Host': settings.ARCHIVES_HOST, - }) - resp = h.getresponse() - if resp.status != 200: - if resp.status == 404: + resp = requests.get("http{0}://{1}:{2}{3}".format(settings.ARCHIVES_PORT == 443 and 's' or '', + settings.ARCHIVES_SERVER, + settings.ARCHIVES_PORT, + suburl), + params=params, + headers={ + 'Host': settings.ARCHIVES_HOST, + }, + timeout=settings.ARCHIVES_TIMEOUT, + ) + if resp.status_code != 200: + if resp.status_code == 404: raise Http404() - raise Exception("JSON call failed: %s" % resp.status) + raise Exception("JSON call failed: %s" % resp.status_code) - r = json.load(resp) - resp.close() - h.close() - except socket.error, e: + return resp.json() + except Http404: + raise + except Exception as e: raise Http503("Failed to communicate with archives backend: %s" % e) - return r def getThreads(request): search = request.GET.has_key('s') and request.GET['s'] or None -- 2.39.5