From c8e908d07b819a86ea41cd67deac7537c32c2672 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Thu, 25 Dec 2025 15:51:04 +0200 Subject: [PATCH] added http auth and upload functions --- README.md | 2 +- deck/http.py | 7 ++++--- tdeckboot.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 72e7e1e..baa29e5 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ Exports the following functions: - `http_fetch(url, useragent=None, hdrs={})`: fetch text content from a URL into a string (returns `(status_code, content)` tuple) - `auth_header(username, password)`: get an dictionary fragment with the corresponding HTTP Basic Auth header according to the username and password - `wget(url, filename='', useragent=None, hdrs={})`: download a file into a local path (returns `(status_code, filename)` tuple) -- `wput(url, file_path, field_name="file", method="POST", format="x-www-form-urlencoded", headers=None)`: upload a file from a local path in the `x-www-form-urlencoded` or JSON format (returns `(status_code, content)` tuple) +- `wput(url, file_path, field_name="file", method="POST", format="x-www-form-urlencoded", headers={}, useragent=None)`: upload a file from a local path in the `x-www-form-urlencoded` or JSON format (returns `(status_code, content)` tuple) ### `deck.llm` diff --git a/deck/http.py b/deck/http.py index 47cd583..45e980c 100644 --- a/deck/http.py +++ b/deck/http.py @@ -54,10 +54,11 @@ def wget(url, filename='', useragent=None, hdrs={}): else: filename = '' return (r.status_code, filename) -def wput(url, file_path, field_name="file", method="POST", format="x-www-form-urlencoded", headers=None): +def wput(url, file_path, field_name="file", method="POST", format="x-www-form-urlencoded", headers={}, useragent=None): """Upload a file""" - if headers is None: - headers = {} + if useragent is None: + useragent = DEFAULT_UA + headers['User-Agent'] = useragent method = method.upper() try: with open(file_path, "rb") as f: diff --git a/tdeckboot.py b/tdeckboot.py index e0dfa6d..2840ad8 100644 --- a/tdeckboot.py +++ b/tdeckboot.py @@ -9,7 +9,7 @@ from deck.input import input, input_multi from deck.pager import print_paged, term_size from deck.time import * from deck.hwinfo import board_id, battery_v, cpu_f, REAL_HW -from deck.http import wget, http_fetch, http_set_ua +from deck.http import wget, wput, http_fetch, http_set_ua, auth_header from deck import xlat from app.ed import edit, view try: