From c95c1dd702dbdd57346929a83acfd090cc888b2f Mon Sep 17 00:00:00 2001 From: Luxferre Date: Tue, 23 Dec 2025 19:03:18 +0200 Subject: [PATCH] implemented deck.http --- README.md | 16 ++++++++++++++-- deck/http.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ tdeckboot.py | 3 ++- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 deck/http.py diff --git a/README.md b/README.md index cd9fcff..1db5ad9 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,9 @@ Various hardware information. May be expanded in the future. Exports the following functions: - `board_id()`: get the board ID (e.g. `lilygo_tdeck` for T-Deck/T-Deck Plus), or `emulated` if it's not run on a CircuitPython device -- `battery_v()`: (T-Deck-specific) get the current battery voltage +- `battery_v()`: (T-Deck-specific) get the current battery voltage (or 0 if running in an emulated environment) + +Also, this module exposes the `REAL_HW` variable to detect whether the code is running on a real hardware in the CircuitPython environment. ### `deck.net` @@ -138,6 +140,16 @@ except: import requests ``` +### `deck.http` + +High-level HTTP client library. + +Exports the following functions: + +- `http_set_ua(ua_str='Mozilla/5.0')`: set a default HTTP user agent string +- `http_fetch(url, useragent=None, hdrs={})`: fetch text content from a URL into a string (returns `(status_code, content)` tuple) +- `wget(url, filename='', useragent=None, hdrs={})`: download a file into a local path (returns `(status_code, filename)` tuple) + ### `deck.llm` A helper library for interaction with remote LLM (large language model) endpoints that expose OpenAI-compatible API. As of now, it only exports a single class, `LLMChat`, to encapsulate all the interaction. @@ -254,7 +266,7 @@ You can start it with `from tdeckboot import *` to give it the same extended cap The foundational module set is going to at least include high-level capabilities for: - exposing low-level socket and HTTP APIs (done), -- high-level HTTP APIs, +- high-level HTTP APIs (partially done), - high-level LLM APIs (partially done), - date and time informaion (done), - file system manipulation (done), diff --git a/deck/http.py b/deck/http.py new file mode 100644 index 0000000..e4cf0ce --- /dev/null +++ b/deck/http.py @@ -0,0 +1,49 @@ +# High-level HTTP protocol interaction +# Created by Luxferre in 2025, released into public domain + +# init requests library in a platform-agnostic way +requests = None +try: + from deck import net + status, stext, myip = net.wifi_connect_wait() + if status: + requests = net.init_requests() +except: + import requests + +DEFAULT_UA = 'Mozilla/5.0' +def http_set_ua(ua_str='Mozilla/5.0'): + """Set a default user agent""" + global DEFAULT_UA + DEFAULT_UA = ua_str + +def http_fetch(url, useragent=None, hdrs={}): + """Fetch text content from URL""" + if useragent is None: + useragent = DEFAULT_UA + hdrs['User-Agent'] = useragent + try: + r = requests.get(url, headers=hdrs) + return (r.status_code, r.text) + except: + return (404, '') + +def wget(url, filename='', useragent=None, hdrs={}): + """Download a file""" + if filename == '': + filename = url.split('/')[-1] + if useragent is None: + useragent = DEFAULT_UA + hdrs['User-Agent'] = useragent + r = requests.get(url, stream=True, headers=hdrs) + if r.status_code >= 200 and r.status_code < 400: + with open(filename, 'wb') as f: + try: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + except AttributeError: + f.write(r.content) + else: filename = '' + return (r.status_code, filename) + + diff --git a/tdeckboot.py b/tdeckboot.py index cb7e544..d690894 100644 --- a/tdeckboot.py +++ b/tdeckboot.py @@ -8,7 +8,8 @@ from deck.fs import du, df, sep, pwd, ls, md, cd, rd, rm, mv, cp, sync 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 +from deck.hwinfo import board_id, battery_v, REAL_HW +from deck.http import wget, http_fetch, http_set_ua from app.ed import edit, view try: from deck import net