rintrix/matrixapi.py

18 lines
840 B
Python

class MatrixLibHttp():
def __init__(self,instance,token=None,username=None,password=None,chatlib=None):
self.instance=instance
self.https=True
self.token=token
if chatlib is None:
import chatlib
chatlib.addlistener() # TODO: probably should make that a decorator actually. Lol, it already is.
@property
def baseurl(self):
return 'http'+['','s'][self.https]+'://'+self.instance+'/_matrix/client'
async def request(self,endpoint='sync',method='GET', ver=0,headers={}, *args,**kwargs):
async with self.session.request(method, f'{self.baseurl}/r{ver}/{endpoint}', headers=headers|{'Authorization':f'Bearer {self.token}'}, *args,**kwargs) as fetched:
if fetched.status_code!=200: raise Exception('fix ur shit')
try: return await fetched.json()
except JSONDecodeError: pass # TODO: Figure out what this is called