forked from Hay1tsme/artemis
1
0
Fork 0

diva: add encoder helper methods

This commit is contained in:
Hay1tsme 2023-11-26 01:41:25 -05:00
parent dd1c7667e0
commit 4831aa5628
1 changed files with 99 additions and 2 deletions

View File

@ -1,6 +1,9 @@
from urllib import parse
from urllib.parse import quote
from datetime import datetime
from typing import Union, Dict
from typing import Union, Dict, List, Any
from ..const import DivaConstants
def lazy_http_form_parse(src: Union[str, bytes]) -> Dict[bytes, bytes]:
out = {}
@ -184,4 +187,98 @@ class SpendCreditResponse(BaseResponse):
self.vcld_pts = 0
self.lv_str = ""
self.lv_efct_id = 0
self.lv_plt_id = 0
self.lv_plt_id = 0
def encode_int(key: str, val: Union[int, None] = None) -> str:
if type(val) != int:
val = -1
return f"{key}={val}"
def encode_str(key: str, val: Union[str, None] = None, urlencode_val: bool = False) -> str:
if type(val) != str:
val = "xxx"
if urlencode_val:
val = quote(val)
return f"{key}={val}"
def encode_date(key: str, val: Union[datetime, None], urlencode_val: bool = False, fmt: str = DivaConstants.LUT_TIME_FMT) -> str:
if type(val) != datetime:
val = datetime.now().astimezone()
val = val.replace(microsecond=0)
dt_fmt = val.strftime(fmt)
if urlencode_val:
dt_fmt = quote(dt_fmt)
return f"{key}={dt_fmt}"
def encode_list_int(key: str, val: Union[List[int], None], urlencode_final_val: bool = False) -> str:
if not val:
return f"{key}="
for x in range(len(val)):
if type(val[x]) != int:
val[x] = -1
all_vals = ",".join([str(x) for x in val])
if urlencode_final_val:
all_vals = quote(all_vals)
return f"{key}={all_vals}"
def encode_list_str(key: str, val: Union[List[str], None], urlencode_final_val: bool = False, urlencode_vals: bool = False) -> str:
if not val:
return f"{key}="
for x in range(len(val)):
if type(val[x]) != str:
val[x] = "xxx"
if urlencode_vals:
val[x] = quote(val[x])
all_vals = ",".join(val)
if urlencode_final_val:
all_vals = quote(all_vals)
return f"{key}={all_vals}"
def encode_list_date(key: str, val: Union[List[datetime], None], urlencode_final_val: bool = False, urlencode_vals: bool = False, fmt: str = DivaConstants.LUT_TIME_FMT) -> str:
if not val:
return f"{key}="
for x in range(len(val)):
if type(val[x]) != datetime:
val[x] = datetime.now().astimezone()
val[x] = val[x].replace(microsecond=0).strftime(fmt)
if urlencode_vals:
val[x] = quote(val[x])
all_vals = ",".join(val)
if urlencode_final_val:
all_vals = quote(all_vals)
return f"{key}={all_vals}"
def encode_list_any(key: str, val: Union[List[Any], None], urlencode_final_val: bool = False, urlencode_vals: bool = False) -> str:
if not val:
return f"{key}="
for x in range(len(val)):
if val[x] is None:
val[x] = "x"
if urlencode_vals:
val[x] = quote(val[x])
all_vals = ",".join([str(x) for x in val])
if urlencode_final_val:
all_vals = quote(all_vals)
return f"{key}={all_vals}"