Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add more type hints to HTTP client. #8812

Merged
merged 8 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ async def get_file(

try:
length = await make_deferred_yieldable(
_readBodyToFile(response, output_stream, max_size)
readBodyToFile(response, output_stream, max_size)
)
except SynapseError:
# This can happen e.g. because the body is too large.
Expand Down Expand Up @@ -730,7 +730,7 @@ def _timeout_to_request_timed_out_error(f: Failure):
return f


class _ReadBodyToFileProtocol(protocol.Protocol):
class readBodyToFileProtocol(protocol.Protocol):
clokep marked this conversation as resolved.
Show resolved Hide resolved
def __init__(
self, stream: BinaryIO, deferred: defer.Deferred, max_size: Optional[int]
):
Expand Down Expand Up @@ -764,15 +764,36 @@ def connectionLost(self, reason: Failure) -> None:
self.deferred.errback(reason)


def _readBodyToFile(
def readBodyToFile(
response: IResponse, stream: BinaryIO, max_size: Optional[int]
) -> defer.Deferred:
"""
Read a HTTP response body to a file-object. Optionally enforcing a maximum file size.

Args:
response: The HTTP response to read from.
stream: The file-object to write to.
max_size: The maximum file size to allow.

Returns:
A Deferred which resolves to the length of the read body.
"""

d = defer.Deferred()
response.deliverBody(_ReadBodyToFileProtocol(stream, d, max_size))
response.deliverBody(readBodyToFileProtocol(stream, d, max_size))
return d


def encode_query_args(args: Optional[Mapping[str, Union[str, List[str]]]]) -> bytes:
"""
Encodes a map of query arguments to bytes which can be appended to a URL.

Args:
args: The query arguments, a mapping of string to string or list of strings.

Returns:
The query arguments encoded as bytes.
"""
if args is None:
return b""

Expand Down
4 changes: 2 additions & 2 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
from synapse.http.client import (
BlacklistingAgentWrapper,
IPBlacklistingResolver,
_readBodyToFile,
encode_query_args,
readBodyToFile,
)
from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent
from synapse.logging.context import make_deferred_yieldable
Expand Down Expand Up @@ -985,7 +985,7 @@ async def get_file(
headers = dict(response.headers.getAllRawHeaders())

try:
d = _readBodyToFile(response, output_stream, max_size)
d = readBodyToFile(response, output_stream, max_size)
d.addTimeout(self.default_timeout, self.reactor)
length = await make_deferred_yieldable(d)
except Exception as e:
Expand Down