This commit is contained in:
yumoqing 2025-02-23 11:30:30 +08:00
parent ffad3d0494
commit f605e8ad11
2 changed files with 19 additions and 7 deletions

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
import aiohttp import aiohttp
from aiohttp import FormData
import json import json
from appPublic.myTE import MyTemplateEngine from appPublic.myTE import MyTemplateEngine
import re import re
@ -109,11 +110,13 @@ class HttpClient:
session = self.getsession(url) session = self.getsession(url)
class JsonHttpAPI: class JsonHttpAPI:
def __init__(self): def __init__(self, env={}):
this.te = MyTemplateEngine() self.env = env
this.hc = HttpClient() self.te = MyTemplateEngine([], env=env)
self.hc = HttpClient()
async def call(self, url, method='GET', ns={}, async def call(self, url, method='GET', ns={},
stream_func=None,
headerstmpl=None, headerstmpl=None,
paramstmpl=None, paramstmpl=None,
datatmpl=None, datatmpl=None,
@ -121,19 +124,26 @@ class JsonHttpAPI:
headers = None headers = None
if headerstmpl: if headerstmpl:
headers = json.loads(self.te.renders(headerstmpl, ns)) headers = json.loads(self.te.renders(headerstmpl, ns))
info(f'{headers=},{ns=}, {headerstmpl=}')
params = None params = None
if paramstmpl: if paramstmpl:
params = json.loads(self.te.renders(paramstmpl, ns)) params = json.loads(self.te.renders(paramstmpl, ns))
data = None data = None
if datatmpl: if datatmpl:
datas = json.loads(self.te.renders(datatmpl, ns)) datadic = json.loads(self.te.renders(datatmpl, ns))
data = FormData()
for k,v in datadic.items():
data.add_field(k, v)
info(f'{data=},{ns=}, {datatmpl=}')
resp = await self.hc.request(url, method=method, headers=headers, resp = await self.hc.request(url, method=method, headers=headers,
response_type=RESPONSE_JSON, response_type=RESPONSE_JSON,
params=params, params=params,
data=data) data=data)
ret = resp ret = resp
if resptmpl: if resptmpl:
ret = self.te.renders(resptmpl, resp) rets = self.te.renders(resptmpl, resp)
ret = json.loads(self.te.renders(resptmpl, resp))
return ret return ret
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -17,11 +17,11 @@ def string_template_render(tmp_string, data):
return rtemplate.render(**data) return rtemplate.render(**data)
class MyTemplateEngine: class MyTemplateEngine:
def __init__(self,pathList,file_coding='utf-8',out_coding='utf-8'): def __init__(self,pathList,file_coding='utf-8',out_coding='utf-8', env={}):
self.file_coding = file_coding self.file_coding = file_coding
self.out_coding = out_coding self.out_coding = out_coding
loader = FileSystemLoader(pathList, encoding=self.file_coding) loader = FileSystemLoader(pathList, encoding=self.file_coding)
self.env = Environment(loader=loader, enable_async=False) self.env = Environment(loader=loader, enable_async=False)
denv={ denv={
'json':json, 'json':json,
'hasattr':hasattr, 'hasattr':hasattr,
@ -41,6 +41,8 @@ class MyTemplateEngine:
'extname':lambda x:os.path.splitext(x)[-1], 'extname':lambda x:os.path.splitext(x)[-1],
} }
self.env.globals.update(denv) self.env.globals.update(denv)
if env:
self.env.globals.update(env)
def set(self,k,v): def set(self,k,v):
self.env.globals.update({k:v}) self.env.globals.update({k:v})