-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
36 lines (27 loc) · 933 Bytes
/
service.py
File metadata and controls
36 lines (27 loc) · 933 Bytes
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
from abc import ABC, abstractmethod
from dataclasses import dataclass
import json
from typing import Any, TypeAlias
from httpx import AsyncClient, post # type: ignore
_Data: TypeAlias = dict[str, Any]
@dataclass
class Service(ABC):
url: str
def process_data(self, data: _Data) -> str:
return json.dumps(data)
async def async_post(
self, client: AsyncClient, /, *, path: str, data: _Data
) -> Any:
response = await client.post(
url=f"{self.url}{path}", data=self.process_data(data), timeout=10
)
return response.json()
def post(self, /, *, path: str, data: _Data) -> Any:
response = post(
url=f"{self.url}{path}", data=self.process_data(data), timeout=10
)
return response.json()
@classmethod
@abstractmethod
def from_config(cls, path: str = "config.ini") -> "Service":
raise NotImplementedError