diff --git a/appPublic/httpclient.py b/appPublic/httpclient.py index 40f18f0..51bf10c 100755 --- a/appPublic/httpclient.py +++ b/appPublic/httpclient.py @@ -1,5 +1,6 @@ import asyncio import aiohttp +from aiohttp import FormData import json from appPublic.myTE import MyTemplateEngine import re @@ -109,11 +110,13 @@ class HttpClient: session = self.getsession(url) class JsonHttpAPI: - def __init__(self): - this.te = MyTemplateEngine() - this.hc = HttpClient() + def __init__(self, env={}): + self.env = env + self.te = MyTemplateEngine([], env=env) + self.hc = HttpClient() async def call(self, url, method='GET', ns={}, + stream_func=None, headerstmpl=None, paramstmpl=None, datatmpl=None, @@ -121,19 +124,26 @@ class JsonHttpAPI: headers = None if headerstmpl: headers = json.loads(self.te.renders(headerstmpl, ns)) + info(f'{headers=},{ns=}, {headerstmpl=}') params = None if paramstmpl: params = json.loads(self.te.renders(paramstmpl, ns)) data = None 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, response_type=RESPONSE_JSON, params=params, data=data) ret = resp if resptmpl: - ret = self.te.renders(resptmpl, resp) + rets = self.te.renders(resptmpl, resp) + ret = json.loads(self.te.renders(resptmpl, resp)) return ret if __name__ == '__main__': diff --git a/appPublic/myTE.py b/appPublic/myTE.py index a3fcb80..0728638 100755 --- a/appPublic/myTE.py +++ b/appPublic/myTE.py @@ -17,11 +17,11 @@ def string_template_render(tmp_string, data): return rtemplate.render(**data) 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.out_coding = out_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={ 'json':json, 'hasattr':hasattr, @@ -41,6 +41,8 @@ class MyTemplateEngine: 'extname':lambda x:os.path.splitext(x)[-1], } self.env.globals.update(denv) + if env: + self.env.globals.update(env) def set(self,k,v): self.env.globals.update({k:v})