Move repeated code for ClickHouseHelper to _insert_post

This commit is contained in:
Mikhail f. Shiryaev 2023-08-09 23:46:45 +02:00
parent c7b045fdde
commit ce30b93b34
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -47,45 +47,9 @@ class ClickHouseHelper:
params[k] = v
with open(file, "rb") as data_fd:
for i in range(5):
try:
response = requests.post(
url, params=params, data=data_fd, headers=auth
)
except Exception as e:
error = f"Received exception while sending data to {url} on {i} attempt: {e}"
logging.warning(error)
continue
logging.info("Response content '%s'", response.content)
if response.ok:
break
error = (
"Cannot insert data into clickhouse at try "
+ str(i)
+ ": HTTP code "
+ str(response.status_code)
+ ": '"
+ str(response.text)
+ "'"
)
if response.status_code >= 500:
# A retriable error
time.sleep(1)
continue
logging.info(
"Request headers '%s', body '%s'",
response.request.headers,
response.request.body,
)
raise InsertException(error)
else:
raise InsertException(error)
ClickHouseHelper._insert_post(
url, params=params, data=data_fd, headers=auth
)
@staticmethod
def insert_json_str(url, auth, db, table, json_str):
@ -95,12 +59,18 @@ class ClickHouseHelper:
"date_time_input_format": "best_effort",
"send_logs_level": "warning",
}
ClickHouseHelper._insert_post(url, params=params, data=json_str, headers=auth)
@staticmethod
def _insert_post(*args, **kwargs):
url = ""
if args:
url = args[0]
url = kwargs.get("url", url)
for i in range(5):
try:
response = requests.post(
url, params=params, data=json_str, headers=auth
)
response = requests.post(*args, **kwargs)
except Exception as e:
error = f"Received exception while sending data to {url} on {i} attempt: {e}"
logging.warning(error)
@ -112,13 +82,8 @@ class ClickHouseHelper:
break
error = (
"Cannot insert data into clickhouse at try "
+ str(i)
+ ": HTTP code "
+ str(response.status_code)
+ ": '"
+ str(response.text)
+ "'"
f"Cannot insert data into clickhouse at try {i}: HTTP code "
f"{response.status_code}: '{response.text}'"
)
if response.status_code >= 500: