Skip to content

Commit

Permalink
Fixed __prepare_file_attachment using requests with files parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
st1vms committed Jan 6, 2024
1 parent 5a97068 commit 1e40c1f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 9 additions & 11 deletions claude2_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mimetypes import guess_type
from zlib import decompress as zlib_decompress
from zlib import MAX_WBITS
from requests import Request, Session
from curl_cffi.requests import Response
from curl_cffi.requests import get as http_get
from curl_cffi.requests import post as http_post
Expand Down Expand Up @@ -231,7 +232,7 @@ def __get_content_type(self, fpath: str):
mime_type, _ = guess_type(f"file.{extension}")
return mime_type or "application/octet-stream"

def __prepare_file_attachment(self, fpath: str, chat_id: str) -> dict | None:
def __prepare_file_attachment(self, fpath: str) -> dict | None:
content_type = self.__get_content_type(fpath)
if content_type == "text/plain":
return self.__prepare_text_file_attachment(fpath)
Expand All @@ -244,9 +245,7 @@ def __prepare_file_attachment(self, fpath: str, chat_id: str) -> dict | None:
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/json",
"Content-Length": f"{ospath.getsize(fpath)}",
"Referer": f"{self.__BASE_URL}/chat/{chat_id}",
"Referer": f"{self.__BASE_URL}/chats",
"Origin": self.__BASE_URL,
"DNT": "1",
"Sec-Fetch-Dest": "empty",
Expand All @@ -260,16 +259,15 @@ def __prepare_file_attachment(self, fpath: str, chat_id: str) -> dict | None:
with open(fpath, "rb") as fp:
files = {
"file": (ospath.basename(fpath), fp, content_type),
"orgUuid": (self.__session.organization_id,),
"orgUuid": (None, self.__session.organization_id),
}

response = http_post(
url,
headers=headers,
files=files,
s = Session()
req = s.prepare_request(Request("POST", url, headers=headers, files=files))
response = s.send(
req,
proxies=self.__get_proxy(),
timeout=self.timeout,
impersonate="chrome110",
)
if response.status_code == 200:
return response.json()
Expand Down Expand Up @@ -475,7 +473,7 @@ def send_message(
attachments = [
a
for a in [
self.__prepare_file_attachment(path, chat_id)
self.__prepare_file_attachment(path)
for path in attachment_paths
]
if a
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name="unofficial-claude2-api",
version="0.2.4",
version="0.2.5",
author="st1vms",
author_email="[email protected]",
description=__DESCRIPTION,
Expand Down

0 comments on commit 1e40c1f

Please sign in to comment.