12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import json
- import xml.etree.ElementTree as ET
- import aiohttp
- import requests
- def _xml_to_dict(element):
- if len(element) == 0:
- return element.text
- return {child.tag: _xml_to_dict(child) for child in element}
- def xml_parser(xml_str: str):
- xml_root = ET.fromstring(xml_str)
- obj = _xml_to_dict(xml_root)
- return obj
- def xml_build(root_text: str, data: object):
- root = ET.Element(root_text)
- for key in data.__dir__():
- if not key.startswith('__'):
- value = getattr(data, key)
- if not callable(value) and value is not None:
- item = ET.SubElement(root, key)
- item.text = str(value)
- return root
- def xml_to_string(root: ET.Element):
- return ET.tostring(root, encoding='utf-8').decode('utf-8')
- def __get_access_token__():
- url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxf01ec97b45a4bc49&secret=ee1681fc5d4bfe8797de9c61ff15f08b"
- rsp = requests.get(url)
- obj = json.loads(rsp.text)
- return obj['access_token']
- def sync_post(url: str, body: object):
- return requests.post(url=url, json=body)
- async def post_as_normal(url: str, headers: object, body: object):
- async with aiohttp.ClientSession() as session:
- async with session.post(url, headers=headers, json=body) as response:
- return await response.text()
|