Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions src/hip/_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, parser: HTTPParser, is_response: bool):
self._buffer = io.BytesIO()
self._finished = False

def read(self, size=-1) -> bytes:
async def read(self, size=-1) -> bytes:
if self._finished:
return b""

Expand All @@ -31,7 +31,7 @@ def read(self, size=-1) -> bytes:

# Read each chunk in turn.
while (size < 0) or (length < size):
section = self._parser.recv_body()
section = await self._parser.recv_body()
sections.append(section)
length += len(section)
if section == b"":
Expand All @@ -47,10 +47,10 @@ def read(self, size=-1) -> bytes:

return output

def close(self) -> None:
async def close(self) -> None:
self._buffer.close()
if self._is_response:
self._parser.complete()
await self._parser.complete()


class GZipStream(Stream):
Expand All @@ -61,9 +61,9 @@ def __init__(self, stream: Stream):
self._finished = False
self._buffer = bytearray()

def read(self, size=-1) -> bytes:
async def read(self, size=-1) -> bytes:
while (size < 0 or len(self._buffer) < size) and not self._finished:
compressed = self._stream.read(8192)
compressed = await self._stream.read(8192)

try:
if compressed:
Expand All @@ -88,5 +88,5 @@ def read(self, size=-1) -> bytes:

return output

def close(self) -> None:
self._stream.close()
async def close(self) -> None:
await self._stream.close()
46 changes: 23 additions & 23 deletions src/hip/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,75 +49,75 @@ def build_request(
content=content,
)

