Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix formatting #45

Merged
merged 5 commits into from
Jan 8, 2024
Merged
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
31 changes: 22 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,10 @@ 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)
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 +159,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 +171,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 +196,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
1 change: 1 addition & 0 deletions src/fastapi_poe/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class SettingsResponse(BaseModel):
allow_attachments: bool = False
introduction_message: str = ""


class AttachmentUploadResponse(BaseModel):
inline_ref: Optional[str]

Expand Down