http_api.py 990 B

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2023/7/20 16:29
  3. # @Author : XuJiakai
  4. # @File : http_api
  5. # @Software: PyCharm
  6. import aiohttp
  7. from data_clean.exception.fetch_exception import FetchException
  8. async def get(url: str, result_json: bool = True):
  9. async with aiohttp.ClientSession() as session:
  10. async with session.get(url) as response:
  11. if result_json:
  12. result = await response.json()
  13. else:
  14. result = await response.text()
  15. if response.status != 200:
  16. raise FetchException(response.status, result)
  17. return result
  18. pass
  19. async def post(url: str, data: dict):
  20. async with aiohttp.ClientSession() as session:
  21. async with session.post(url, json=data) as response:
  22. result = await response.json()
  23. if response.status != 200:
  24. raise FetchException(response.status, result)
  25. return result
  26. pass
  27. if __name__ == '__main__':
  28. pass