From a852710e1982af169e502de10e8fbd45260eaacb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sun, 8 Sep 2024 19:26:29 +0200 Subject: [PATCH] Initial HTTP GET support --- example_config.json | 6 +++--- menu.py | 23 ++++++++++++++++++++++- requirements.txt | 5 +++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/example_config.json b/example_config.json index 043fd50..01f6eca 100644 --- a/example_config.json +++ b/example_config.json @@ -21,9 +21,9 @@ "action_param": "This is example 4" }, { - "label": "example 5", - "action_type": "MSG", - "action_param": "This is example 5" + "label": "GET example", + "action_type": "GET", + "action_param": "http://example.com/" } ], "bg_color": "lime" diff --git a/menu.py b/menu.py index 1dfa935..48c8d8c 100644 --- a/menu.py +++ b/menu.py @@ -12,6 +12,7 @@ from typing import Tuple, Callable, TypedDict, List import pygame import pygame.freetype +import requests class ButtonDef(TypedDict): @@ -20,7 +21,8 @@ class ButtonDef(TypedDict): class ActionType(Enum): - MSG = auto() + MSG = auto() # Shows a message for about 5 seconds + GET = auto() # Performs a HTTP GET class Action(TypedDict): @@ -155,9 +157,28 @@ class App: self.board = MessageBoard(self.get_screen_rect(), action['param'], bg_color=self.bg_color) 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]: if action['action_type'] == ActionType.MSG: return self.show_message_handler(action) + elif action['action_type'] == ActionType.GET: + return self.get_handler(action) raise NotImplementedError(action['action_type']) def loop(self): diff --git a/requirements.txt b/requirements.txt index a76295c..07b1be7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,6 @@ +certifi==2024.8.30 +charset-normalizer==3.3.2 +idna==3.8 pygame==2.6.0 +requests==2.32.3 +urllib3==2.2.2