1
0
Fork 0

Initial HTTP GET support

This commit is contained in:
Michał Rudowicz 2024-09-08 19:26:29 +02:00
parent c704b137a4
commit a852710e19
3 changed files with 30 additions and 4 deletions

View File

@ -21,9 +21,9 @@
"action_param": "This is example 4" "action_param": "This is example 4"
}, },
{ {
"label": "example 5", "label": "GET example",
"action_type": "MSG", "action_type": "GET",
"action_param": "This is example 5" "action_param": "http://example.com/"
} }
], ],
"bg_color": "lime" "bg_color": "lime"

23
menu.py
View File

@ -12,6 +12,7 @@ from typing import Tuple, Callable, TypedDict, List
import pygame import pygame
import pygame.freetype import pygame.freetype
import requests
class ButtonDef(TypedDict): class ButtonDef(TypedDict):
@ -20,7 +21,8 @@ class ButtonDef(TypedDict):
class ActionType(Enum): class ActionType(Enum):
MSG = auto() MSG = auto() # Shows a message for about 5 seconds
GET = auto() # Performs a HTTP GET
class Action(TypedDict): class Action(TypedDict):
@ -155,9 +157,28 @@ class App:
self.board = MessageBoard(self.get_screen_rect(), action['param'], bg_color=self.bg_color) self.board = MessageBoard(self.get_screen_rect(), action['param'], bg_color=self.bg_color)
return impl return impl
def get_handler(self, action: Action) -> Callable[[], None]:
def impl():
previous_board = self.board
def thr_fun():
def end_thr():
self.board = previous_board
ret = requests.get(action['param'])
print(f"GET {action['param']}: {ret}")
self.task_q.put(end_thr)
process_thr = Thread(target=thr_fun)
process_thr.start()
self.board = MessageBoard(self.get_screen_rect(), f"GETting {action['param']}", bg_color=self.bg_color)
return impl
def button_press_handler(self, action: Action) -> Callable[[], None]: def button_press_handler(self, action: Action) -> Callable[[], None]:
if action['action_type'] == ActionType.MSG: if action['action_type'] == ActionType.MSG:
return self.show_message_handler(action) return self.show_message_handler(action)
elif action['action_type'] == ActionType.GET:
return self.get_handler(action)
raise NotImplementedError(action['action_type']) raise NotImplementedError(action['action_type'])
def loop(self): def loop(self):

View File

@ -1 +1,6 @@
certifi==2024.8.30
charset-normalizer==3.3.2
idna==3.8
pygame==2.6.0 pygame==2.6.0
requests==2.32.3
urllib3==2.2.2