64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
import requests
|
|
import threading
|
|
import os
|
|
from .request import buildRequest
|
|
|
|
#session = requests.Session()
|
|
_thread_local = threading.local()
|
|
|
|
#def getSession():
|
|
def getSession(reset=False):
|
|
if reset and hasattr(_thread_local, "session"):
|
|
_thread_local.session.close()
|
|
del _thread_local.session
|
|
|
|
if not hasattr(_thread_local, "session"):
|
|
session = requests.Session()
|
|
session.name = threading.current_thread().name
|
|
_thread_local.session = session
|
|
|
|
return _thread_local.session
|
|
|
|
|
|
def executeRequest(action, outputs, resolved):
|
|
session = getSession()
|
|
|
|
session_log = bool(os.environ.get("LOGS_SESSION", False))
|
|
|
|
if session_log:
|
|
print(
|
|
f"Session {session.name} used by "
|
|
f"{threading.current_thread().name} thread"
|
|
)
|
|
|
|
try:
|
|
request, outputs = buildRequest(action, outputs, resolved)
|
|
|
|
response = session.request(
|
|
method=request["method"],
|
|
url=request["path"],
|
|
json=request.get("object"),
|
|
headers=request.get("headers"),
|
|
data=request.get("data"),
|
|
files=request.get("files")
|
|
)
|
|
|
|
except requests.ConnectionError as error:
|
|
print(f"Request failed due to {error} during {action.name}")
|
|
|
|
session = getSession(reset=True)
|
|
|
|
print(f"Retrying {action.name}")
|
|
|
|
response = session.request(
|
|
method=request["method"],
|
|
url=request["path"],
|
|
json=request.get("object"),
|
|
headers=request.get("headers"),
|
|
data=request.get("data")
|
|
)
|
|
|
|
return response, outputs
|
|
|
|
return response, outputs
|