From ed6eaf0d3b9da51b7c1b3f44e25776a2016fd5c1 Mon Sep 17 00:00:00 2001 From: "robin.hruska@teskalabs.com" Date: Mon, 9 Jan 2023 11:05:57 +0100 Subject: [PATCH 1/2] Webhook: Replace requests with http.client --- asab/storage/upsertor.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/asab/storage/upsertor.py b/asab/storage/upsertor.py index 25044fe8b..e1e0ae3e5 100644 --- a/asab/storage/upsertor.py +++ b/asab/storage/upsertor.py @@ -1,11 +1,12 @@ import abc import json +import urllib.parse import uuid import hashlib import datetime import logging import asab.web.rest.json -import requests +import http.client import typing # @@ -153,22 +154,28 @@ async def webhook(self, data: dict): def _webhook(self, data, uri, auth=None): + u = urllib.parse.urlparse(uri) + if u.scheme == "https": + conn = http.client.HTTPSConnection(u.netloc) + else: + conn = http.client.HTTPConnection(u.netloc) try: - with requests.Session() as session: - if self.Storage.WebhookAuth: - session.headers["Authorization"] = self.Storage.WebhookAuth - with session.put( - uri, - data=data, - headers={"Content-Type": "application/json"} - ) as response: - if response.status_code // 100 != 2: - text = response.text - L.error( - "Webhook endpoint responded with {}:\n{}".format(response.status_code, text), - struct_data={"uri": uri}) - return response.json() + conn.request( + "PUT", uri, data, + {"Authorization": auth, "Content-Type": "application/json"} + ) + response = conn.getresponse() + if response.status // 100 != 2: + text = response.read() + L.error("Webhook endpoint responded with {}:\n{}".format(response.status, text)) + return + self.WebhookResponseData = json.load(response) + except ConnectionRefusedError: + L.error("Webhook call failed: Connection refused.", struct_data={"uri": uri}) + return except json.decoder.JSONDecodeError as e: L.error("Failed to decode JSON response from webhook: {}".format(str(e))) except Exception as e: L.error("Webhook call failed with {}: {}".format(type(e).__name__, str(e))) + finally: + conn.close() From 726a8c71d4533f9ecddae34baea19ebc91b434fe Mon Sep 17 00:00:00 2001 From: "robin.hruska@teskalabs.com" Date: Mon, 9 Jan 2023 11:08:07 +0100 Subject: [PATCH 2/2] Webhook: Log uri --- asab/storage/upsertor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/asab/storage/upsertor.py b/asab/storage/upsertor.py index e1e0ae3e5..301056997 100644 --- a/asab/storage/upsertor.py +++ b/asab/storage/upsertor.py @@ -167,15 +167,17 @@ def _webhook(self, data, uri, auth=None): response = conn.getresponse() if response.status // 100 != 2: text = response.read() - L.error("Webhook endpoint responded with {}:\n{}".format(response.status, text)) + L.error( + "Webhook endpoint responded with {}:\n{}".format(response.status, text), + struct_data={"uri": uri}) return self.WebhookResponseData = json.load(response) except ConnectionRefusedError: L.error("Webhook call failed: Connection refused.", struct_data={"uri": uri}) return except json.decoder.JSONDecodeError as e: - L.error("Failed to decode JSON response from webhook: {}".format(str(e))) + L.error("Failed to decode JSON response from webhook: {}".format(str(e)), struct_data={"uri": uri}) except Exception as e: - L.error("Webhook call failed with {}: {}".format(type(e).__name__, str(e))) + L.error("Webhook call failed with {}: {}".format(type(e).__name__, str(e)), struct_data={"uri": uri}) finally: conn.close()