def request(
async def request(
self,
method: Method | str,
url: URL | str,
headers: Headers | dict[str, str] | None = None,
content: Content | bytes | None = None,
) -> Response:
request = self.build_request(method, url, headers=headers, content=content)
with self.via.send(request) as (status_code, resp_headers, stream):
async with await self.via.send(request) as (status_code, resp_headers, stream):
ct = resp_headers.get("Content-Type", "application/octet-stream")
cls = select_media(self.media, ct)
content = cls.parse(stream)
content = await cls.parse(stream)
return Response(
status_code=status_code,
headers=resp_headers,
content=content,
)

def get(
async def get(
self,
url: URL | str,
headers: Headers | dict[str, str] | None = None,
):
return self.request("GET", url, headers=headers)
return await self.request("GET", url, headers=headers)

def post(
async def post(
self,
url: URL | str,
headers: Headers | dict[str, str] | None = None,
content: Content | bytes | None = None,
):
return self.request("POST", url, headers=headers, content=content)
return await self.request("POST", url, headers=headers, content=content)

def put(
async def put(
self,
url: URL | str,
headers: Headers | dict[str, str] | None = None,
content: Content | bytes | None = None,
):
return self.request("PUT", url, headers=headers, content=content)
return await self.request("PUT", url, headers=headers, content=content)

def patch(
async def patch(
self,
url: URL | str,
headers: Headers | dict[str, str] | None = None,
content: Content | bytes | None = None,
):
return self.request("PATCH", url, headers=headers, content=content)
return await self.request("PATCH", url, headers=headers, content=content)

def delete(
async def delete(
self,
url: URL | str,
headers: Headers | dict[str, str] | None = None,
):
return self.request("DELETE", url, headers=headers)
return await self.request("DELETE", url, headers=headers)

def close(self):
self.via.close()
async def close(self):
await self.via.close()

def __enter__(self):
async def __aenter__(self):
return self

def __exit__(
async def __aexit__(
self,
exc_type: type[BaseException] | None = None,
exc_value: BaseException | None = None,
traceback: types.TracebackType | None = None,
):
self.close()
await self.close()

def __repr__(self):
return f"<Client [{self.transport.description()}]>"
Expand Down Expand Up @@ -150,9 +150,9 @@ def build_redirect_request(

return Request(method, url, headers, content)

def send(self, request: Request) -> SendContext:
async def send(self, request: Request) -> SendContext:
while True:
ctx = self._transport.send(request)
ctx = await self._transport.send(request)

# Determine if we have a redirect or not.
redirect = self.build_redirect_request(request, ctx)
Expand All @@ -166,10 +166,10 @@ def send(self, request: Request) -> SendContext:
# request/response cycle.
with ctx:
_, _, stream = ctx
stream.read()
await stream.read()

# Make the next request
request = redirect

def close(self):
self._transport.close()
async def close(self):
await self._transport.close()
28 changes: 14 additions & 14 deletions src/hip/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class Content:
def open(self) -> Stream:
async def open(self) -> Stream:
raise NotImplementedError()

def headers(self) -> Headers:
Expand All @@ -44,7 +44,7 @@ def __init__(self, data: typing.Any, source: bytes | None = None) -> None:
else source
)

def open(self) -> Stream:
async def open(self) -> Stream:
return ByteStream(self._content)

def headers(self) -> Headers:
Expand All @@ -58,8 +58,8 @@ def headers(self) -> Headers:
)

@classmethod
def parse(self, stream: Stream) -> "Content":
source = stream.read()
async def parse(self, stream: Stream) -> "Content":
source = await stream.read()
data = json.loads(source)
return JSON(data, source)

Expand Down Expand Up @@ -99,7 +99,7 @@ def __init__(self, text: str, source: bytes | None = None) -> None:
self._text = text
self._content = self._text.encode("utf-8") if source is None else source

def open(self) -> Stream:
async def open(self) -> Stream:
return ByteStream(self._content)

def headers(self) -> Headers:
Expand All @@ -113,8 +113,8 @@ def headers(self) -> Headers:
)

@classmethod
def parse(self, stream: Stream) -> Content:
source = stream.read()
async def parse(self, stream: Stream) -> Content:
source = await stream.read()
text = source.decode("utf-8")
return Text(text, source)

Expand All @@ -133,7 +133,7 @@ def __init__(self, text: str, source: bytes | None = None) -> None:
self._text = text
self._content = self._text.encode("utf-8") if source is None else source

def open(self) -> Stream:
async def open(self) -> Stream:
return ByteStream(self._content)

def headers(self) -> Headers:
Expand All @@ -147,8 +147,8 @@ def headers(self) -> Headers:
)

@classmethod
def parse(self, stream: Stream) -> Content:
source = stream.read()
async def parse(self, stream: Stream) -> Content:
source = await stream.read()
text = source.decode("utf-8")
return HTML(text, source)

Expand All @@ -166,16 +166,16 @@ class Binary(Content):
def __init__(self, content: bytes | None) -> None:
self._content = b"" if (content is None) else content

def open(self) -> Stream:
async def open(self) -> Stream:
return ByteStream(self._content)

def headers(self) -> Headers:
content_length = len(self._content)
return Headers({"Content-Length": f"{content_length}"})

@classmethod
def parse(self, stream: Stream) -> Content:
source = stream.read()
async def parse(self, stream: Stream) -> Content:
source = await stream.read()
return Binary(source)

def __bytes__(self) -> bytes:
Expand All @@ -189,7 +189,7 @@ class Empty(Content):
def __init__(self) -> None:
pass

def open(self) -> Stream:
async def open(self) -> Stream:
return ByteStream(b"")

def headers(self) -> Headers:
Expand Down
10 changes: 5 additions & 5 deletions src/hip/_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(

# Content API

def open(self) -> Stream:
async def open(self) -> Stream:
return ByteStream(self._content)

def headers(self) -> Headers:
Expand Down Expand Up @@ -168,7 +168,7 @@ def __init__(self, path: str):
def name(self) -> str:
return os.path.basename(self._path)

def open(self) -> Stream:
async def open(self) -> Stream:
fileobj = open(self._path, "rb") # noqa: SIM115
return FileStream(fileobj)

Expand Down Expand Up @@ -254,8 +254,8 @@ def get_list(self, key: str) -> list[File]:
return list(self._dict.get(key, []))

# Content interface
def open(self) -> Stream:
return MultiPart(files=self, boundary=self._boundary).open()
async def open(self) -> Stream:
return await MultiPart(files=self, boundary=self._boundary).open()

def headers(self) -> Headers:
content_type = f"multipart/form-data; boundary={self._boundary}"
Expand Down Expand Up @@ -321,7 +321,7 @@ def form(self) -> Form:
def files(self) -> Files:
return self._files

def open(self) -> Stream:
async def open(self) -> Stream:
form = [(key, value) for key, value in self._form.items()]
files = [(key, file._path) for key, file in self._files.items()]
return MultiPartStream(form, files, boundary=self._boundary)
Expand Down
Loading
Loading