libhv.pyi 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from typing import Literal, Generator, Callable
  2. WsMessageType = Literal['onopen', 'onclose', 'onmessage']
  3. WsChannelId = int
  4. HttpStatusCode = int
  5. HttpHeaders = dict[str, str]
  6. ErrorCode = int
  7. class Future[T]:
  8. @property
  9. def completed(self) -> bool: ...
  10. def cancel(self) -> None: ...
  11. def __iter__(self) -> Generator[T, None, None]: ...
  12. class HttpResponse(Future['HttpResponse']):
  13. @property
  14. def status_code(self) -> int: ...
  15. @property
  16. def headers(self) -> dict[str, str]: ...
  17. @property
  18. def text(self) -> str: ...
  19. @property
  20. def content(self) -> bytes: ...
  21. def json(self): ...
  22. class HttpClient:
  23. def get(self, url: str, /, params=None, headers=None, timeout=10) -> HttpResponse: ...
  24. def post(self, url: str, /, params=None, headers=None, data=None, json=None, timeout=10) -> HttpResponse: ...
  25. def put(self, url: str, /, params=None, headers=None, data=None, json=None, timeout=10) -> HttpResponse: ...
  26. def delete(self, url: str, /, params=None, headers=None, timeout=10) -> HttpResponse: ...
  27. class HttpRequest:
  28. @property
  29. def method(self) -> Literal['GET', 'POST', 'PUT', 'DELETE']: ...
  30. @property
  31. def path(self) -> str: ...
  32. @property
  33. def url(self) -> str: ...
  34. @property
  35. def headers(self) -> HttpHeaders: ...
  36. @property
  37. def data(self) -> str | bytes: ...
  38. class HttpServer:
  39. def __init__(self, host: str, port: int, /) -> None: ...
  40. def start(self) -> ErrorCode: ...
  41. def stop(self) -> ErrorCode: ...
  42. def dispatch[T](self, fn: Callable[
  43. [HttpRequest],
  44. T | tuple[T, HttpStatusCode] | tuple[T, HttpStatusCode, HttpHeaders]
  45. ], /) -> bool:
  46. """Dispatch one HTTP request through `fn`. `fn` should return one of the following:
  47. + object
  48. + (object, status_code)
  49. + (object, status_code, headers)
  50. Return `True` if dispatched, otherwise `False`.
  51. """
  52. def ws_set_ping_interval(self, milliseconds: int, /) -> None:
  53. """Set WebSocket ping interval in milliseconds."""
  54. def ws_close(self, channel: WsChannelId, /) -> ErrorCode:
  55. """Close WebSocket channel."""
  56. def ws_send(self, channel: WsChannelId, data: str, /) -> int:
  57. """Send WebSocket message through `channel`."""
  58. def ws_recv(self) -> tuple[
  59. WsMessageType,
  60. tuple[WsChannelId, HttpRequest] | WsChannelId | tuple[WsChannelId, str]
  61. ] | None:
  62. """Receive one WebSocket message.
  63. Return one of the following or `None` if nothing to receive.
  64. + `"onopen"`: (channel, request)
  65. + `"onclose"`: channel
  66. + `"onmessage"`: (channel, body)
  67. """
  68. class WebSocketClient:
  69. def open(self, url: str, headers=None, /) -> ErrorCode: ...
  70. def close(self) -> ErrorCode: ...
  71. def send(self, data: str, /) -> int:
  72. """Send WebSocket message."""
  73. def recv(self) -> tuple[WsMessageType, str | None] | None:
  74. """Receive one WebSocket message.
  75. Return one of the following or `None` if nothing to receive.
  76. + `"onopen"`: `None`
  77. + `"onclose"`: `None`
  78. + `"onmessage"`: body
  79. """
  80. def strerror(errno: ErrorCode, /) -> str:
  81. """Get error message by errno via `hv_strerror`."""