rintrix/matrixapi.py

18 lines
840 B
Python
Raw Normal View History

2022-08-18 03:46:07 +10:00
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