http_api.py 867 B

123456789101112131415161718192021222324252627282930313233
  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):
  9. async with aiohttp.ClientSession() as session:
  10. async with session.get(url) as response:
  11. result = await response.json()
  12. if response.status != 200:
  13. raise FetchException(response.status, result)
  14. return result
  15. pass
  16. async def post(url: str, data: dict):
  17. async with aiohttp.ClientSession() as session:
  18. async with session.post(url, json=data) as response:
  19. result = await response.json()
  20. if response.status != 200:
  21. raise FetchException(response.status, result)
  22. return result
  23. pass
  24. if __name__ == '__main__':
  25. pass