From 11399dde7662e61b031142d9a602ab964ce6fd3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Thu, 5 Aug 2021 20:57:23 +0200 Subject: [PATCH] Refine abstract methods and properties (#1118) --- httpie/models.py | 11 ++++++----- httpie/output/streams.py | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/httpie/models.py b/httpie/models.py index a90b6a13..b56670f7 100644 --- a/httpie/models.py +++ b/httpie/models.py @@ -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: diff --git a/httpie/output/streams.py b/httpie/output/streams.py index 2ef908ad..4fe94acd 100644 --- a/httpie/output/streams.py +++ b/httpie/output/streams.py @@ -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`."""