feat: creating chats for cards and clients, sending and deleting text messages
This commit is contained in:
		
							
								
								
									
										0
									
								
								external/chat/__init__.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								external/chat/__init__.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
								
								
									
										36
									
								
								external/chat/chat_client.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								external/chat/chat_client.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
import aiohttp
 | 
			
		||||
import jwt
 | 
			
		||||
 | 
			
		||||
from backend.config import CHATS_SYNC_URL, CHAT_CONNECTOR_API_KEY
 | 
			
		||||
from external.chat.schemas import *
 | 
			
		||||
from services.auth import algorithm
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ChatClient:
 | 
			
		||||
    def __init__(self, api_key: str):
 | 
			
		||||
        self.api_key = api_key
 | 
			
		||||
        self.headers = {
 | 
			
		||||
            'Authorization': 'Bearer ' + self.create_jwt_token()
 | 
			
		||||
        }
 | 
			
		||||
        self.base_url = CHATS_SYNC_URL
 | 
			
		||||
        self.chats_sync_endpoint = '/chats-sync'
 | 
			
		||||
        self.groups_endpoint = '/group'
 | 
			
		||||
 | 
			
		||||
    def create_jwt_token(self):
 | 
			
		||||
        return jwt.encode({'sub': self.api_key}, CHAT_CONNECTOR_API_KEY, algorithm=algorithm)
 | 
			
		||||
 | 
			
		||||
    async def _method(self, http_method, method, **kwargs):
 | 
			
		||||
        async with aiohttp.ClientSession(headers=self.headers) as session:
 | 
			
		||||
            async with session.request(http_method, self.base_url + method, **kwargs) as response:
 | 
			
		||||
                print(response)
 | 
			
		||||
                return await response.json()
 | 
			
		||||
 | 
			
		||||
    async def create_group(self, request: ExternalCreateGroupRequest) -> ExternalCreateGroupResponse:
 | 
			
		||||
        json_data = request.model_dump()
 | 
			
		||||
        response = await self._method('POST', self.groups_endpoint + '/create', json=json_data)
 | 
			
		||||
        return ExternalCreateGroupResponse.model_validate(response)
 | 
			
		||||
 | 
			
		||||
    async def create_topic(self, request: ExternalCreateTopicRequest) -> ExternalCreateTopicResponse:
 | 
			
		||||
        json_data = request.model_dump()
 | 
			
		||||
        response = await self._method('POST', self.groups_endpoint + '/topic/create', json=json_data)
 | 
			
		||||
        return ExternalCreateTopicResponse.model_validate(response)
 | 
			
		||||
							
								
								
									
										32
									
								
								external/chat/schemas.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								external/chat/schemas.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
from typing import Optional
 | 
			
		||||
from uuid import UUID
 | 
			
		||||
 | 
			
		||||
from schemas.base import BaseSchema
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# region Requests
 | 
			
		||||
 | 
			
		||||
class ExternalCreateGroupRequest(BaseSchema):
 | 
			
		||||
    title: str
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ExternalCreateTopicRequest(BaseSchema):
 | 
			
		||||
    group_id: str
 | 
			
		||||
    title: str
 | 
			
		||||
    icon_emoji_id: Optional[int] = None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# endregion
 | 
			
		||||
 | 
			
		||||
# region Responses
 | 
			
		||||
 | 
			
		||||
class ExternalCreateGroupResponse(BaseSchema):
 | 
			
		||||
    tg_group_id: int
 | 
			
		||||
    group_id: UUID
 | 
			
		||||
    tg_invite_link: str
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ExternalCreateTopicResponse(BaseSchema):
 | 
			
		||||
    tg_topic_id: int
 | 
			
		||||
 | 
			
		||||
# endregion
 | 
			
		||||
		Reference in New Issue
	
	Block a user