diff --git a/uapi/appapi.py b/uapi/appapi.py index 8415fe4..4d3c115 100644 --- a/uapi/appapi.py +++ b/uapi/appapi.py @@ -30,6 +30,17 @@ where b.orgid = ${orgid}$ i = randint(0, cnt - 1) return recs[i].ownerid +async def get_deerer(upappid, callerid): + db = DBPools() + dbname = get_dbname() + async with db.sqlorContext(dbname) as sor: + ki = await get_userapikey(sor, upappid, callerid) + d = deerer(ki.myappid, ki.apikey, ki.secretkey) + if not d: + return None + return d[7:] + return None + async def get_uapi(upappid, apiname): dbname = get_dbname() db = DBPools() @@ -37,6 +48,40 @@ async def get_uapi(upappid, apiname): return await sor_get_uapi(sor, upappid, apiname) return None +async def get_userapikey(sor, upappid, callerid): + """ + argumemts: + upappid: upappid which will make call to + orgid: owner organization or user which as the caller of the call + return: + None: this orgid has not gotton apikey from upapp + dict if apikey and upapp infos + """ + sql = """select +a.myappid, +a.ownerid as appownerid, +a.baseurl, +b.apikey, +a.secretkey +from upapp a, upappkey b +where a.id = b.upappid +and a.id = ${appid}$ +and b.ownerid = ${ownerid}$""" + recs = await sor.sqlExe(sql, {'appid':upappid, 'ownerid': callerid}) + if len(recs) < 1: + e = Exception(f'{upappid=}, {callerid=} has not apikey') + exception(f'{e}, {format_exc()}') + raise e + r = recs[0] + debug(f'{r=}') + return DictObject(**{ + 'apikey':password_decode(r.apikey), + 'secretkey':password_decode(r.secretkey), + 'baseurl':r.baseurl, + 'appownerid': r.appownerid, + 'myappid': r.myappid + }) + async def sor_get_uapi(sor, upappid, apiname): sql = """select a.*, c.auth_apiname @@ -101,7 +146,7 @@ class UAPI: if uapi.auth_apiname: auth_uapi = await sor_get_uapi(sor, upappid, iuapi.auth_apiname) - kinfo = await self.get_userapikey(sor, upappid, callerid) + kinfo = await get_userapikey(sor, upappid, callerid) self.env.update(kinfo) return auth_uapi, uapi @@ -181,39 +226,5 @@ class UAPI: async for chunk in gen: yield chunk - async def get_userapikey(self, sor, upappid, callerid): - """ - argumemts: - upappid: upappid which will make call to - orgid: owner organization or user which as the caller of the call - return: - None: this orgid has not gotton apikey from upapp - dict if apikey and upapp infos - """ - sql = """select -a.myappid, -a.ownerid as appownerid, -a.baseurl, -b.apikey, -a.secretkey -from upapp a, upappkey b -where a.id = b.upappid - and a.id = ${appid}$ - and b.ownerid = ${ownerid}$""" - recs = await sor.sqlExe(sql, {'appid':upappid, 'ownerid': callerid}) - if len(recs) < 1: - e = Exception(f'{upappid=}, {callerid=} has not apikey') - exception(f'{e}, {format_exc()}') - raise e - r = recs[0] - debug(f'{r=}') - return DictObject(**{ - 'apikey':password_decode(r.apikey), - 'secretkey':password_decode(r.secretkey), - 'baseurl':r.baseurl, - 'appownerid': r.appownerid, - 'myappid': r.myappid - }) - if __name__ == '__main__': print('test') diff --git a/uapi/init.py b/uapi/init.py index e164b3d..05ba6a6 100644 --- a/uapi/init.py +++ b/uapi/init.py @@ -1,9 +1,10 @@ from ahserver.serverenv import ServerEnv -from .appapi import UAPI, deerer, bearer, get_callerid, sor_get_callerid +from .appapi import UAPI, deerer, bearer, get_callerid, sor_get_callerid, get_deerer def load_uapi(): g = ServerEnv() g.UAPI = UAPI + g.get_deerer = get_deerer g.deerer = deerer g.bearer = bearer