Skip to content

Commit

Permalink
Handle POST data size for JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
laffra committed Jan 1, 2024
1 parent dce8308 commit 53f754d
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions ltk/jquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,24 @@ def error(jqXHR, textStatus, errorThrown):
return window.ajax(url, "DELETE", wrapper).fail(proxy(error))


def post(url, data, handler):
def post(url, payload, handler, kind="json"):
start = get_time()
if "?" in url:
index = url.index("?")
url = f"{url[:index]}?_=p&{url[index:]}"
payload = window.encodeURIComponent(json.dumps(data))
payload = window.encodeURIComponent(json.dumps(payload))
@callback
def success(response, *rest):
window.console.log("[Network] POST OK", f"{get_time() - start:.2f}", f"{toHuman(len(data))}/{toHuman(len(response))}", url)
return handler(window.JSON.stringify(response))
data = response if isinstance(response, str) else to_py(response)
response_size = len(response) if data is response else len(json.dumps(data))
size = f"{toHuman(len(payload))}/{toHuman(response_size)}"
window.console.log("[Network] POST OK", f"{get_time() - start:.2f}", size, url)
return handler(data)
@callback
def error(jqXHR, textStatus, errorThrown):
window.console.error("[Network] POST ERROR", f"{get_time() - start:.2f}", jqXHR.status, repr(errorThrown), url)
return handler(window.JSON.stringify({ "Error": errorThrown}))
return jQuery.post(url, payload, success, "json").fail(error)
window.ltk_post(url, payload, success, kind, error)


def async_proxy(function):
Expand Down

0 comments on commit 53f754d

Please sign in to comment.