From a677820d3fe2a174572b6864b28096695182477b Mon Sep 17 00:00:00 2001 From: SempaiEcchi Date: Sun, 21 Nov 2021 19:33:55 +0100 Subject: [PATCH 1/2] add aiohttp --- bpmn_types.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/bpmn_types.py b/bpmn_types.py index 21f0cab..4c31dfe 100644 --- a/bpmn_types.py +++ b/bpmn_types.py @@ -2,7 +2,11 @@ import os import env from utils.common import parse_expression +import aiohttp +from aiohttp.client import ClientSession, ClientTimeout, ContentTypeError +timeout = ClientTimeout(sock_connect=5) +client_session = ClientSession(timeout=timeout) NS = { "bpmn": "http://www.omg.org/spec/BPMN/20100524/MODEL", "camunda": "http://camunda.org/schema/1.0/bpmn", @@ -233,26 +237,32 @@ async def run_connector(self, variables, instance_id): url = os.path.join( self.connector_fields["input_variables"].get("base_url", ""), - self.connector_fields["input_variables"]["url"].lstrip("/"), + (self.connector_fields["input_variables"].get("url") or "").lstrip("/"), ) - # Check method and make request - if method := self.connector_fields["input_variables"].get("method"): + if method := self.connector_fields["input_variables"].get("method") or "GET": if method == "POST": - call_function = requests.post + call_function = client_session.post elif method == "PATCH": - call_function = requests.patch + call_function = client_session.patch else: - call_function = requests.get + call_function = client_session.get - response = call_function( + response = await call_function( url, params=parameters, - json=data, + data=data, ) + if response.status not in (200, 201): + raise Exception(response.text) - if response.status_code not in (200, 201): - raise Exception(response.text) + r = {} + try: + r = await response.json() + except Exception as e: + print("error") + if not isinstance(e, ContentTypeError): + raise e # Check for output variables if self.output_variables: From ea06dbe106f8d01a3d6c247520567ba46815f9a4 Mon Sep 17 00:00:00 2001 From: SempaiEcchi Date: Sun, 21 Nov 2021 19:36:34 +0100 Subject: [PATCH 2/2] add await --- bpmn_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bpmn_types.py b/bpmn_types.py index 4c31dfe..f38ff0b 100644 --- a/bpmn_types.py +++ b/bpmn_types.py @@ -259,6 +259,7 @@ async def run_connector(self, variables, instance_id): r = {} try: r = await response.json() + except Exception as e: print("error") if not isinstance(e, ContentTypeError): @@ -266,7 +267,6 @@ async def run_connector(self, variables, instance_id): # Check for output variables if self.output_variables: - r = response.json() for key in self.output_variables: if key in r: variables[key] = r[key]