Skip to content

add: proxies argument for Client class initializing. #73

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,5 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

34 changes: 20 additions & 14 deletions claude-api/claude_api.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import json
import os
import re
import uuid
from curl_cffi import requests

import requests as req
import re
from curl_cffi import requests


class Client:

def __init__(self, cookie):
def __init__(self, cookie, proxies=None):
"""
:param cookie: cookie for Claude.ai
:param proxies: proxies for requests, e.g. {"http": "http://localhost:3450", "https": "http://localhost:3450"}
"""
self.cookie = cookie
self.proxies = proxies
self.organization_id = self.get_organization_id()

def get_organization_id(self):
Expand All @@ -28,7 +34,7 @@ def get_organization_id(self):
'Cookie': f'{self.cookie}'
}

response = requests.get(url, headers=headers,impersonate="chrome110")
response = requests.get(url, headers=headers,impersonate="chrome110", proxies=self.proxies)
res = json.loads(response.text)
uuid = res[0]['uuid']

Expand Down Expand Up @@ -64,7 +70,7 @@ def list_all_conversations(self):
'Cookie': f'{self.cookie}'
}

response = requests.get(url, headers=headers,impersonate="chrome110")
response = requests.get(url, headers=headers,impersonate="chrome110", proxies=self.proxies)
conversations = response.json()

# Returns all conversation information in a list
Expand Down Expand Up @@ -119,7 +125,7 @@ def send_message(self, prompt, conversation_id, attachment=None,timeout=500):
'TE': 'trailers'
}

response = requests.post( url, headers=headers, data=payload,impersonate="chrome110",timeout=500)
response = requests.post( url, headers=headers, data=payload,impersonate="chrome110",timeout=500, proxies=self.proxies)
decoded_data = response.content.decode("utf-8")
decoded_data = re.sub('\n+', '\n', decoded_data).strip()
data_strings = decoded_data.split('\n')
Expand Down Expand Up @@ -156,7 +162,7 @@ def delete_conversation(self, conversation_id):
'TE': 'trailers'
}

response = requests.delete( url, headers=headers, data=payload,impersonate="chrome110")
response = requests.delete( url, headers=headers, data=payload,impersonate="chrome110", proxies=self.proxies)

# Returns True if deleted or False if any error in deleting
if response.status_code == 204:
Expand All @@ -181,8 +187,8 @@ def chat_conversation_history(self, conversation_id):
'Cookie': f'{self.cookie}'
}

response = requests.get( url, headers=headers,impersonate="chrome110")
response = requests.get( url, headers=headers,impersonate="chrome110", proxies=self.proxies)


# List all the conversations in JSON
return response.json()
Expand Down Expand Up @@ -214,7 +220,7 @@ def create_new_chat(self):
'TE': 'trailers'
}

response = requests.post( url, headers=headers, data=payload,impersonate="chrome110")
response = requests.post( url, headers=headers, data=payload,impersonate="chrome110", proxies=self.proxies)

# Returns JSON of the newly created conversation information
return response.json()
Expand Down Expand Up @@ -271,9 +277,9 @@ def upload_attachment(self, file_path):
return response.json()
else:
return False





# Renames the chat conversation title
def rename_chat(self, title, conversation_id):
url = "https://claude.ai/api/rename_chat"
Expand All @@ -298,10 +304,10 @@ def rename_chat(self, title, conversation_id):
'TE': 'trailers'
}

response = requests.post(url, headers=headers, data=payload,impersonate="chrome110")
response = requests.post(url, headers=headers, data=payload,impersonate="chrome110", proxies=self.proxies)

if response.status_code == 200:
return True
else:
return False