Skip to content

Commit

Permalink
Reduce complexity and extract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Seichter committed Jul 27, 2024
1 parent 76e9a88 commit b41da61
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/validate_bzst.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,20 @@ def start_validation(payload, iscli=True):

try:
resp = http.request("GET", URL, fields=bzstmap)
dom = minidom.parseString(resp.data)
params = dom.childNodes

rc = {}
for param in params:
arrays = param.getElementsByTagName("array")
iskey = True
for array in arrays:
values = array.getElementsByTagName("value")
for value in values:
strings = value.getElementsByTagName("string")
if iskey:
iskey = False
for string in strings:
newkey = gettext(string.childNodes)
else:
iskey = True
for string in strings:
newvalue = gettext(string.childNodes)
rc[newkey] = newvalue
rc = parse_response(resp.data)

validationresult = {
"key1": payload["key1"],
"key2": payload["key2"],
"ownvat": payload["ownvat"],
"foreignvat": payload["foreignvat"],
"type": "BZST",
"valid": rc["ErrorCode"] in ["200", "216"],
"valid": is_valid(rc["ErrorCode"]),
"errorcode": rc["ErrorCode"],
"errorcode_description": load_codes(payload["lang"], rc["ErrorCode"]),
"valid_from": rc["Gueltig_ab"],
"valid_to": rc["Gueltig_bis"],
"timestamp": datetime.datetime.now(datetime.timezone.utc).strftime(
"%Y-%m-%dT%H:%M:%S"
),
"timestamp": get_current_timestamp(),
"company": rc["Firmenname"],
"address": "",
"town": rc["Ort"],
Expand All @@ -139,6 +118,38 @@ def start_validation(payload, iscli=True):
return {"vatError": "VAT3500", "vatErrorMessage": repr(e)}


def parse_response(response_data):
dom = minidom.parseString(response_data)
params = dom.childNodes

rc = {}
for param in params:
arrays = param.getElementsByTagName("array")
iskey = True
for array in arrays:
values = array.getElementsByTagName("value")
for value in values:
strings = value.getElementsByTagName("string")
if iskey:
iskey = False
for string in strings:
newkey = gettext(string.childNodes)
else:
iskey = True
for string in strings:
newvalue = gettext(string.childNodes)
rc[newkey] = newvalue
return rc


def is_valid(error_code):
return error_code in ["200", "216"]


def get_current_timestamp():
return datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")


def substitute_variables_in_description(payload):
description = payload["errorcode_description"]

Expand Down

0 comments on commit b41da61

Please sign in to comment.