From 4e0e092aeb75e4a2969dc4d6642c964fcc84ca77 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 29 Jul 2023 15:43:24 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- claude-api/claude_api.py | 30 +++++++----------------------- usecases/console_chat.py | 6 +++--- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/claude-api/claude_api.py b/claude-api/claude_api.py index 3e2a25a..ab34018 100644 --- a/claude-api/claude_api.py +++ b/claude-api/claude_api.py @@ -28,9 +28,7 @@ def get_organization_id(self): response = requests.request("GET", url, headers=headers) res = json.loads(response.text) - uuid = res[0]['uuid'] - - return uuid + return res[0]['uuid'] def get_content_type(self, file_path): # Function to determine content type based on file extension @@ -78,8 +76,7 @@ def send_message(self, prompt, conversation_id, attachment=None): # Upload attachment if provided attachments = [] if attachment: - attachment_response = self.upload_attachment(attachment) - if attachment_response: + if attachment_response := self.upload_attachment(attachment): attachments = [attachment_response] else: return {"Error: Invalid file format. Please try again."} @@ -121,10 +118,7 @@ def send_message(self, prompt, conversation_id, attachment=None): decoded_data = response.content.decode("utf-8") data = decoded_data.strip().split('\n')[-1] - answer = {"answer": json.loads(data[6:])['completion']}['answer'] - - # Returns answer - return answer + return {"answer": json.loads(data[6:])['completion']}['answer'] # Deletes the conversation def delete_conversation(self, conversation_id): @@ -150,10 +144,7 @@ def delete_conversation(self, conversation_id): response = requests.request("DELETE", url, headers=headers, data=payload) # Returns True if deleted or False if any error in deleting - if response.status_code == 204: - return True - else: - return False + return response.status_code == 204 # Returns all the messages in conversation def chat_conversation_history(self, conversation_id): @@ -181,8 +172,7 @@ def chat_conversation_history(self, conversation_id): def generate_uuid(self): random_uuid = uuid.uuid4() random_uuid_str = str(random_uuid) - formatted_uuid = f"{random_uuid_str[0:8]}-{random_uuid_str[9:13]}-{random_uuid_str[14:18]}-{random_uuid_str[19:23]}-{random_uuid_str[24:]}" - return formatted_uuid + return f"{random_uuid_str[:8]}-{random_uuid_str[9:13]}-{random_uuid_str[14:18]}-{random_uuid_str[19:23]}-{random_uuid_str[24:]}" def create_new_chat(self): url = f"https://claude.ai/api/organizations/{self.organization_id}/chat_conversations" @@ -245,10 +235,7 @@ def upload_attachment(self, file_path): } response = requests.post(url, headers=headers, files=files) - if response.status_code == 200: - return response.json() - else: - return False + return response.json() if response.status_code == 200 else False @@ -278,8 +265,5 @@ def rename_chat(self, title, conversation_id): response = requests.request("POST", url, headers=headers, data=payload) - if response.status_code == 200: - return True - else: - return False + return response.status_code == 200 diff --git a/usecases/console_chat.py b/usecases/console_chat.py index 97a9b27..cbd78ab 100644 --- a/usecases/console_chat.py +++ b/usecases/console_chat.py @@ -2,10 +2,10 @@ from claude_api import Client def get_cookie(): - cookie = os.environ.get('cookie') - if not cookie: + if cookie := os.environ.get('cookie'): + return cookie + else: raise ValueError("Please set the 'cookie' environment variable.") - return cookie def main(): cookie = get_cookie()