File: service.py

package info (click to toggle)
qasync 0.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 336 kB
  • sloc: python: 1,849; makefile: 10
file content (44 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import httpx

from PySide6.QtCore import QObject, Signal, Property, Slot
from qasync import asyncSlot


class ExampleService(QObject):
    valueChanged = Signal(str, arguments=["value"])
    loadingChanged = Signal(bool, arguments=["loading"])

    def __init__(self, parent=None):
        QObject.__init__(self, parent)

        self._value = None
        self._loading = False

    def _set_value(self, value):
        if self._value != value:
            self._value = value
            self.valueChanged.emit(value)

    def _set_loading(self, value):
        if self._loading != value:
            self._loading = value
            self.loadingChanged.emit(value)

    @Property(str, notify=valueChanged)
    def value(self) -> str:
        return self._value

    @Property(bool, notify=loadingChanged)
    def isLoading(self) -> bool:
        return self._loading

    @asyncSlot(str)
    async def fetch(self, endpoint: str):
        if not endpoint:
            return

        self._set_loading(True)
        async with httpx.AsyncClient() as client:
            resp = await client.get(endpoint)
            self._set_value(resp.text)
            self._set_loading(False)