Skip to content

Commit

Permalink
Add headers to ltk.get and ltk.post
Browse files Browse the repository at this point in the history
  • Loading branch information
laffra committed Nov 24, 2024
1 parent 67f2437 commit dacee24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
8 changes: 4 additions & 4 deletions ltk/jquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def cancel(key):
del timers[key]


def get(url, handler, kind="json"):
def get(url, handler, kind="json", headers=None):
"""
Performs an asynchronous GET request to the given URL.
The handler function is called with the response data.
Expand All @@ -196,7 +196,7 @@ def error(request, text_status, error_thrown):
request.status, repr(text_status), url
)
return handler(f'{{"Network error for {url}": "{text_status}"}}')
window.ltk_get(url, success, kind, error)
window.ltk_get(url, success, kind, error, headers)


def delete(url, handler):
Expand All @@ -216,7 +216,7 @@ def error(request, text_status, error_thrown):
return window.ltk_delete(url, success, error)


def post(url, payload, handler, kind="json"):
def post(url, payload, handler, kind="json", headers=None):
"""
Performs an asynchronous POST request to the given URL, passing the given payload.
The handler function is called with the response data.
Expand All @@ -237,7 +237,7 @@ def error(request, text_status, error_thrown):
request.status, text_status, repr(error_thrown), url
)
return handler(f'{{"Error": "{error_thrown}"}}')
window.ltk_post(url, payload, success, kind, error)
window.ltk_post(url, payload, success, kind, error, headers)


def async_proxy(function):
Expand Down
16 changes: 12 additions & 4 deletions ltk/ltk.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@
tableCell(table, column, row).text(value)
}

window.ltk_get = (url, success, kind, error) => {
$.get(url, success, kind).fail(error)
window.ltk_get = (url, success, dataType, error, headers) => {
if (headers) {
$.ajax({ url, dataType, headers }).done(success).fail(error)
} else {
$.get(url, success, dataType).fail(error)
}
}

window.ltk_post = (url, payload, success, kind, error) => {
$.post(url, payload, success, kind).fail(error)
window.ltk_post = (url, data, success, dataType, error, headers) => {
if (headers) {
$.ajax({ url, type: "POST", data, dataType, headers }).done(success).fail(error)
} else {
$.post(url, payload, success, dataType).fail(error)
}
}

window.ltk_delete = (url, success, error) => {
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyscript-ltk"
version = "0.2.5"
version = "0.2.6"
description = "A little toolkit for writing UIs in PyScript"
readme = "README.md"
authors = [{ name = "Chris Laffra", email = "chris@chrislaffra.com" }]
Expand Down

0 comments on commit dacee24

Please sign in to comment.