Skip to content

Commit

Permalink
fix formatting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohntheLi committed Jan 8, 2024
1 parent 5c03491 commit faced62
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/fastapi_poe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
class InvalidParameterError(Exception):
pass


class AttachmentUploadError(Exception):
pass


class LoggingMiddleware(BaseHTTPMiddleware):
async def set_body(self, request: Request):
receive_ = await request._receive()
Expand Down Expand Up @@ -125,7 +127,7 @@ async def post_message_attachment(
file_data: Optional[Union[bytes, BinaryIO]] = None,
filename: Optional[str] = None,
content_type: Optional[str] = None,
is_inline: bool = False
is_inline: bool = False,
) -> AttachmentUploadResponse:
task = asyncio.create_task(
self._make_file_attachment_request(
Expand All @@ -135,10 +137,12 @@ async def post_message_attachment(
file_data=file_data,
filename=filename,
content_type=content_type,
is_inline=is_inline
is_inline=is_inline,
)
)
pending_tasks_for_message = self._pending_file_attachment_tasks.get(message_id, None)
pending_tasks_for_message = self._pending_file_attachment_tasks.get(
message_id, None
)
if pending_tasks_for_message is None:
pending_tasks_for_message = set()
self._pending_file_attachment_tasks[message_id] = pending_tasks_for_message
Expand All @@ -157,7 +161,7 @@ async def _make_file_attachment_request(
file_data: Optional[Union[bytes, BinaryIO]] = None,
filename: Optional[str] = None,
content_type: Optional[str] = None,
is_inline: bool = False
is_inline: bool = False,
) -> AttachmentUploadResponse:
url = "https://www.quora.com/poe_api/file_attachment_POST"

Expand All @@ -169,13 +173,18 @@ async def _make_file_attachment_request(
raise InvalidParameterError(
"Cannot provide filename or file_data if download_url is provided."
)
data = {"message_id": message_id, "is_inline": is_inline, "download_url": download_url}
data = {
"message_id": message_id,
"is_inline": is_inline,
"download_url": download_url,
}
request = httpx.Request("POST", url, data=data, headers=headers)
elif file_data and filename:
data = {"message_id": message_id, "is_inline": is_inline}
files = {
"file": (
(filename, file_data) if content_type is None
(filename, file_data)
if content_type is None
else (filename, file_data, content_type)
)
}
Expand All @@ -189,17 +198,23 @@ async def _make_file_attachment_request(
response = await client.send(request)

if response.status_code != 200:
raise AttachmentUploadError(f"{response.status_code}: {response.reason_phrase}")
raise AttachmentUploadError(
f"{response.status_code}: {response.reason_phrase}"
)

return AttachmentUploadResponse(inline_ref=response.json().get("inline_ref"))
return AttachmentUploadResponse(
inline_ref=response.json().get("inline_ref")
)

except httpx.HTTPError:
logger.error("An HTTP error occurred when attempting to attach file")
raise

async def _process_pending_attachment_requests(self, message_id):
try:
await asyncio.gather(*self._pending_file_attachment_tasks.pop(message_id, []))
await asyncio.gather(
*self._pending_file_attachment_tasks.pop(message_id, [])
)
except Exception:
logger.error("Error processing pending attachment requests")
raise
Expand Down

0 comments on commit faced62

Please sign in to comment.