Refine abstract methods and properties (#1118)

This commit is contained in:
Mickaël Schoentgen 2021-08-05 20:57:23 +02:00 committed by GitHub
parent da47e37c44
commit 11399dde76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View File

@ -1,32 +1,33 @@
from abc import ABCMeta, abstractmethod
from typing import Iterable, Optional
from urllib.parse import urlsplit
from .utils import split_cookies
class HTTPMessage:
class HTTPMessage(metaclass=ABCMeta):
"""Abstract class for HTTP messages."""
def __init__(self, orig):
self._orig = orig
@abstractmethod
def iter_body(self, chunk_size: int) -> Iterable[bytes]:
"""Return an iterator over the body."""
raise NotImplementedError()
@abstractmethod
def iter_lines(self, chunk_size: int) -> Iterable[bytes]:
"""Return an iterator over the body yielding (`line`, `line_feed`)."""
raise NotImplementedError()
@property
@abstractmethod
def headers(self) -> str:
"""Return a `str` with the message's headers."""
raise NotImplementedError()
@property
@abstractmethod
def encoding(self) -> Optional[str]:
"""Return a `str` with the message's encoding, if known."""
raise NotImplementedError()
@property
def body(self) -> bytes:

View File

@ -1,3 +1,4 @@
from abc import ABCMeta, abstractmethod
from itertools import chain
from typing import Callable, Iterable, Union
@ -24,7 +25,7 @@ class BinarySuppressedError(DataSuppressedError):
message = BINARY_SUPPRESSED_NOTICE
class BaseStream:
class BaseStream(metaclass=ABCMeta):
"""Base HTTP message output stream class."""
def __init__(
@ -50,9 +51,9 @@ class BaseStream:
"""Return the headers' bytes."""
return self.msg.headers.encode('utf8')
@abstractmethod
def iter_body(self) -> Iterable[bytes]:
"""Return an iterator over the message body."""
raise NotImplementedError()
def __iter__(self) -> Iterable[bytes]:
"""Return an iterator over `self.msg`."""