feat: creating chats for cards and clients, sending and deleting text messages
This commit is contained in:
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)
|
||||
Reference in New Issue
Block a user