bugfix
This commit is contained in:
parent
bbd2ed42f2
commit
9553afeb77
BIN
models/uapi.xlsx
BIN
models/uapi.xlsx
Binary file not shown.
Binary file not shown.
Binary file not shown.
136
uapi/appapi.py
136
uapi/appapi.py
@ -15,7 +15,7 @@ def get_dbname():
|
|||||||
dbname = f('uapi')
|
dbname = f('uapi')
|
||||||
return dbname
|
return dbname
|
||||||
|
|
||||||
def build_manisdata(appid, apikey, secretkey):
|
def build_deererdata(appid, apikey, secretkey):
|
||||||
"""
|
"""
|
||||||
this appid is isusses by upapp we connect to,
|
this appid is isusses by upapp we connect to,
|
||||||
secretkey is with the appid, is s fixed key from upapp
|
secretkey is with the appid, is s fixed key from upapp
|
||||||
@ -29,17 +29,16 @@ def build_manisdata(appid, apikey, secretkey):
|
|||||||
def build_dearerdata(apikey):
|
def build_dearerdata(apikey):
|
||||||
return f'Dearer {apikey}'
|
return f'Dearer {apikey}'
|
||||||
|
|
||||||
async def get_apikeys(sor, appid, orgid, userid):
|
async def get_apikeys(sor, appid, userid):
|
||||||
ns = {
|
ns = {
|
||||||
'appid':appid,
|
'appid':appid,
|
||||||
'orgid':orgid,
|
|
||||||
'userid':userid,
|
'userid':userid,
|
||||||
'today':curDateString()
|
'today':curDateString()
|
||||||
}
|
}
|
||||||
sql = """select a.myid, b.apikey, b.secretkey from upapp a, upapikey b
|
sql = """select a.myid, b.apikey, b.secretkey from upapp a, upappkey b
|
||||||
where a.upappid = ${appid}$
|
where a.id = b.upappid
|
||||||
and b.userid = ${userid}$
|
and a.id = ${appid}$
|
||||||
and b.orgid = ${orgid}$
|
and b.owner = ${userid}$
|
||||||
and b.expired_date > ${today}$
|
and b.expired_date > ${today}$
|
||||||
and b.enabled_date <= ${today}$"""
|
and b.enabled_date <= ${today}$"""
|
||||||
recs = await sor.sqlExe(sql, ns)
|
recs = await sor.sqlExe(sql, ns)
|
||||||
@ -48,13 +47,14 @@ where a.upappid = ${appid}$
|
|||||||
return r
|
return r
|
||||||
return r
|
return r
|
||||||
|
|
||||||
async def sync_users(request, upappid, orgid):
|
async def sync_users(request, upappid, userid):
|
||||||
db = DBPools()
|
db = DBPools()
|
||||||
dbname = get_dbname()
|
dbname = get_dbname()
|
||||||
async with db.sqlorContext(dbname) as sor:
|
async with db.sqlorContext(dbname) as sor:
|
||||||
upapp = await get_upapp(sor, upappid)
|
upapp = await get_upapp(sor, upappid)
|
||||||
|
|
||||||
async def dearer_header(request, appid):
|
|
||||||
|
async def bearer_header(request, appid):
|
||||||
db = DBPools()
|
db = DBPools()
|
||||||
dbname = get_dbname()
|
dbname = get_dbname()
|
||||||
async with db.sqlorContext(dbname) as sor:
|
async with db.sqlorContext(dbname) as sor:
|
||||||
@ -66,7 +66,7 @@ async def dearer_header(request, appid):
|
|||||||
return dearer
|
return dearer
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def manis_header(request, appid):
|
async def deerer_header(request, appid):
|
||||||
db = DBPools()
|
db = DBPools()
|
||||||
dbname = get_dbname()
|
dbname = get_dbname()
|
||||||
async with db.sqlorContext(dbname) as sor:
|
async with db.sqlorContext(dbname) as sor:
|
||||||
@ -74,14 +74,15 @@ async def manis_header(request, appid):
|
|||||||
r = await get_apikeys(sor, appid, u.userorgid, u.userid)
|
r = await get_apikeys(sor, appid, u.userorgid, u.userid)
|
||||||
if r is None:
|
if r is None:
|
||||||
return None
|
return None
|
||||||
manis = build_manisdata(r.myid, r.apikey, r.secretkey)
|
manis = build_deererdata(r.myid, r.apikey, r.secretkey)
|
||||||
return manis
|
return manis
|
||||||
return None
|
return None
|
||||||
|
|
||||||
class UAPI:
|
class UAPI:
|
||||||
def __init__(self, env={}):
|
def __init__(self, request, env=DictObject()):
|
||||||
self.te = MyTemplateEngine([], env=env)
|
self.te = MyTemplateEngine([], env=env)
|
||||||
self.env = env
|
self.env = env
|
||||||
|
self.env.request.request = request
|
||||||
self.auth_api = None
|
self.auth_api = None
|
||||||
self.auth_ret = None
|
self.auth_ret = None
|
||||||
|
|
||||||
@ -93,28 +94,38 @@ class UAPI:
|
|||||||
te = MyTemplateEngine([], env=self.env)
|
te = MyTemplateEngine([], env=self.env)
|
||||||
return te.renders(tmplstr, ns)
|
return te.renders(tmplstr, ns)
|
||||||
|
|
||||||
async def cvt_upappapi2uapi(self, upappapiid, callerid, params={}):
|
async def get_uapis(self, upappid, uapiid, callerid, params={}):
|
||||||
self.env.update(params)
|
self.env.update(params)
|
||||||
db = DBPools()
|
db = DBPools()
|
||||||
dbname = get_dbname()
|
dbname = get_dbname()
|
||||||
uapi = None
|
uapi = None
|
||||||
auth_uapi = None
|
auth_uapi = None
|
||||||
async with db.sqlorContext(dbname) as sor:
|
async with db.sqlorContext(dbname) as sor:
|
||||||
upappapi = await self.get_upappapis(sor, upappapiid)
|
recs = await sor.R('uapi', {'id':uapiid})
|
||||||
kinfo = await self.get_orgapikey(sor, upappapi.upappid, callerid)
|
if len(recs)==0:
|
||||||
|
return None, None
|
||||||
|
uapi = recs[0]
|
||||||
|
kinfo = await self.get_userapikey(sor, upappid, callerid)
|
||||||
self.env.update(kinfo)
|
self.env.update(kinfo)
|
||||||
|
apisets = await sor.R('uapiset', {'id': uapi.usid})
|
||||||
|
if len(apisets) == 0:
|
||||||
|
return None, None
|
||||||
|
apiset = apisets[0]
|
||||||
auth_uapi = None
|
auth_uapi = None
|
||||||
if kinfo.auth_apiid:
|
if apiset.auth_apiid:
|
||||||
auth_upapi = await self.get_upappapis(sor, kinfo.auth_apiid)
|
uapis = await sor.R('uapi', {'id': apiset.auth_apiid})
|
||||||
auth_uapi = await self.get_uapi(sor, kinfo.auth_upapi.uapiid)
|
if len(uapis) == 0:
|
||||||
uapi = await self.get_uapi(sor, upappapi.uapiid)
|
e = Exceptions(f'{upappid=}, {uapiid=}, {callerid=} {apiset.auth_apiid=} auth_apiid not found')
|
||||||
|
exception(f'{e=}\n{format_exc()}')
|
||||||
|
raise e
|
||||||
|
auth_uapi = uapi[0]
|
||||||
return auth_uapi, uapi
|
return auth_uapi, uapi
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
async def __call__(self, upapiid, callerid, params={}):
|
async def __call__(self, upappid, upapiid, callerid, params={}):
|
||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
auth_uapi, uapi = await self.cvt_upappapi2uapi(upapiid,
|
auth_uapi, uapi = await self.get_uapis(upapiid,
|
||||||
callerid, params=params)
|
callerid, params=params)
|
||||||
if uapi is None:
|
if uapi is None:
|
||||||
return
|
return
|
||||||
@ -123,8 +134,8 @@ class UAPI:
|
|||||||
async for line in self.stream_gen(uapi):
|
async for line in self.stream_gen(uapi):
|
||||||
yield line
|
yield line
|
||||||
|
|
||||||
async def request(self, upapiid, callerid, params={}):
|
async def request(self, upappid, upapiid, callerid, params={}):
|
||||||
auth_uapi, uapi = await self.cvt_upappapi2uapi(upapiid,
|
auth_uapi, uapi = await self.get_uapis(upapiid,
|
||||||
callerid, params=params)
|
callerid, params=params)
|
||||||
if auth_uapi:
|
if auth_uapi:
|
||||||
await self.do_auth(auth_uapi)
|
await self.do_auth(auth_uapi)
|
||||||
@ -203,23 +214,7 @@ class UAPI:
|
|||||||
exception(f'{line=}\n{e=},{format_exc()}')
|
exception(f'{line=}\n{e=},{format_exc()}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def get_upappapis(self, sor, upapiid):
|
async def get_userapikey(self, sor, upappid, callerid):
|
||||||
recs = await sor.R('upappapis', {'id': upapiid})
|
|
||||||
if len(recs) < 1:
|
|
||||||
e = Exception(f'{upapiid} not found in table(upappapis)')
|
|
||||||
exception(f'{e}\n{format_exc()}')
|
|
||||||
raise e
|
|
||||||
return recs[0]
|
|
||||||
|
|
||||||
async def get_uapi(self, sor, uapiid):
|
|
||||||
recs = await sor.R('uapi', {'id': uapiid})
|
|
||||||
if len(recs) < 1:
|
|
||||||
e = Exception(f'{uapiid} not found in table(uapi)')
|
|
||||||
exception(f'{e}\n{format_exc()}')
|
|
||||||
raise e
|
|
||||||
return recs[0]
|
|
||||||
|
|
||||||
async def get_orgapikey(self, sor, upappid, callerid):
|
|
||||||
"""
|
"""
|
||||||
argumemts:
|
argumemts:
|
||||||
upappid: upappid which will make call to
|
upappid: upappid which will make call to
|
||||||
@ -230,15 +225,15 @@ class UAPI:
|
|||||||
"""
|
"""
|
||||||
sql = """select
|
sql = """select
|
||||||
a.myappid,
|
a.myappid,
|
||||||
a.apisetid,
|
a.ownerid as appownerid,
|
||||||
a.auth_apiid,
|
|
||||||
a.baseurl,
|
a.baseurl,
|
||||||
b.*
|
b.apikey,
|
||||||
|
b.secretkey
|
||||||
from upapp a, upappkey b
|
from upapp a, upappkey b
|
||||||
where a.id = b.upappid
|
where a.id = b.upappid
|
||||||
and a.id = ${appid}$
|
and a.id = ${appid}$
|
||||||
and b.ownerid = ${orgid}$"""
|
and b.ownerid = ${ownerid}$"""
|
||||||
recs = await sor.sqlExe(sql, {'appid':upappid, 'orgid': callerid})
|
recs = await sor.sqlExe(sql, {'appid':upappid, 'ownerid': callerid})
|
||||||
if len(recs) < 1:
|
if len(recs) < 1:
|
||||||
e = Exception(f'{appid=}, {callerid=} has not apikey')
|
e = Exception(f'{appid=}, {callerid=} has not apikey')
|
||||||
exception(f'{e}, {format_exc()}')
|
exception(f'{e}, {format_exc()}')
|
||||||
@ -247,55 +242,8 @@ where a.id = b.upappid
|
|||||||
return DictObject(**{
|
return DictObject(**{
|
||||||
'apikey':password_decode(r.apikey),
|
'apikey':password_decode(r.apikey),
|
||||||
'secretkey':password_decode(r.secretkey),
|
'secretkey':password_decode(r.secretkey),
|
||||||
'apisetid': r.apisetid,
|
'baseurl':r.baseurl,
|
||||||
'auth_apiid': r.auth_apiid,
|
'appownerid': r.appownerid,
|
||||||
'myappid': r.myappid
|
'myappid': r.myappid
|
||||||
})
|
})
|
||||||
|
|
||||||
async def get_authapi(self, sor):
|
|
||||||
sql = "select * from jsonhttpapi where name = 'auth' and appid=${appid}$"
|
|
||||||
recs = await sor.sqlExe(sql, {'appid': self.appid})
|
|
||||||
if len(recs):
|
|
||||||
return recs[0]
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def get_apiinfo(self, orgids, apiname):
|
|
||||||
db = DBPools()
|
|
||||||
dbname = get_serverenv('get_module_dbname')('uapi')
|
|
||||||
async with db.sqlorContext(dbname) as sor:
|
|
||||||
sql = """select c.baseurl,
|
|
||||||
c.name as appname,
|
|
||||||
b.*,
|
|
||||||
a.apikey,
|
|
||||||
a.secretkey
|
|
||||||
from upappkey a, jsonhttpapi b, upapp c
|
|
||||||
where
|
|
||||||
a.upappid=b.appid
|
|
||||||
and a.upappid = c.id
|
|
||||||
and b.appid = ${appid}$
|
|
||||||
and b.name = ${apiname}$
|
|
||||||
and a.ownerid = ${orgid}$
|
|
||||||
"""
|
|
||||||
for orgid in orgids:
|
|
||||||
ns = {
|
|
||||||
"orgid": orgid,
|
|
||||||
"apiname": appname,
|
|
||||||
"appid": self.appid
|
|
||||||
}
|
|
||||||
recs = await sor.sqlExe(sql, ns)
|
|
||||||
if len(recs) > 0:
|
|
||||||
rec = recs[0]
|
|
||||||
rec.apikey = password_decode(rec.apikey)
|
|
||||||
if rec.secretkey:
|
|
||||||
rec.secretkey = password_decode(rec.secretkey)
|
|
||||||
if rec.need_auth and self.auth_api is None:
|
|
||||||
self.auth_api = await self.get_authapi(sor)
|
|
||||||
if rec.need_auth and self.auth_api is None:
|
|
||||||
e = Exception(f'{rec.appname} app has not auth api but need')
|
|
||||||
exception(f'{e=}, {format_exc()}')
|
|
||||||
raise e
|
|
||||||
rec.auth_api = self.auth_api
|
|
||||||
rec.apikey_orgid = orgid
|
|
||||||
return recs[0]
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user