-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
58 lines (45 loc) · 1.76 KB
/
model.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import dataclasses, enum, pathlib, typing, re
class HttpMethod(str, enum.Enum):
ANY = "*"
GET = "GET"
PUT = "PUT"
POST = "POST"
PATCH = "PATCH"
DELETE = "DELETE"
TRACE = "TRACE"
class HttpScheme(str, enum.Enum):
ANY = "*"
FTP = "ftp"
HTTP = "http"
HTTPS = "https"
@dataclasses.dataclass
class HttpRequest:
uri: typing.Union[pathlib.PurePath, str] = dataclasses.field(default="/")
path: typing.Union[pathlib.PurePath, str] = dataclasses.field(default="/")
query: typing.Optional[typing.Union[typing.Mapping, str]] = None
scheme: typing.Union[HttpScheme, str] = HttpScheme.ANY
method: typing.Union[HttpMethod, str] = HttpMethod.ANY
remote: typing.Tuple[str, str] = ("", "")
server: typing.Tuple[str, str] = ("", "")
length: int = 0
type: typing.Optional[str] = None
root: typing.Optional[str] = None
host: typing.Optional[str] = None
agent: typing.Optional[str] = None
head: typing.Optional[typing.Mapping[str, typing.Union[str, typing.List[str]]]] = None
body: typing.Optional[typing.Union[str, bytes]] = None
@dataclasses.dataclass
class HttpResponse:
status: int
headers: typing.Mapping[str, typing.Union[str, typing.Iterable[str]]]
body: typing.Optional[typing.Union[str, bytes]]
class HttpCall(typing.Protocol):
def __call__(self, http: HttpRequest, match: re.Match, *args, **kwargs) -> typing.Union[HttpResponse, str, dict]:
return HttpResponse(0, dict(), None)
def flatten(paths: typing.Union[pathlib.PurePath, typing.Iterable[typing.Union[pathlib.PurePath, str]], str]) -> pathlib.PurePath:
if isinstance(paths, (pathlib.Path, pathlib.PurePath, str)):
return pathlib.PurePath(paths)
if isinstance(paths, typing.Iterable):
return pathlib.PurePath(*paths)
return pathlib.PurePath("/")