implemented deck.http
This commit is contained in:
@@ -105,7 +105,9 @@ Various hardware information. May be expanded in the future.
|
|||||||
Exports the following functions:
|
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
|
- `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`
|
### `deck.net`
|
||||||
|
|
||||||
@@ -138,6 +140,16 @@ except:
|
|||||||
import requests
|
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`
|
### `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.
|
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:
|
The foundational module set is going to at least include high-level capabilities for:
|
||||||
|
|
||||||
- exposing low-level socket and HTTP APIs (done),
|
- exposing low-level socket and HTTP APIs (done),
|
||||||
- high-level HTTP APIs,
|
- high-level HTTP APIs (partially done),
|
||||||
- high-level LLM APIs (partially done),
|
- high-level LLM APIs (partially done),
|
||||||
- date and time informaion (done),
|
- date and time informaion (done),
|
||||||
- file system manipulation (done),
|
- file system manipulation (done),
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
|
||||||
+2
-1
@@ -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.input import input, input_multi
|
||||||
from deck.pager import print_paged, term_size
|
from deck.pager import print_paged, term_size
|
||||||
from deck.time import *
|
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
|
from app.ed import edit, view
|
||||||
try:
|
try:
|
||||||
from deck import net
|
from deck import net
|
||||||
|
|||||||
Reference in New Issue
Block a user