libhv.pyi 3.3 KB

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